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