Skip to content

Incorrect padding of too-long data names #23

@richardmoser

Description

@richardmoser
Image ^ screenshot of the `PRINT_DATA()` function (left) and the resulting error (right).

At least on Ubuntu 25.10 (no clue about other systems), the original code for PRINT_DATA() does not correctly handle cases where the data length is greater than the max allowed length.

After some testing in which I split the 3 condition if statement in line 147 ,
if (( data_len > MAX_DATA_LEN || data_len == MAX_DATA_LEN-1 )); then # changed '>=' to '>'
into an if and 2 elif statements based on each of the conditions, I found two issues:

  • The ( data_len > MAX_DATA_LEN ) case was truncating the data more than was necessary and not padding at the end.
    • An example of this is in the "OS" output in the screenshot above.
  • The ( data_len = MAX_DATA_LEN ) case was unnecessary and the else statement following the original compound if statement handled these cases fine.
    • The default bar graph code always generates graphs of the same length as the max, so each of these was printing incorrectly as seen above.
    • Removing the = from the first condition resolves this issue. It does not seem to cause issues after testing it by manually setting non-bar graph fields to strings with exactly the max allowed length.

I will try to get a PR in with these updates.

The corrected PRINT_DATA() function is below:

PRINT_DATA() {
    local name="$1"
    local data="$2"
    local max_data_len=$CURRENT_LEN

    # Pad name
    local name_len=${#name}
    if (( name_len < MIN_NAME_LEN )); then
        name=$(printf "%-${MIN_NAME_LEN}s" "$name")
    elif (( name_len > MAX_NAME_LEN )); then
        name=$(echo "$name" | cut -c 1-$((MAX_NAME_LEN-3)))...
    else
        name=$(printf "%-${MAX_NAME_LEN}s" "$name")
    fi

    # Truncate or pad data
    local data_len=${#data}
#    if (( data_len >= MAX_DATA_LEN || data_len == MAX_DATA_LEN-1 )); then    # the original line
    if (( data_len > MAX_DATA_LEN || data_len == MAX_DATA_LEN-1 )); then      # changed '>=' to '>'
        data=$(echo "$data" | cut -c 1-$((MAX_DATA_LEN-3-2)))...                # the original line
        data=$(printf "%-${max_data_len}s" "$data")                             # added a line to pad after truncation
    else
        data=$(printf "%-${max_data_len}s" "$data")
    fi

    printf "│ %-${MAX_NAME_LEN}s │ %s │\n" "$name" "$data"
}
Image ^ screenshot of the corrected `PRINT_DATA()` function (left) and the output (right).

There is also an error from line 308 due to lastlog not being installed or available for Ubuntu 25.10, but this doesn't seem to affect anything and commenting that line out removes the error.

Thanks for creating this neat tool and letting me get a little practice in with bash scripting!

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