File: realign.sh
   1 #!/bin/sh
   2 
   3 # The MIT License (MIT)
   4 #
   5 # Copyright © 2024 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...] [filenames...]
  27 #
  28 # Realign all detected columns, right-aligning any detected numbers in any
  29 # column.
  30 #
  31 # To realign columns from TSV (tab-separated values) lines, use any among
  32 # these as the leading option: `-t`, `-tab` `-tabs`, or `-tsv`.
  33 #
  34 # Double-dashed variants of supported options are also supported.
  35 
  36 
  37 # handle leading options
  38 tsv=0
  39 case "$1" in
  40     -h|--h|-help|--help)
  41         awk '/^# +realign/, /^$/ { gsub(/^# ?/, ""); print }' "$0"
  42         exit 0
  43     ;;
  44     -t|--t|-tab|--tab|-tabs|--tabs|-tsv|--tsv)
  45         # enable TSV (tab-separated values) mode
  46         tsv=1
  47         shift
  48     ;;
  49 esac
  50 
  51 
  52 awk -v tsv="${tsv}" '
  53 BEGIN { if (tsv) FS = "\t" }
  54 
  55 function match_number(v) {
  56     return match(v, /^[+-]?[0-9]+(\.[0-9]+)?$/)
  57 }
  58 
  59 function match_dot_digits(v) {
  60     return match(v, /\.[0-9]+$/)
  61 }
  62 
  63 {
  64     gsub(/\r$/, "")
  65 
  66     for (i = 1; i <= NF; i++) {
  67         data[NR][i] = $i
  68 
  69         if (match_number($i)) {
  70             if (match_dot_digits($i)) {
  71                 dd = RLENGTH
  72                 if (dot_decs[i] < dd) dot_decs[i] = dd
  73                 iw = RSTART - 1
  74                 if (int_widths[i] < iw) int_widths[i] = iw
  75             } else {
  76                 w = length($i)
  77                 if (int_widths[i] < w) int_widths[i] = w
  78             }
  79 
  80             continue
  81         }
  82 
  83         w = length($i)
  84         if (widths[i] < w) widths[i] = w
  85     }
  86 }
  87 
  88 END {
  89     # fix column-widths using the number-padding info
  90     for (i = 1; i <= NF; i++) {
  91         w = int_widths[i] + dot_decs[i]
  92         if (widths[i] < w) widths[i] = w
  93     }
  94 
  95     for (i = 1; i <= NR; i++) {
  96         last = length(data[i])
  97 
  98         for (j = 1; j <= last; j++) {
  99             if (j > 1) printf "  " # put 2-space gaps between columns
 100 
 101             v = data[i][j]
 102 
 103             if (!match_number(v)) {
 104                 # avoid adding trailing spaces at the end of lines
 105                 printf "%*s", (j == last) ? 0 : -widths[j], v
 106                 continue
 107             }
 108 
 109             w = length(v)
 110             if (match_dot_digits(v)) {
 111                 dd = RLENGTH
 112                 iw = RSTART - 1
 113             } else {
 114                 dd = 0
 115                 iw = w
 116             }
 117 
 118             dpad = dot_decs[j] - dd
 119             ipad = int_widths[j] - iw
 120             if (ipad < 0) ipad = 0
 121             lpad = widths[j] - (ipad + w + dpad)
 122             if (lpad < 0) lpad = 0
 123 
 124             # avoid adding trailing spaces at the end of lines
 125             if (j == last) dpad = 0
 126 
 127             printf "%*s%*s%s%*s", lpad, "", ipad, "", v, dpad, ""
 128         }
 129 
 130         printf "\n"
 131     }
 132 }' "$@"