File: realign.sh
   1 #!/bin/sh
   2 
   3 # The MIT License (MIT)
   4 #
   5 # Copyright © 2025 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 
  78         for (i = 1; i <= NF; i++) {
  79             data[nrows][i] = $i
  80 
  81             plain = $i
  82             gsub(/\x1b\[[0-9;]*[A-Za-z]/, "", plain)
  83             w = length(plain)
  84             if (widths[i] < w) widths[i] = w
  85 
  86             # handle non-numbers
  87             if (!match(plain, /^[+-]?[0-9]+(\.[0-9]+)?$/)) {
  88                 continue
  89             }
  90 
  91             # see if number has decimals
  92             if (match(plain, /\./)) {
  93                 dd = w - (RSTART - 1)
  94                 if (dot_decs[i] < dd) dot_decs[i] = dd
  95             }
  96         }
  97     }
  98 
  99     END {
 100         for (i = 1; i <= nrows; i++) {
 101             due = 0
 102 
 103             for (j = 1; j <= width; j++) {
 104                 v = data[i][j]
 105 
 106                 # put 2-space gaps between columns
 107                 if (1 < j) due += 2
 108 
 109                 if (v ~ /^ *$/) {
 110                     due += widths[j]
 111                     continue
 112                 }
 113 
 114                 plain = v
 115                 gsub(/\x1b\[[0-9;]*[A-Za-z]/, "", plain)
 116                 w = length(plain)
 117 
 118                 # handle non-numbers
 119                 if (!match(plain, /^[+-]?[0-9]+(\.[0-9]+)?$/)) {
 120                     printf "%*s%s", due, "", v
 121                     due = widths[j] - w
 122                     continue
 123                 }
 124 
 125                 # count `dot-decimals` trail in the number
 126                 dd = match(plain, /\./) ? w - (RSTART - 1) : 0
 127 
 128                 rpad = dot_decs[j] - dd
 129                 lpad = widths[j] - (w + rpad) + due
 130 
 131                 printf "%*s%s", lpad, "", v
 132                 due = rpad
 133             }
 134 
 135             # treat extra columns as part of the last one
 136             last = length(data[i])
 137             for (j = width + 1; j <= last; j++) printf " %s", data[i][j]
 138 
 139             printf "\n"
 140         }
 141     }
 142 ' "$@"