File: nt.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 # nt [options...] [filenames...]
  27 #
  28 # Nice Tables realigns and styles TSV (tab-separated values) data using ANSI
  29 # sequences. When not given filepaths to read data from, this tool reads from
  30 # standard input.
  31 #
  32 # If you're using Linux or MacOS, you may find this cmd-line shortcut useful:
  33 #
  34 # # View Nice Table(s) / Very Nice Table(s); uses my scripts `nt` and `nn`
  35 # vnt() {
  36 #     awk 1 "$@" | nl -b a -w 1 -v 0 | nt | nn |
  37 #         awk '(NR - 1) % 5 == 1 && NR > 1 { print "" } 1' | less -JMKiCRS
  38 # }
  39 
  40 
  41 # handle leading options
  42 case "$1" in
  43     -h|--h|-help|--help)
  44         awk '/^# +nt/, /^$/ { gsub(/^# ?/, ""); print }' "$0"
  45         exit 0
  46     ;;
  47 esac
  48 
  49 
  50 awk -F "\t" '
  51 function match_number(v) {
  52     return match(v, /^[+-]?[0-9]+(\.[0-9]+)?$/)
  53 }
  54 
  55 function match_dot_digits(v) {
  56     return match(v, /\.[0-9]+$/)
  57 }
  58 
  59 function show_tiles(i, l, j, v) {
  60     printf "\x1b[48;5;255m"
  61 
  62     for (j = 1; j <= l; j++) {
  63         v = data[i][j]
  64 
  65         if (v == "") {
  66             printf "%s", "\x1b[0m\x1b[48;5;255m○"
  67             continue
  68         }
  69 
  70         if (!match_number(v)) {
  71             v = v ~ /^ | $/ ? "\x1b[38;5;3m■" : "\x1b[0m■"
  72             printf "%s", v
  73             continue
  74         }
  75 
  76         if (v > 0) {
  77             v = match_dot_digits(v) ? "\x1b[38;5;29m■" : "\x1b[38;5;22m■"
  78             printf "%s", v
  79             continue
  80         }
  81 
  82         if (v < 0) {
  83             v = match_dot_digits(v) ? "\x1b[38;5;167m■" : "\x1b[38;5;1m■"
  84             printf "%s", v
  85             continue
  86         }
  87 
  88         printf "%s", "\x1b[38;5;26m■"
  89     }
  90 
  91     printf "\x1b[0m\x1b[48;5;255m"
  92     for (j = l + 1; j <= num_cols; j++) {
  93         printf "%s", "×"
  94     }
  95     printf "\x1b[0m"
  96 }
  97 
  98 function show_number(v, j, last, w, dd, iw, dpad, ipad, lpad, style) {
  99     if (match_dot_digits(v)) {
 100         dd = RLENGTH
 101         iw = RSTART - 1
 102     } else {
 103         dd = 0
 104         iw = w
 105     }
 106 
 107     dpad = dot_decs[j] - dd
 108     ipad = int_widths[j] - iw
 109     if (ipad < 0) ipad = 0
 110     lpad = widths[j] - (ipad + w + dpad)
 111     if (lpad < 0) lpad = 0
 112 
 113     # avoid adding trailing spaces at the end of lines
 114     if (j == last) dpad = 0
 115 
 116     if (v > 0) {
 117         style = dot_decs[j] > 0 ? "\x1b[38;5;29m" : "\x1b[38;5;22m"
 118     } else if (v < 0) {
 119         style = dot_decs[j] > 0 ? "\x1b[38;5;167m" : "\x1b[38;5;1m"
 120     } else {
 121         style = "\x1b[38;5;26m"
 122     }
 123 
 124     printf "%*s%*s%s%s\x1b[0m%*s", lpad, "", ipad, "", style, v, dpad, ""
 125 }
 126 
 127 {
 128     gsub(/\r$/, "")
 129     if (num_cols < NF) num_cols = NF
 130 
 131     for (i = 1; i <= NF; i++) {
 132         data[NR][i] = $i
 133 
 134         if (match_number($i)) {
 135             numbers[i]++
 136             sums[i] += $i + 0
 137 
 138             if (match_dot_digits($i)) {
 139                 dd = RLENGTH
 140                 if (dot_decs[i] < dd) dot_decs[i] = dd
 141                 iw = RSTART - 1
 142                 if (int_widths[i] < iw) int_widths[i] = iw
 143             } else {
 144                 w = length($i)
 145                 if (int_widths[i] < w) int_widths[i] = w
 146             }
 147 
 148             continue
 149         }
 150 
 151         w = length($i)
 152         if (widths[i] < w) widths[i] = w
 153     }
 154 }
 155 
 156 END {
 157     # fix column-widths using the number-padding info, and the column-totals
 158     for (i = 1; i <= num_cols; i++) {
 159         if (numbers[i] > 0) {
 160             decs = dot_decs[i] > 0 ? dot_decs[i] - 1 : 0
 161             w = length(sprintf("%.*f", decs, sums[i]))
 162         } else {
 163             w = 1
 164         }
 165         if (widths[i] < w) widths[i] = w
 166 
 167         w = int_widths[i] + dot_decs[i]
 168         if (widths[i] < w) widths[i] = w
 169     }
 170 
 171     for (i = 1; i <= NR; i++) {
 172         last = length(data[i])
 173         show_tiles(i, last)
 174         printf "  "
 175 
 176         for (j = 1; j <= last; j++) {
 177             if (j > 1) printf "  " # put 2-space gaps between columns
 178 
 179             v = data[i][j]
 180 
 181             if (!match_number(v)) {
 182                 # avoid adding trailing spaces at the end of lines
 183                 printf "%*s", (j == last) ? 0 : -widths[j], v
 184                 continue
 185             }
 186 
 187             show_number(v, j, last, length(v))
 188         }
 189 
 190         printf "\n"
 191     }
 192 
 193     # show extra row with the column-sums
 194     if (num_cols > 0) printf "%*s", num_cols, ""
 195     for (i = 1; i <= num_cols; i++) {
 196         printf "  "
 197         if (numbers[i] > 0) {
 198             decs = dot_decs[i] > 0 ? dot_decs[i] - 1 : 0
 199             s = sprintf("%.*f", decs, sums[i])
 200             show_number(s, i, num_cols, length(s))
 201         } else {
 202             printf "%*s", -widths[i], "-"
 203         }
 204     }
 205     printf "\n"
 206 }' "$@"