File: realign.sh
   1 #!/bin/sh
   2 
   3 # The MIT License (MIT)
   4 #
   5 # Copyright © 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 [filenames...]
  27 #
  28 # Realign all detected columns, right-aligning any detected numbers in any
  29 # column. ANSI style-codes are also kept as given.
  30 #
  31 # The only option available is to show this help message, using any of
  32 # `-h`, `--h`, `-help`, or `--help`, without the quotes.
  33 
  34 
  35 case "$1" in
  36     -h|--h|-help|--help)
  37         awk '/^# +realign /, /^$/ { gsub(/^# ?/, ""); print }' "$0"
  38         exit 0
  39     ;;
  40 esac
  41 
  42 [ "$1" = '--' ] && shift
  43 
  44 command='awk'
  45 if [ -e /usr/bin/gawk ]; then
  46     command='gawk'
  47 fi
  48 
  49 # show all non-existing files given
  50 failed=0
  51 for arg in "$@"; do
  52     if [ "${arg}" = "-" ]; then
  53         continue
  54     fi
  55     if [ ! -e "${arg}" ]; then
  56         printf "no file named \"%s\"\n" "${arg}" > /dev/stderr
  57         failed=1
  58     fi
  59 done
  60 
  61 if [ "${failed}" -gt 0 ]; then
  62     exit 2
  63 fi
  64 
  65 ${command} '
  66     # always ignore trailing carriage-returns
  67     { gsub(/\r$/, "") }
  68 
  69     # first non-empty line auto-detects whether input is SSV or TSV
  70     width == 0 && /\t/ { FS = "\t"; $0 = $0 }
  71 
  72     # first non-empty line auto-detects number of table columns
  73     width == 0 { width = NF }
  74 
  75     width > 0 {
  76         nrows++
  77         nitems[nrows] = NF
  78 
  79         for (i = 1; i <= NF; i++) {
  80             data[nrows][i] = $i
  81 
  82             plain = $i
  83             gsub(/\x1b\[[0-9;]*[A-Za-z]/, "", plain)
  84             w = length(plain)
  85             if (widths[i] < w) widths[i] = w
  86 
  87             # handle non-numbers
  88             if (!match(plain, /^[+-]?[0-9]+(\.[0-9]+)?$/)) {
  89                 continue
  90             }
  91 
  92             # see if number has decimals
  93             if (match(plain, /\./)) {
  94                 dd = w - (RSTART - 1)
  95                 if (dot_decs[i] < dd) dot_decs[i] = dd
  96             }
  97         }
  98     }
  99 
 100     END {
 101         for (i = 1; i <= nrows; i++) {
 102             due = 0
 103 
 104             for (j = 1; j <= width; j++) {
 105                 v = data[i][j]
 106 
 107                 # put 2-space gaps between columns
 108                 if (1 < j) due += 2
 109 
 110                 if (v ~ /^ *$/) {
 111                     due += widths[j]
 112                     continue
 113                 }
 114 
 115                 plain = v
 116                 gsub(/\x1b\[[0-9;]*[A-Za-z]/, "", plain)
 117                 w = length(plain)
 118 
 119                 # handle non-numbers
 120                 if (!match(plain, /^[+-]?[0-9]+(\.[0-9]+)?$/)) {
 121                     printf "%*s%s", due, "", v
 122                     due = widths[j] - w
 123                     continue
 124                 }
 125 
 126                 # count `dot-decimals` trail in the number
 127                 dd = match(plain, /\./) ? w - (RSTART - 1) : 0
 128 
 129                 rpad = dot_decs[j] - dd
 130                 lpad = widths[j] - (w + rpad) + due
 131 
 132                 printf "%*s%s", lpad, "", v
 133                 due = rpad
 134             }
 135 
 136             # treat extra columns as part of the last one
 137             last = nitems[i]
 138             for (j = width + 1; j <= last; j++) printf " %s", data[i][j]
 139 
 140             printf "\n"
 141         }
 142     }
 143 ' "$@"