]> git.tue.mpg.de Git - gsu.git/commitdiff
subcommand: Improve formatting of command list.
authorAndre Noll <maan@tuebingen.mpg.de>
Wed, 2 Aug 2023 20:35:16 +0000 (22:35 +0200)
committerAndre Noll <maan@tuebingen.mpg.de>
Sun, 20 Aug 2023 14:27:44 +0000 (16:27 +0200)
If a gsu application is run without specifying a subcommand we print
the available subcommands in tabular form. Currently, this output
always consists of four columns and we also assume that the length
of a subcommand name is shorter than 16 characters. If longer names
exist, the help output is formatted incorrectly. This commit should
fix that. We now compute the maximal command length and the width of
the terminal upfront, and derive the number of columns for the output
from this information.

subcommand

index e5c0001e4903299e8e9e4f430a03373fd18b0e5a..817486dba0c43564701095b49f2069498da9ee6b 100644 (file)
@@ -168,20 +168,25 @@ gsu_getopts()
 _gsu_print_available_commands()
 {
        local cmd cmds
-       local -i count=0
+       local -i maxlen=0 cols width=80 count=0
 
+       result=$(stty size 2>/dev/null)
+       if (($? == 0)); then
+               gsu_is_a_number "${result#* }"
+               ((ret >= 0)) && ((result > 0)) && width=$result
+       fi
        _gsu_available_commands
-       cmds="$result"
-       printf 'Available commands:\n'
+       cmds=$result
+       for cmd in $cmds; do
+               ((${#cmd} > maxlen)) && maxlen=${#cmd}
+       done
+       let maxlen++
+       ((width < maxlen)) && cols=1 || cols=$((width / maxlen))
+       printf 'Available commands:'
        for cmd in $cmds; do
-               printf '%s' "$cmd"
+               ((count % cols == 0)) && printf '\n'
+               printf '%-*s' $maxlen $cmd
                let ++count
-               if ((count % 4)); then
-                       printf '\t'
-                       ((${#cmd} < 8)) && printf '\t'
-               else
-                       printf '\n'
-               fi
        done
        printf '\n'
 }