File: realign.sh
   1 #!/bin/sh
   2 
   3 # The MIT License (MIT)
   4 #
   5 # Copyright (c) 2026 pacman64
   6 #
   7 # Permission is hereby granted, free of charge, to any person obtaining a copy
   8 # of this software and associated documentation files (the "Software"), to deal
   9 # in the Software without restriction, including without limitation the rights
  10 # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  11 # copies of the Software, and to permit persons to whom the Software is
  12 # furnished to do so, subject to the following conditions:
  13 #
  14 # The above copyright notice and this permission notice shall be included in
  15 # all copies or substantial portions of the Software.
  16 #
  17 # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  18 # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  19 # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  20 # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  21 # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  22 # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  23 # SOFTWARE.
  24 
  25 
  26 # realign [options...] [files...]
  27 #
  28 # Realign all detected columns, right-aligning any detected numbers in any
  29 # column, and keeping ANSI-sequences as given.
  30 #
  31 # The options are, available both in single and double-dash versions
  32 #
  33 #    -h, -help           show this help message
  34 #    -m, -max-columns    use the row with the most items for the item-count
  35 
  36 
  37 maxcols=0
  38 
  39 while [ $# -gt 0 ]; do
  40     case "$1" in
  41         -h|--h|-help|--help)
  42             awk '/^# +realign /, /^$/ { gsub(/^# ?/, ""); print }' "$0"
  43             exit 0
  44         ;;
  45 
  46         -m|--m|-maxcols|--maxcols|-maxcolumns|--maxcolumns|-max-columns|\
  47         --max-columns)
  48             maxcols=1
  49             shift
  50             continue
  51         ;;
  52     esac
  53 
  54     break
  55 done
  56 
  57 [ "$1" = '--' ] && shift
  58 
  59 # show all non-existing files given
  60 failed=0
  61 for arg in "$@"; do
  62     if [ "${arg}" = "-" ]; then
  63         continue
  64     fi
  65     if [ ! -e "${arg}" ]; then
  66         printf "no file named \"%s\"\n" "${arg}" >&2
  67         failed=1
  68     fi
  69 done
  70 
  71 if [ "${failed}" -gt 0 ]; then
  72     exit 2
  73 fi
  74 
  75 awk -v ncols="${maxcols}" '
  76 BEGIN { if (SUBSEP == "") SUBSEP = "\034" }
  77 
  78 # always ignore trailing carriage-returns
  79 { gsub(/\r$/, "") }
  80 
  81 # first non-empty line auto-detects SSV vs. TSV, and the column-count
  82 width == 0 { width = NF; if (/\t/) { FS = "\t"; $0 = $0 } }
  83 
  84 width > 0 {
  85     if (ncols && width < NF) width = NF;
  86     nitems[++nrows] = NF
  87 
  88     for (i = 1; i <= NF; i++) {
  89         data[nrows SUBSEP i] = $i
  90 
  91         plain = $i
  92         gsub(/\x1b\[[0-9;]*[A-Za-z]/, "", plain)
  93         w = length(plain)
  94         if (widths[i] < w) widths[i] = w
  95 
  96         # handle non-numbers
  97         if (!match(plain, /^[+-]?[0-9]+(\.[0-9]+)?$/)) continue
  98 
  99         # see if number has decimals
 100         if (!match(plain, /\./)) continue
 101 
 102         dd = w - (RSTART - 1)
 103         if (dot_decs[i] < dd) dot_decs[i] = dd
 104     }
 105 }
 106 
 107 function show_row() {
 108     due = 0
 109 
 110     for (j = 1; j <= width; j++) {
 111         v = data[i SUBSEP j]
 112 
 113         # put 2-space gaps between columns
 114         if (1 < j) due += 2
 115 
 116         if (v ~ /^ *$/) {
 117             due += widths[j]
 118             continue
 119         }
 120 
 121         plain = v
 122         gsub(/\x1b\[[0-9;]*[A-Za-z]/, "", plain)
 123         w = length(plain)
 124 
 125         # handle non-numbers
 126         if (!match(plain, /^[+-]?[0-9]+(\.[0-9]+)?$/)) {
 127             for (k = 1; k <= due; k++) printf " "
 128             printf "%s", v
 129 
 130             due = widths[j] - w
 131             continue
 132         }
 133 
 134         # count `dot-decimals` trail in the number
 135         dd = match(plain, /\./) ? w - (RSTART - 1) : 0
 136 
 137         rpad = dot_decs[j] - dd
 138         lpad = widths[j] - (w + rpad) + due
 139 
 140         for (k = 1; k <= lpad; k++) printf " "
 141         printf "%s", v
 142 
 143         due = rpad
 144     }
 145 }
 146 
 147 END {
 148     for (i = 1; i <= nrows; i++) {
 149         show_row()
 150 
 151         # treat extra columns as part of the last one
 152         last = nitems[i]
 153         for (j = width + 1; j <= last; j++) printf " %s", data[i SUBSEP j]
 154 
 155         print ""
 156     }
 157 }
 158 ' "$@"