File: nn.sh
   1 #!/bin/sh
   2 
   3 # The MIT License (MIT)
   4 #
   5 # Copyright (c) 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 # nn [option...] [files...]
  27 #
  28 #
  29 # Nice Numbers restyles all runs of 4+ digits by alternating ANSI-styles
  30 # every 3-digit group, so long numbers become easier to read at a glance.
  31 #
  32 # All (optional) leading options start with either single or double-dash,
  33 # and most of them change the style/color used. Some of the options are,
  34 # shown in their single-dash form:
  35 #
  36 #     -h, -help         show this help message
  37 #
  38 #     -inv, -inverse              use a highlighting/inverse style
  39 #     -hi, -hilite, -highlight    use a highlighting/inverse style
  40 #
  41 #     -b, -blue         use a blue color
  42 #         -bold         bold-style digits
  43 #     -g, -green        use a green color
  44 #         -gray         use a gray color (default)
  45 #     -m, -magenta      use a magenta color
  46 #     -o, -orange       use an orange color
  47 #     -p, -purple       use a purple color
  48 #     -r, -red          use a red color
  49 #     -u, -underline    underline digits
  50 
  51 
  52 buffered=0
  53 
  54 case "$1" in
  55     -b|--b|-buffered|--buffered)
  56         buffered=1
  57         shift
  58     ;;
  59 
  60     -h|--h|-help|--help)
  61         awk '/^# +nn /, /^$/ { gsub(/^# ?/, ""); print }' "$0"
  62         exit 0
  63     ;;
  64 esac
  65 
  66 name="${1:-gray}"
  67 [ $# -gt 0 ] && shift
  68 
  69 [ "$1" = '--' ] && shift
  70 
  71 # show all non-existing files given
  72 failed=0
  73 for arg in "$@"; do
  74     if [ "${arg}" = "-" ]; then
  75         continue
  76     fi
  77     if [ ! -e "${arg}" ]; then
  78         printf "no file named \"%s\"\n" "${arg}" >&2
  79         failed=1
  80     fi
  81 done
  82 
  83 if [ "${failed}" -gt 0 ]; then
  84     exit 2
  85 fi
  86 
  87 flush=0
  88 if [ "${buffered}" -eq 0 ] && { [ -p /dev/stdout ] || [ -t 1 ]; }; then
  89     flush=1
  90 fi
  91 
  92 awk -v flush="${flush}" -v name="${name}" '
  93     BEGIN {
  94         orig = name
  95         gsub(/^-{1,2}/, "", name)
  96 
  97         # alias-lookup table
  98         a["r"] = "red"
  99         a["g"] = "green"
 100         a["b"] = "blue"
 101         a["o"] = "orange"
 102         a["p"] = "purple"
 103         a["m"] = "magenta"
 104         a["h"] = "invert"
 105         a["i"] = "invert"
 106         a["u"] = "underline"
 107         a["or"] = "orange"
 108         a["ma"] = "magenta"
 109         a["hi"] = "invert"
 110         a["in"] = "invert"
 111         a["un"] = "underline"
 112         a["inv"] = "invert"
 113         a["mag"] = "magenta"
 114         a["grey"] = "gray"
 115         a["inverse"] = "invert"
 116         a["inverted"] = "invert"
 117         a["hilite"] = "invert"
 118         a["hilited"] = "invert"
 119         a["highlight"] = "invert"
 120         a["highlighted"] = "invert"
 121         a["underlined"] = "underline"
 122         a["bb"] = "blueback"
 123         a["gb"] = "greenback"
 124         a["mb"] = "magentaback"
 125         a["ob"] = "orangeback"
 126         a["pb"] = "purpleback"
 127         a["rb"] = "redback"
 128         a["bg"] = "greenback"
 129         a["bm"] = "magentaback"
 130         a["bo"] = "orangeback"
 131         a["bp"] = "purpleback"
 132         a["br"] = "redback"
 133         a["bluebg"] = "blueback"
 134         a["graybg"] = "grayback"
 135         a["greenbg"] = "greenback"
 136         a["greyback"] = "grayback"
 137         a["greybg"] = "grayback"
 138         a["magentabg"] = "magentaback"
 139         a["orangebg"] = "orangeback"
 140         a["purplebg"] = "purpleback"
 141         a["redbg"] = "redback"
 142         a["magback"] = "magentaback"
 143         a["magbg"] = "magentaback"
 144         a["orback"] = "orangeback"
 145         a["orbg"] = "orangeback"
 146         a["purback"] = "purpleback"
 147         a["purbg"] = "purpleback"
 148 
 149         # style-lookup table
 150         s["red"] = "\x1b[38;2;204;0;0m"
 151         s["green"] = "\x1b[38;2;0;135;95m"
 152         s["blue"] = "\x1b[38;2;0;95;215m"
 153         s["orange"] = "\x1b[38;2;215;95;0m"
 154         s["purple"] = "\x1b[38;2;135;95;255m"
 155         s["magenta"] = "\x1b[38;2;215;0;255m"
 156         s["gray"] = "\x1b[38;2;168;168;168m"
 157         s["bold"] = "\x1b[1m"
 158         s["invert"] = "\x1b[7m"
 159         s["italic"] = "\x1b[3m"
 160         s["underline"] = "\x1b[4m"
 161         s["blueback"] = "\x1b[48;2;0;95;215m\x1b[38;2;238;238;238m"
 162         s["grayback"] = "\x1b[48;2;168;168;168m\x1b[38;2;238;238;238m"
 163         s["greenback"] = "\x1b[48;2;0;135;95m\x1b[38;2;238;238;238m"
 164         s["magentaback"] = "\x1b[48;2;215;0;255m\x1b[38;2;238;238;238m"
 165         s["orangeback"] = "\x1b[48;2;215;95;0m\x1b[38;2;238;238;238m"
 166         s["purpleback"] = "\x1b[48;2;135;95;255m\x1b[38;2;238;238;238m"
 167         s["redback"] = "\x1b[48;2;204;0;0m\x1b[38;2;238;238;238m"
 168 
 169         # resolve aliases
 170         if (a[name] != "") name = a[name]
 171 
 172         # handle unsupported style-names with an error
 173         if (s[name] == "") {
 174             printf "unsupported style/color name `%s`\n", orig > "/dev/stderr"
 175             exit 1
 176         }
 177 
 178         # match ANSI-code to the name
 179         style = s[name]
 180 
 181         gensub(/./, "", "g", "") # fail before output, if gensub not available
 182         r18 = sprintf("\\1%s\\2\x1b[0m\\3%s\\4\x1b[0m\\5%s\\6\x1b[0m\\7", style, style, style, style)
 183         r15 = sprintf("\\1%s\\2\x1b[0m\\3%s\\4\x1b[0m\\5%s\\6\x1b[0m", style, style, style)
 184         r12 = sprintf("\\1%s\\2\x1b[0m\\3%s\\4\x1b[0m\\5", style, style)
 185         r9 = sprintf("\\1%s\\2\x1b[0m\\3%s\\4\x1b[0m", style, style)
 186         r6 = sprintf("\\1%s\\2\x1b[0m\\3", style)
 187         r3 = sprintf("\\1%s\\2\x1b[0m", style)
 188     }
 189 
 190     # (re)style all long-enough digit-runs/numbers in lines
 191     {
 192         s = $0
 193 
 194         # ignore screen/buffer-switching ANSI-codes, to avoid corrupting them
 195         gsub(/\x1b\[([0-9]{4,}[A-Za-z])/, "", s)
 196 
 197         # give up on restyling numbers on lines with any very long numbers, to
 198         # avoid any misleading restyling
 199         if (s ~ /[0-9]{22,}/) {
 200             print
 201             if (flush) fflush()
 202             next
 203         }
 204 
 205         s = gensub(/([0-9]{1,3})([0-9]{3})([0-9]{3})([0-9]{3})([0-9]{3})([0-9]{3})([0-9]{3})/, r18, "g", s)
 206         s = gensub(/([0-9]{1,3})([0-9]{3})([0-9]{3})([0-9]{3})([0-9]{3})([0-9]{3})/, r15, "g", s)
 207         s = gensub(/([0-9]{1,3})([0-9]{3})([0-9]{3})([0-9]{3})([0-9]{3})/, r12, "g", s)
 208         s = gensub(/([0-9]{1,3})([0-9]{3})([0-9]{3})([0-9]{3})/, r9, "g", s)
 209         s = gensub(/([0-9]{1,3})([0-9]{3})([0-9]{3})/, r6, "g", s)
 210         s = gensub(/([0-9]{1,3})([0-9]{3})/, r3, "g", s)
 211         print s
 212         if (flush) fflush()
 213     }
 214 ' "$@"