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.
  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     { gsub(/\r$/, "") }
  67 
  68     FNR == 1 { FS = ($0 ~ /\t/) ? "\t" : " "; $0 = $0 }
  69 
  70     {
  71         for (i = 1; i <= NF; i++) {
  72             data[NR][i] = $i
  73 
  74             if (match_number($i)) {
  75                 if (match_dot_digits($i)) {
  76                     dd = RLENGTH
  77                     if (dot_decs[i] < dd) dot_decs[i] = dd
  78                     iw = RSTART - 1
  79                     if (int_widths[i] < iw) int_widths[i] = iw
  80                 } else {
  81                     w = length($i)
  82                     if (int_widths[i] < w) int_widths[i] = w
  83                 }
  84 
  85                 continue
  86             }
  87 
  88             w = length($i)
  89             if (widths[i] < w) widths[i] = w
  90         }
  91     }
  92 
  93     END {
  94         # fix column-widths using the number-padding info
  95         for (i = 1; i <= NF; i++) {
  96             w = int_widths[i] + dot_decs[i]
  97             if (widths[i] < w) widths[i] = w
  98         }
  99 
 100         for (i = 1; i <= NR; i++) {
 101             last = length(data[i])
 102 
 103             for (j = 1; j <= last; j++) {
 104                 v = data[i][j]
 105 
 106                 # put 2-space gaps between columns
 107                 if (j > 1 && j < last) printf "  "
 108                 else if (j == last && v != "") printf "  "
 109 
 110                 if (!match_number(v)) {
 111                     # avoid adding trailing spaces at the end of lines
 112                     printf "%*s", (j == last) ? 0 : -widths[j], v
 113                     continue
 114                 }
 115 
 116                 w = length(v)
 117                 if (match_dot_digits(v)) {
 118                     dd = RLENGTH
 119                     iw = RSTART - 1
 120                 } else {
 121                     dd = 0
 122                     iw = w
 123                 }
 124 
 125                 dpad = dot_decs[j] - dd
 126                 ipad = int_widths[j] - iw
 127                 if (ipad < 0) ipad = 0
 128                 lpad = widths[j] - (ipad + w + dpad)
 129                 if (lpad < 0) lpad = 0
 130 
 131                 # avoid adding trailing spaces at the end of lines
 132                 if (j == last) dpad = 0
 133 
 134                 printf "%*s%*s%s%*s", lpad, "", ipad, "", v, dpad, ""
 135             }
 136 
 137             printf "\n"
 138         }
 139     }
 140 ' "$@"