#!/bin/sh # The MIT License (MIT) # # Copyright © 2020-2025 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. # ntsv [options...] [filenames...] # # # Nice Tab Separated Values realigns and styles data tables using ANSI color # sequences. When not given filepaths to read data from, this tool reads from # standard input. # # If you're using Linux or MacOS, you may also find this command useful: # # # View Nice Table(s) / Very Nice Table(s); uses my scripts `ntsv` and `nn` # vnt() { # awk 1 "$@" | nl -b a -w 1 -v 0 | ntsv | nn | # awk '(NR - 1) % 5 == 1 { print "" } 1' | less -MKiCRS # } # # If `less` supports the `header` option, an even more useful command is: # # # View Nice Table(s) / Very Nice Table(s); uses my scripts `ntsv` and `nn` # vnt() { # awk 1 "$@" | nl -b a -w 1 -v 0 | ntsv | nn | # awk '(NR - 1) % 5 == 1 { print "" } 1' | less -MKiCRS --header=1 # } case "$1" in -h|--h|-help|--help) awk '/^# +ntsv /, /^$/ { gsub(/^# ?/, ""); print }' "$0" exit 0 ;; esac [ "$1" = "--" ] && shift # show all non-existing files given failed=0 for arg in "$@"; do if [ "${arg}" = "-" ]; then continue fi if [ ! -e "${arg}" ]; then printf "no file named \"%s\"\n" "${arg}" > /dev/stderr failed=1 fi done if [ "${failed}" -gt 0 ]; then exit 2 fi awk -F "\t" ' function match_number(v) { return match(v, /^[+-]?[0-9]+(\.[0-9]+)?$/) } function match_dot_digits(v) { return match(v, /\.[0-9]+$/) } # show_tiles uses `pad` and `decs` as local variables function show_tiles(i, l, j, v, pad, decs) { for (j = 1; j <= l; j++) { v = data[i][j] if (v == "") { printf "%s", "\x1b[0m○" continue } if (!match_number(v)) { pad = v ~ /^ | $/ v = pad ? "\x1b[38;2;196;160;0m■" : "\x1b[38;2;128;128;128m■" printf "%s", v continue } if (v > 0) { decs = match_dot_digits(v) v = decs ? "\x1b[38;2;0;135;95m■" : "\x1b[38;2;0;95;0m■" printf "%s", v continue } if (v < 0) { decs = match_dot_digits(v) v = decs ? "\x1b[38;2;215;95;95m■" : "\x1b[38;2;204;0;0m■" printf "%s", v continue } printf "%s", "\x1b[38;2;0;95;215m■" } printf "\x1b[0m" for (j = l + 1; j <= num_cols; j++) { printf "%s", "×" } printf "\x1b[0m" } # show_number uses `dd`, `iw`, `dpad`, `ipad`, `lpad`, `style`, and `decs` # as local variables function show_number(v, j, last, w, dd, iw, dpad, ipad, lpad, style, decs) { 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) { decs = dot_decs[j] > 0 style = decs ? "\x1b[38;2;0;135;95m" : "\x1b[38;2;0;95;0m" } else if (v < 0) { decs = dot_decs[j] > 0 style = decs ? "\x1b[38;2;215;95;95m" : "\x1b[38;2;204;0;0m" } else { style = "\x1b[38;2;0;95;215m" } 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++) { v = data[i][j] # put 2-space gaps between columns if (j > 1 && j < last) printf " " else if (j == last && v != "") printf " " 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], "-" } } if (num_cols > 0) printf "\n" } ' "$@"