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