File: ncol.sh
   1 #!/bin/sh
   2 
   3 # The MIT License (MIT)
   4 #
   5 # Copyright © 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 # 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         nitems[num_rows] = NF
  83 
  84         for (i = 1; i <= NF; i++) {
  85             data[num_rows][i] = $i
  86 
  87             plain = $i
  88             gsub(/\x1b\[[0-9;]*[A-Za-z]/, "", plain)
  89             w = length(plain)
  90             if (widths[i] < w) widths[i] = w
  91 
  92             # handle non-numbers
  93             if (!match(plain, /^[+-]?[0-9]+(\.[0-9]+)?$/)) {
  94                 continue
  95             }
  96 
  97             numbers[i]++
  98             sums[i] += plain + 0
  99 
 100             # count `dot-decimals` trail in the number
 101             if (match(plain, /\./)) {
 102                 dd = w - (RSTART - 1)
 103                 if (dot_decs[i] < dd) dot_decs[i] = dd
 104             }
 105         }
 106     }
 107 
 108     END {
 109         # fix column-widths using number-padding info and the column-totals
 110         for (i = 1; i <= num_cols; i++) {
 111             if (numbers[i] > 0) {
 112                 decs = dot_decs[i] > 0 ? dot_decs[i] - 1 : 0
 113                 w = length(sprintf("%.*f", decs, sums[i]))
 114             } else {
 115                 w = 1
 116             }
 117             if (widths[i] < w) widths[i] = w
 118         }
 119 
 120         # add fake-row with all the column-sums
 121         num_rows++
 122         for (i = 1; i <= num_cols; i++) {
 123             if (numbers[i] > 0) {
 124                 decs = dot_decs[i] > 0 ? dot_decs[i] - 1 : 0
 125                 data[num_rows][i] = sprintf("%.*f", decs, sums[i])
 126             } else {
 127                 data[num_rows][i] = "-"
 128             }
 129         }
 130 
 131         for (i = 1; i <= num_rows; i++) {
 132             # show tiles, except for the last fake-row with the sums
 133             for (j = 1; i < num_rows && j <= num_cols; j++) {
 134                 v = data[i][j]
 135 
 136                 if (v == "") {
 137                     printf "\x1b[0m○"
 138                     continue
 139                 }
 140 
 141                 if (!match(v, /^[+-]?[0-9]+(\.[0-9]+)?$/)) {
 142                     if (v ~ /^ | $/) printf "\x1b[38;2;196;160;0m■"
 143                     else printf "\x1b[38;2;128;128;128m■"
 144                     continue
 145                 }
 146 
 147                 if (v > 0) {
 148                     if (match(v, /\./)) printf "\x1b[38;2;0;155;95m■"
 149                     else printf "\x1b[38;2;0;135;0m■"
 150                     continue
 151                 }
 152 
 153                 if (v < 0) {
 154                     if (match(v, /\./)) printf "\x1b[38;2;215;95;95m■"
 155                     else printf "\x1b[38;2;204;0;0m■"
 156                     continue
 157                 }
 158 
 159                 printf "\x1b[38;2;0;95;215m■"
 160             }
 161 
 162             # show tiles for missing trailing fields, except for the fake-row
 163             if (i < num_rows) {
 164                 extra = num_cols - nitems[i]
 165                 if (extra > 0) printf "\x1b[0m"
 166                 for (j = 1; j <= extra; j++) printf "×"
 167                 printf "\x1b[0m  "
 168             } else {
 169                 if (num_cols > 0) printf "%*s", num_cols + 2, ""
 170             }
 171 
 172             due = 0
 173 
 174             # show/realign row fields
 175             for (j = 1; j <= num_cols; j++) {
 176                 v = data[i][j]
 177 
 178                 # put 2-space gaps between columns
 179                 if (1 < j) due += 2
 180 
 181                 if (v ~ /^ *$/) {
 182                     due += widths[j]
 183                     continue
 184                 }
 185 
 186                 plain = v
 187                 gsub(/\x1b\[[0-9;]*[A-Za-z]/, "", plain)
 188                 w = length(plain)
 189 
 190                 # handle non-numbers
 191                 if (!match(plain, /^[+-]?[0-9]+(\.[0-9]+)?$/)) {
 192                     printf "%*s%s", due, "", v
 193                     due = widths[j] - w
 194                     continue
 195                 }
 196 
 197                 # count `dot-decimals` trail in the number
 198                 dd = match(plain, /\./) ? w - (RSTART - 1) : 0
 199 
 200                 rpad = dot_decs[j] - dd
 201                 lpad = widths[j] - (w + rpad) + due
 202 
 203                 if (plain > 0) {
 204                     rgb = (dot_decs[j] > 0) ? "0;135;95" : "0;155;0"
 205                 } else if (plain < 0) {
 206                     rgb = (dot_decs[j] > 0) ? "215;95;95" : "204;0;0"
 207                 } else {
 208                     rgb = "0;95;215"
 209                 }
 210 
 211                 printf "%*s\x1b[38;2;%sm%s\x1b[0m", lpad, "", rgb, v
 212                 due = rpad
 213             }
 214 
 215             # treat extra fields as part of the last one
 216             last = nitems[i]
 217             for (j = num_cols + 1; j <= last; j++) printf " %s", data[i][j]
 218 
 219             printf "\n"
 220         }
 221     }
 222 ' "$@" | sed -E \
 223     -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' \
 224     -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' \
 225     -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' \
 226     -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' \
 227     -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' \
 228     -e 's-([0-9]{1,3})([0-9]{3})\x1b\[0m-\1\x1b[38;2;168;168;168m\2\x1b[0m-g'