File: realign.sh
   1 #!/bin/sh
   2 
   3 # The MIT License (MIT)
   4 #
   5 # Copyright © 2020-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 
  32 case "$1" in
  33     -h|--h|-help|--help)
  34         awk '/^# +realign /, /^$/ { gsub(/^# ?/, ""); print }' "$0"
  35         exit 0
  36     ;;
  37 esac
  38 
  39 [ "$1" = "--" ] && shift
  40 
  41 command='awk'
  42 if [ -e /usr/bin/gawk ]; then
  43     command='gawk'
  44 fi
  45 
  46 # show all non-existing files given
  47 failed=0
  48 for arg in "$@"; do
  49     if [ "${arg}" = "-" ]; then
  50         continue
  51     fi
  52     if [ ! -e "${arg}" ]; then
  53         printf "no file named \"%s\"\n" "${arg}" > /dev/stderr
  54         failed=1
  55     fi
  56 done
  57 
  58 if [ "${failed}" -gt 0 ]; then
  59     exit 2
  60 fi
  61 
  62 ${command} '
  63     function match_number(v) { return match(v, /^[+-]?[0-9]+(\.[0-9]+)?$/) }
  64     function match_dot_digits(v) { return match(v, /\.[0-9]+$/) }
  65 
  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 
  85             if (!match_number(plain)) {
  86                 if (widths[i] < w) widths[i] = w
  87                 continue
  88             }
  89 
  90             if (match_dot_digits(plain)) {
  91                 dd = RLENGTH
  92                 if (dot_decs[i] < dd) dot_decs[i] = dd
  93                 iw = RSTART - 1
  94                 if (int_widths[i] < iw) int_widths[i] = iw
  95                 w = iw + dd
  96             } else {
  97                 if (int_widths[i] < w) int_widths[i] = w
  98             }
  99 
 100             if (widths[i] < w) widths[i] = w
 101         }
 102     }
 103 
 104     END {
 105         for (i = 1; i <= nrows; i++) {
 106             due = 0
 107 
 108             for (j = 1; j <= width; j++) {
 109                 v = data[i][j]
 110 
 111                 # put 2-space gaps between columns
 112                 if (1 < j) due += 2
 113 
 114                 if (v ~ /^ *$/) {
 115                     due += widths[j]
 116                     continue
 117                 }
 118 
 119                 printf "%*s", due, ""
 120                 due = 0
 121 
 122                 plain = v
 123                 gsub(/\x1b\[[0-9;]*[A-Za-z]/, "", plain)
 124                 w = length(plain)
 125 
 126                 if (!match_number(plain)) {
 127                     printf "%s", v
 128                     due = widths[j] - w
 129                     continue
 130                 }
 131 
 132                 if (match_dot_digits(plain)) {
 133                     dd = RLENGTH
 134                     iw = RSTART - 1
 135                 } else {
 136                     dd = 0
 137                     iw = w
 138                 }
 139 
 140                 dpad = dot_decs[j] - dd
 141                 ipad = int_widths[j] - iw
 142                 if (ipad < 0) ipad = 0
 143                 lpad = widths[j] - (ipad + w + dpad)
 144                 if (lpad < 0) lpad = 0
 145 
 146                 printf "%*s%*s%s", lpad, "", ipad, "", v
 147                 due = dpad
 148             }
 149 
 150             # treat extra columns as part of the last one
 151             last = length(data[i])
 152             for (j = width + 1; j <= last; j++) printf " %s", data[i][j]
 153 
 154             printf "\n"
 155         }
 156     }
 157 ' "$@"