File: ncol.sh
   1 #!/bin/sh
   2 
   3 # The MIT License (MIT)
   4 #
   5 # Copyright © 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 # ncol [options...] [filenames...]
  27 #
  28 # Nice COLumns realigns and styles data tables using ANSI color sequences. In
  29 # particular, all auto-detected numbers are styled so they're easier to read
  30 # at a glance. Input tables can be either lines of space-separated values or
  31 # tab-separated values, and are auto-detected using the first non-empty line.
  32 #
  33 # When not given filepaths to read data from, this tool reads from standard
  34 # input by default.
  35 #
  36 # The only option is the help option, using any of `-h`, `--h`, `-help`, or
  37 # `--help`.
  38 
  39 
  40 case "$1" in
  41     -h|--h|-help|--help)
  42         awk '/^# +ncol /, /^$/ { gsub(/^# ?/, ""); print }' "$0"
  43         exit 0
  44     ;;
  45 esac
  46 
  47 [ "$1" = '--' ] && shift
  48 
  49 command='awk'
  50 if [ -e /usr/bin/gawk ]; then
  51     command='gawk'
  52 fi
  53 
  54 # show all non-existing files given
  55 failed=0
  56 for arg in "$@"; do
  57     if [ "${arg}" = "-" ]; then
  58         continue
  59     fi
  60     if [ ! -e "${arg}" ]; then
  61         printf "no file named \"%s\"\n" "${arg}" > /dev/stderr
  62         failed=1
  63     fi
  64 done
  65 
  66 if [ "${failed}" -gt 0 ]; then
  67     exit 2
  68 fi
  69 
  70 ${command} '
  71     # always ignore trailing carriage-returns
  72     { gsub(/\r$/, "") }
  73 
  74     # first non-empty line auto-detects whether input is SSV or TSV
  75     num_cols == 0 && /\t/ { FS = "\t"; $0 = $0 }
  76 
  77     # first non-empty line auto-detects number of table columns
  78     num_cols == 0 { num_cols = NF }
  79 
  80     num_cols > 0 {
  81         num_rows++;
  82 
  83         for (i = 1; i <= NF; i++) {
  84             data[num_rows][i] = $i
  85 
  86             plain = $i
  87             gsub(/\x1b\[[0-9;]*[A-Za-z]/, "", plain)
  88             w = length(plain)
  89             if (widths[i] < w) widths[i] = w
  90 
  91             # handle non-numbers
  92             if (!match(plain, /^[+-]?[0-9]+(\.[0-9]+)?$/)) {
  93                 continue
  94             }
  95 
  96             numbers[i]++
  97             sums[i] += plain + 0
  98 
  99             # count `dot-decimals` trail in the number
 100             if (match(plain, /\./)) {
 101                 dd = w - (RSTART - 1)
 102                 if (dot_decs[i] < dd) dot_decs[i] = dd
 103             }
 104         }
 105     }
 106 
 107     END {
 108         # fix column-widths using number-padding info and the column-totals
 109         for (i = 1; i <= num_cols; i++) {
 110             if (numbers[i] > 0) {
 111                 decs = dot_decs[i] > 0 ? dot_decs[i] - 1 : 0
 112                 w = length(sprintf("%.*f", decs, sums[i]))
 113             } else {
 114                 w = 1
 115             }
 116             if (widths[i] < w) widths[i] = w
 117         }
 118 
 119         # add fake-row with all the column-sums
 120         num_rows++
 121         for (i = 1; i <= num_cols; i++) {
 122             if (numbers[i] > 0) {
 123                 decs = dot_decs[i] > 0 ? dot_decs[i] - 1 : 0
 124                 data[num_rows][i] = sprintf("%.*f", decs, sums[i])
 125             } else {
 126                 data[num_rows][i] = "-"
 127             }
 128         }
 129 
 130         for (i = 1; i <= num_rows; i++) {
 131             # show tiles, except for the last fake-row with the sums
 132             for (j = 1; i < num_rows && j <= num_cols; j++) {
 133                 v = data[i][j]
 134 
 135                 if (v == "") {
 136                     printf "\x1b[0m○"
 137                     continue
 138                 }
 139 
 140                 if (!match(v, /^[+-]?[0-9]+(\.[0-9]+)?$/)) {
 141                     if (v ~ /^ | $/) printf "\x1b[38;2;196;160;0m■"
 142                     else printf "\x1b[38;2;128;128;128m■"
 143                     continue
 144                 }
 145 
 146                 if (v > 0) {
 147                     if (match(v, /\./)) printf "\x1b[38;2;0;155;95m■"
 148                     else printf "\x1b[38;2;0;135;0m■"
 149                     continue
 150                 }
 151 
 152                 if (v < 0) {
 153                     if (match(v, /\./)) printf "\x1b[38;2;215;95;95m■"
 154                     else printf "\x1b[38;2;204;0;0m■"
 155                     continue
 156                 }
 157 
 158                 printf "\x1b[38;2;0;95;215m■"
 159             }
 160 
 161             # show tiles for missing trailing fields, except for the fake-row
 162             if (i < num_rows) {
 163                 extra = num_cols - length(data[i])
 164                 if (extra > 0) printf "\x1b[0m"
 165                 for (j = 1; j <= extra; j++) printf "×"
 166                 printf "\x1b[0m  "
 167             } else {
 168                 if (num_cols > 0) printf "%*s", num_cols + 2, ""
 169             }
 170 
 171             due = 0
 172 
 173             # show/realign row fields
 174             for (j = 1; j <= num_cols; j++) {
 175                 v = data[i][j]
 176 
 177                 # put 2-space gaps between columns
 178                 if (1 < j) due += 2
 179 
 180                 if (v ~ /^ *$/) {
 181                     due += widths[j]
 182                     continue
 183                 }
 184 
 185                 plain = v
 186                 gsub(/\x1b\[[0-9;]*[A-Za-z]/, "", plain)
 187                 w = length(plain)
 188 
 189                 # handle non-numbers
 190                 if (!match(plain, /^[+-]?[0-9]+(\.[0-9]+)?$/)) {
 191                     printf "%*s%s", due, "", v
 192                     due = widths[j] - w
 193                     continue
 194                 }
 195 
 196                 # count `dot-decimals` trail in the number
 197                 dd = match(plain, /\./) ? w - (RSTART - 1) : 0
 198 
 199                 rpad = dot_decs[j] - dd
 200                 lpad = widths[j] - (w + rpad) + due
 201 
 202                 if (plain > 0) {
 203                     rgb = (dot_decs[j] > 0) ? "0;135;95" : "0;155;0"
 204                 } else if (plain < 0) {
 205                     rgb = (dot_decs[j] > 0) ? "215;95;95" : "204;0;0"
 206                 } else {
 207                     rgb = "0;95;215"
 208                 }
 209 
 210                 printf "%*s\x1b[38;2;%sm%s\x1b[0m", lpad, "", rgb, v
 211                 due = rpad
 212             }
 213 
 214             # treat extra fields as part of the last one
 215             last = length(data[i])
 216             for (j = num_cols + 1; j <= last; j++) printf " %s", data[i][j]
 217 
 218             printf "\n"
 219         }
 220     }
 221 ' "$@" | sed -E \
 222     -e 's-([0-9]{1,3})([0-9]{3})([0-9]{3})([0-9]{3})([0-9]{3})([0-9]{3})([0-9]{3})\x1b\[0m-\1\x1b[38;2;168;168;168m\2\x1b[0m\3\x1b[38;2;168;168;168m\4\x1b[0m\5\x1b[38;2;168;168;168m\6\x1b[0m\7-g' \
 223     -e 's-([0-9]{1,3})([0-9]{3})([0-9]{3})([0-9]{3})([0-9]{3})([0-9]{3})\x1b\[0m-\1\x1b[38;2;168;168;168m\2\x1b[0m\3\x1b[38;2;168;168;168m\4\x1b[0m\5\x1b[38;2;168;168;168m\6\x1b[0m-g' \
 224     -e 's-([0-9]{1,3})([0-9]{3})([0-9]{3})([0-9]{3})([0-9]{3})\x1b\[0m-\1\x1b[38;2;168;168;168m\2\x1b[0m\3\x1b[38;2;168;168;168m\4\x1b[0m\5-g' \
 225     -e 's-([0-9]{1,3})([0-9]{3})([0-9]{3})([0-9]{3})\x1b\[0m-\1\x1b[38;2;168;168;168m\2\x1b[0m\3\x1b[38;2;168;168;168m\4\x1b[0m-g' \
 226     -e 's-([0-9]{1,3})([0-9]{3})([0-9]{3})\x1b\[0m-\1\x1b[38;2;168;168;168m\2\x1b[0m\3-g' \
 227     -e 's-([0-9]{1,3})([0-9]{3})\x1b\[0m-\1\x1b[38;2;168;168;168m\2\x1b[0m-g'