Skip to content

PRINT_DATA truncation misaligns right border when data is near MAX_DATA_LEN #25

Description

@pmarreck

Bug Description

The PRINT_DATA function in machine_report.sh has a truncation bug that causes the right border to appear too far to the left when data is truncated with ....

Root Cause

Two issues in PRINT_DATA (around line 146):

if (( data_len >= MAX_DATA_LEN || data_len == MAX_DATA_LEN-1 )); then
    data=$(echo "$data" | cut -c 1-$((MAX_DATA_LEN-3-2)))...
else
    data=$(printf "%-${max_data_len}s" "$data")
fi

1. Missing padding after truncation: When data is truncated, the result is MAX_DATA_LEN-3-2 + ... = 30 characters, but it's never padded back to CURRENT_LEN. The else branch pads, but the if branch doesn't. This causes the right to shift left by CURRENT_LEN - 30 characters.

2. Over-eager truncation threshold: The condition data_len >= MAX_DATA_LEN || data_len == MAX_DATA_LEN-1 (effectively >= 31) uses the constant MAX_DATA_LEN instead of the dynamic max_data_len (which is CURRENT_LEN). This incorrectly truncates data that fits perfectly in the column — notably bar graphs, which are generated at exactly CURRENT_LEN width.

Visual Example

│ LOAD  1m      │ ██░░░░░░░... │      ← border shifted left
│ LOAD  5m      │ ██░░░░░░░... │
│ DISK USAGE    │ █████████... │
│ USAGE         │ █████████... │
│ HOSTNAME      │ framework-laptop                │  ← correct alignment

Suggested Fix

# Truncate or pad data
local data_len=${#data}
if (( data_len > max_data_len )); then
    data=$(echo "$data" | cut -c 1-$((max_data_len-3)))...
fi
data=$(printf "%-${max_data_len}s" "$data")

Changes:

  • Compare against max_data_len (dynamic) instead of MAX_DATA_LEN (constant)
  • Use > instead of >= so data that fits exactly isn't truncated
  • Always pad to max_data_len, regardless of whether truncation occurred

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions