#!/bin/sh # The MIT License (MIT) # # Copyright © 2024 pacman64 # # Permission is hereby granted, free of charge, to any person obtaining a copy # of this software and associated documentation files (the “Software”), to deal # in the Software without restriction, including without limitation the rights # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell # copies of the Software, and to permit persons to whom the Software is # furnished to do so, subject to the following conditions: # # The above copyright notice and this permission notice shall be included in # all copies or substantial portions of the Software. # # THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE # SOFTWARE. # nt [options...] [filenames...] # # Nice Tables realigns and styles TSV (tab-separated values) data using ANSI # sequences. When not given filepaths to read data from, this tool reads from # standard input. # # If you're using Linux or MacOS, you may find this cmd-line shortcut useful: # # # View Nice Table(s) / Very Nice Table(s); uses my scripts `nt` and `nn` # vnt() { # awk 1 "$@" | nl -b a -w 1 -v 0 | nt | nn | # awk '(NR - 1) % 5 == 1 && NR > 1 { print "" } 1' | less -JMKiCRS # } # handle leading options case "$1" in -h|--h|-help|--help) awk '/^# +nt/, /^$/ { gsub(/^# ?/, ""); print }' "$0" exit 0 ;; esac awk -F "\t" ' function match_number(v) { return match(v, /^[+-]?[0-9]+(\.[0-9]+)?$/) } function match_dot_digits(v) { return match(v, /\.[0-9]+$/) } function show_tiles(i, l, j, v) { printf "\x1b[48;5;255m" for (j = 1; j <= l; j++) { v = data[i][j] if (v == "") { printf "%s", "\x1b[0m\x1b[48;5;255m○" continue } if (!match_number(v)) { v = v ~ /^ | $/ ? "\x1b[38;5;3m■" : "\x1b[0m■" printf "%s", v continue } if (v > 0) { v = match_dot_digits(v) ? "\x1b[38;5;29m■" : "\x1b[38;5;22m■" printf "%s", v continue } if (v < 0) { v = match_dot_digits(v) ? "\x1b[38;5;167m■" : "\x1b[38;5;1m■" printf "%s", v continue } printf "%s", "\x1b[38;5;26m■" } printf "\x1b[0m\x1b[48;5;255m" for (j = l + 1; j <= num_cols; j++) { printf "%s", "×" } printf "\x1b[0m" } function show_number(v, j, last, w, dd, iw, dpad, ipad, lpad, style) { if (match_dot_digits(v)) { dd = RLENGTH iw = RSTART - 1 } else { dd = 0 iw = w } dpad = dot_decs[j] - dd ipad = int_widths[j] - iw if (ipad < 0) ipad = 0 lpad = widths[j] - (ipad + w + dpad) if (lpad < 0) lpad = 0 # avoid adding trailing spaces at the end of lines if (j == last) dpad = 0 if (v > 0) { style = dot_decs[j] > 0 ? "\x1b[38;5;29m" : "\x1b[38;5;22m" } else if (v < 0) { style = dot_decs[j] > 0 ? "\x1b[38;5;167m" : "\x1b[38;5;1m" } else { style = "\x1b[38;5;26m" } printf "%*s%*s%s%s\x1b[0m%*s", lpad, "", ipad, "", style, v, dpad, "" } { gsub(/\r$/, "") if (num_cols < NF) num_cols = NF for (i = 1; i <= NF; i++) { data[NR][i] = $i if (match_number($i)) { numbers[i]++ sums[i] += $i + 0 if (match_dot_digits($i)) { dd = RLENGTH if (dot_decs[i] < dd) dot_decs[i] = dd iw = RSTART - 1 if (int_widths[i] < iw) int_widths[i] = iw } else { w = length($i) if (int_widths[i] < w) int_widths[i] = w } continue } w = length($i) if (widths[i] < w) widths[i] = w } } END { # fix column-widths using the number-padding info, and the column-totals for (i = 1; i <= num_cols; i++) { if (numbers[i] > 0) { decs = dot_decs[i] > 0 ? dot_decs[i] - 1 : 0 w = length(sprintf("%.*f", decs, sums[i])) } else { w = 1 } if (widths[i] < w) widths[i] = w w = int_widths[i] + dot_decs[i] if (widths[i] < w) widths[i] = w } for (i = 1; i <= NR; i++) { last = length(data[i]) show_tiles(i, last) printf " " for (j = 1; j <= last; j++) { if (j > 1) printf " " # put 2-space gaps between columns v = data[i][j] if (!match_number(v)) { # avoid adding trailing spaces at the end of lines printf "%*s", (j == last) ? 0 : -widths[j], v continue } show_number(v, j, last, length(v)) } printf "\n" } # show extra row with the column-sums if (num_cols > 0) printf "%*s", num_cols, "" for (i = 1; i <= num_cols; i++) { printf " " if (numbers[i] > 0) { decs = dot_decs[i] > 0 ? dot_decs[i] - 1 : 0 s = sprintf("%.*f", decs, sums[i]) show_number(s, i, num_cols, length(s)) } else { printf "%*s", -widths[i], "-" } } printf "\n" }' "$@"