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 name='-gray'
  54 
  55 while [ $# -gt 0 ]; do
  56     case "$1" in
  57         -b|--b|-buffered|--buffered) buffered=1; shift; continue ;;
  58 
  59         -h|--h|-help|--help)
  60             awk '/^# +nn /, /^$/ { gsub(/^# ?/, ""); print }' "$0"
  61             exit 0
  62         ;;
  63 
  64         --) shift; break ;;
  65 
  66         -*) name="$1"; shift; continue ;;
  67     esac
  68 
  69     break
  70 done
  71 
  72 # show all non-existing files given
  73 failed=0
  74 for arg in "$@"; do
  75     [ "${arg}" = "-" ] && continue
  76     [ -e "${arg}" ] && continue
  77     printf "%s: no file named \"%s\"\n" "$0" "${arg}" >&2
  78     failed=1
  79 done
  80 
  81 [ "${failed}" -gt 0 ] && exit 2
  82 
  83 flush=0
  84 if [ "${buffered}" -eq 0 ] && { [ -p /dev/stdout ] || [ -t 1 ]; }; then
  85     flush=1
  86 fi
  87 
  88 src='
  89 BEGIN {
  90     orig = name
  91     gsub(/^-{1,2}/, "", name)
  92 
  93     # alias-lookup table
  94     a["r"] = "red"
  95     a["g"] = "green"
  96     a["b"] = "blue"
  97     a["o"] = "orange"
  98     a["p"] = "purple"
  99     a["m"] = "magenta"
 100     a["h"] = "invert"
 101     a["i"] = "invert"
 102     a["u"] = "underline"
 103     a["or"] = "orange"
 104     a["ma"] = "magenta"
 105     a["hi"] = "invert"
 106     a["in"] = "invert"
 107     a["un"] = "underline"
 108     a["inv"] = "invert"
 109     a["mag"] = "magenta"
 110     a["grey"] = "gray"
 111     a["inverse"] = "invert"
 112     a["inverted"] = "invert"
 113     a["hilite"] = "invert"
 114     a["hilited"] = "invert"
 115     a["highlight"] = "invert"
 116     a["highlighted"] = "invert"
 117     a["underlined"] = "underline"
 118     a["bb"] = "blueback"
 119     a["gb"] = "greenback"
 120     a["mb"] = "magentaback"
 121     a["ob"] = "orangeback"
 122     a["pb"] = "purpleback"
 123     a["rb"] = "redback"
 124     a["bg"] = "greenback"
 125     a["bm"] = "magentaback"
 126     a["bo"] = "orangeback"
 127     a["bp"] = "purpleback"
 128     a["br"] = "redback"
 129     a["bluebg"] = "blueback"
 130     a["graybg"] = "grayback"
 131     a["greenbg"] = "greenback"
 132     a["greyback"] = "grayback"
 133     a["greybg"] = "grayback"
 134     a["magentabg"] = "magentaback"
 135     a["orangebg"] = "orangeback"
 136     a["purplebg"] = "purpleback"
 137     a["redbg"] = "redback"
 138     a["magback"] = "magentaback"
 139     a["magbg"] = "magentaback"
 140     a["orback"] = "orangeback"
 141     a["orbg"] = "orangeback"
 142     a["purback"] = "purpleback"
 143     a["purbg"] = "purpleback"
 144 
 145     # style-lookup table
 146     s["red"] = "\x1b[38;2;204;0;0m"
 147     s["green"] = "\x1b[38;2;0;135;95m"
 148     s["blue"] = "\x1b[38;2;0;95;215m"
 149     s["orange"] = "\x1b[38;2;215;95;0m"
 150     s["purple"] = "\x1b[38;2;135;95;255m"
 151     s["magenta"] = "\x1b[38;2;215;0;255m"
 152     s["gray"] = "\x1b[38;2;168;168;168m"
 153     s["bold"] = "\x1b[1m"
 154     s["invert"] = "\x1b[7m"
 155     s["italic"] = "\x1b[3m"
 156     s["underline"] = "\x1b[4m"
 157     s["blueback"] = "\x1b[48;2;0;95;215m\x1b[38;2;238;238;238m"
 158     s["grayback"] = "\x1b[48;2;168;168;168m\x1b[38;2;238;238;238m"
 159     s["greenback"] = "\x1b[48;2;0;135;95m\x1b[38;2;238;238;238m"
 160     s["magentaback"] = "\x1b[48;2;215;0;255m\x1b[38;2;238;238;238m"
 161     s["orangeback"] = "\x1b[48;2;215;95;0m\x1b[38;2;238;238;238m"
 162     s["purpleback"] = "\x1b[48;2;135;95;255m\x1b[38;2;238;238;238m"
 163     s["redback"] = "\x1b[48;2;204;0;0m\x1b[38;2;238;238;238m"
 164 
 165     # resolve aliases
 166     if (a[name] != "") name = a[name]
 167 
 168     # handle unsupported style-names with an error
 169     if (s[name] == "") {
 170         printf "unsupported style/color name `%s`\n", orig > "/dev/stderr"
 171         exit 1
 172     }
 173 
 174     # match ANSI-code to the name
 175     style = s[name]
 176 
 177     # gensub(/./, "", "g", "") # fail before output, if gensub not available
 178     gensub(/([0-9])([0-9])/, "\\1\\2", "g", "12") # check for gensub support
 179 
 180     p = "([0-9]{1,3})"
 181     r = "\\1"
 182     too_long = "[0-9]{28,}"
 183 
 184     for (i = 2; i <= 9; i++) {
 185         p = p "([0-9]{3})"
 186         if ((i % 2) == 0) r = sprintf("%s%s\\%d\x1b[0m", r, style, i)
 187         else r = sprintf("%s\\%d", r, i)
 188 
 189         patterns[i] = p
 190         replacements[i] = r
 191     }
 192 }
 193 
 194 # (re)style all long-enough digit-runs/numbers in lines
 195 {
 196     l = $0
 197 
 198     # ignore screen/buffer-switching ANSI-codes, to avoid corrupting them
 199     gsub(/\x1b\[([0-9]{4,}[A-Za-z])/, "", l)
 200 
 201     # give up on restyling numbers on lines with any very long numbers, to
 202     # avoid any misleading restyling
 203     if (l !~ too_long) {
 204         for (i = 9; i >= 2; i--) {
 205             l = gensub(patterns[i], replacements[i], "g", l)
 206         }
 207     }
 208 
 209     print l
 210     if (flush) fflush()
 211 }
 212 '
 213 
 214 awk -v flush="${flush}" -v name="${name}" "${src}" "$@"