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