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 for arg in "$@"; do
  40     if [ "${arg}" = '--' ]; then
  41         shift
  42         continue
  43     fi
  44 
  45     case "${arg}" in
  46         -h|--h|-help|--help)
  47             awk '/^# +realign /, /^$/ { gsub(/^# ?/, ""); print }' "$0"
  48             exit 0
  49         ;;
  50 
  51         -m|--m|-maxcols|--maxcols|-maxcolumns|--maxcolumns|-max-columns|\
  52         --max-columns)
  53             maxcols=1
  54             shift
  55             continue
  56         ;;
  57     esac
  58 
  59     break
  60 done
  61 
  62 # show all non-existing files given
  63 failed=0
  64 for arg in "$@"; do
  65     if [ "${arg}" = "-" ]; then
  66         continue
  67     fi
  68     if [ ! -e "${arg}" ]; then
  69         printf "no file named \"%s\"\n" "${arg}" > /dev/stderr
  70         failed=1
  71     fi
  72 done
  73 
  74 if [ "${failed}" -gt 0 ]; then
  75     exit 2
  76 fi
  77 
  78 awk -v maxcols="${maxcols}" '
  79     BEGIN { if (SUBSEP == "") SUBSEP = "\034" }
  80 
  81     # always ignore trailing carriage-returns
  82     { gsub(/\r$/, "") }
  83 
  84     # first non-empty line auto-detects SSV vs. TSV, and the column-count
  85     width == 0 { width = NF; if (/\t/) { FS = "\t"; $0 = $0 } }
  86 
  87     width > 0 {
  88         if (maxcols && width < NF) width = NF;
  89         nitems[++nrows] = NF
  90 
  91         for (i = 1; i <= NF; i++) {
  92             data[nrows SUBSEP i] = $i
  93 
  94             plain = $i
  95             gsub(/\x1b\[[0-9;]*[A-Za-z]/, "", plain)
  96             w = length(plain)
  97             if (widths[i] < w) widths[i] = w
  98 
  99             # handle non-numbers
 100             if (!match(plain, /^[+-]?[0-9]+(\.[0-9]+)?$/)) continue
 101 
 102             # see if number has decimals
 103             if (!match(plain, /\./)) continue
 104 
 105             dd = w - (RSTART - 1)
 106             if (dot_decs[i] < dd) dot_decs[i] = dd
 107         }
 108     }
 109 
 110     END {
 111         for (i = 1; i <= nrows; i++) {
 112             due = 0
 113 
 114             for (j = 1; j <= width; j++) {
 115                 v = data[i SUBSEP j]
 116 
 117                 # put 2-space gaps between columns
 118                 if (1 < j) due += 2
 119 
 120                 if (v ~ /^ *$/) {
 121                     due += widths[j]
 122                     continue
 123                 }
 124 
 125                 plain = v
 126                 gsub(/\x1b\[[0-9;]*[A-Za-z]/, "", plain)
 127                 w = length(plain)
 128 
 129                 # handle non-numbers
 130                 if (!match(plain, /^[+-]?[0-9]+(\.[0-9]+)?$/)) {
 131                     for (k = 1; k <= due; k++) printf " "
 132                     printf "%s", v
 133 
 134                     due = widths[j] - w
 135                     continue
 136                 }
 137 
 138                 # count `dot-decimals` trail in the number
 139                 dd = match(plain, /\./) ? w - (RSTART - 1) : 0
 140 
 141                 rpad = dot_decs[j] - dd
 142                 lpad = widths[j] - (w + rpad) + due
 143 
 144                 for (k = 1; k <= lpad; k++) printf " "
 145                 printf "%s", v
 146 
 147                 due = rpad
 148             }
 149 
 150             # treat extra columns as part of the last one
 151             last = nitems[i]
 152             for (j = width + 1; j <= last; j++) printf " %s", data[i SUBSEP j]
 153 
 154             print ""
 155         }
 156     }
 157 ' "$@"