File: nn-compat.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 # This version is compatible with busybox/alpine-linux.
  53 
  54 case "$1" in
  55     -h|--h|-help|--help)
  56         awk '/^# +nn /, /^$/ { gsub(/^# ?/, ""); print }' "$0"
  57         exit 0
  58     ;;
  59 esac
  60 
  61 name="${1:-gray}"
  62 [ $# -gt 0 ] && shift
  63 
  64 [ "$1" = '--' ] && shift
  65 
  66 command='awk'
  67 if { [ -p /dev/stdout ] || [ -t 1 ]; } && [ -e /usr/bin/stdbuf ]; then
  68     command='stdbuf -oL awk'
  69 fi
  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}" > /dev/stderr
  79         failed=1
  80     fi
  81 done
  82 
  83 if [ "${failed}" -gt 0 ]; then
  84     exit 2
  85 fi
  86 
  87 ${command} -v name="${name}" '
  88     BEGIN {
  89         orig = name
  90         gsub(/^-{1,2}/, "", name)
  91 
  92         # alias-lookup table
  93         a["r"] = "red"
  94         a["g"] = "green"
  95         a["b"] = "blue"
  96         a["o"] = "orange"
  97         a["p"] = "purple"
  98         a["m"] = "magenta"
  99         a["h"] = "invert"
 100         a["i"] = "invert"
 101         a["u"] = "underline"
 102         a["or"] = "orange"
 103         a["ma"] = "magenta"
 104         a["hi"] = "invert"
 105         a["in"] = "invert"
 106         a["un"] = "underline"
 107         a["inv"] = "invert"
 108         a["mag"] = "magenta"
 109         a["grey"] = "gray"
 110         a["inverse"] = "invert"
 111         a["inverted"] = "invert"
 112         a["hilite"] = "invert"
 113         a["hilited"] = "invert"
 114         a["highlight"] = "invert"
 115         a["highlighted"] = "invert"
 116         a["underlined"] = "underline"
 117         a["bb"] = "blueback"
 118         a["gb"] = "greenback"
 119         a["mb"] = "magentaback"
 120         a["ob"] = "orangeback"
 121         a["pb"] = "purpleback"
 122         a["rb"] = "redback"
 123         a["bg"] = "greenback"
 124         a["bm"] = "magentaback"
 125         a["bo"] = "orangeback"
 126         a["bp"] = "purpleback"
 127         a["br"] = "redback"
 128         a["bluebg"] = "blueback"
 129         a["graybg"] = "grayback"
 130         a["greenbg"] = "greenback"
 131         a["greyback"] = "grayback"
 132         a["greybg"] = "grayback"
 133         a["magentabg"] = "magentaback"
 134         a["orangebg"] = "orangeback"
 135         a["purplebg"] = "purpleback"
 136         a["redbg"] = "redback"
 137         a["magback"] = "magentaback"
 138         a["magbg"] = "magentaback"
 139         a["orback"] = "orangeback"
 140         a["orbg"] = "orangeback"
 141         a["purback"] = "purpleback"
 142         a["purbg"] = "purpleback"
 143 
 144         # style-lookup table
 145         s["red"] = "\x1b[38;2;204;0;0m"
 146         s["green"] = "\x1b[38;2;0;135;95m"
 147         s["blue"] = "\x1b[38;2;0;95;215m"
 148         s["orange"] = "\x1b[38;2;215;95;0m"
 149         s["purple"] = "\x1b[38;2;135;95;255m"
 150         s["magenta"] = "\x1b[38;2;215;0;255m"
 151         s["gray"] = "\x1b[38;2;168;168;168m"
 152         s["bold"] = "\x1b[1m"
 153         s["invert"] = "\x1b[7m"
 154         s["italic"] = "\x1b[3m"
 155         s["underline"] = "\x1b[4m"
 156         s["blueback"] = "\x1b[48;2;0;95;215m\x1b[38;2;238;238;238m"
 157         s["grayback"] = "\x1b[48;2;168;168;168m\x1b[38;2;238;238;238m"
 158         s["greenback"] = "\x1b[48;2;0;135;95m\x1b[38;2;238;238;238m"
 159         s["magentaback"] = "\x1b[48;2;215;0;255m\x1b[38;2;238;238;238m"
 160         s["orangeback"] = "\x1b[48;2;215;95;0m\x1b[38;2;238;238;238m"
 161         s["purpleback"] = "\x1b[48;2;135;95;255m\x1b[38;2;238;238;238m"
 162         s["redback"] = "\x1b[48;2;204;0;0m\x1b[38;2;238;238;238m"
 163 
 164         # resolve aliases
 165         if (a[name] != "") name = a[name]
 166 
 167         # handle unsupported style-names with an error
 168         if (s[name] == "") {
 169             fmt = "\x1b[31munsupported style/color name `%s`\x1b[0m\n"
 170             printf fmt, orig > "/dev/stderr"
 171             exit 1
 172         }
 173 
 174         # match ANSI-code to the name
 175         style = s[name]
 176     }
 177 
 178     # (re)style all long-enough digit-runs/numbers in lines
 179     {
 180         line = $0
 181 
 182         # ignore screen/buffer-switching ANSI-codes, to avoid corrupting them
 183         gsub(/\x1b\[([0-9]{4,}[A-Za-z])/, "", line)
 184 
 185         # restyle digit-runs which are longer than 3
 186         while (match(line, /[0-9]{4,}/)) {
 187             # give up on digit-runs which are unusually long, to mitigate the
 188             # quadratic time-complexity of slicing strings in a loop; working
 189             # on lines with long-enough numbers is still quadratic, though
 190             if (RLENGTH > 21) {
 191                 printf "%s", substr(line, 1, RSTART + RLENGTH)
 192                 line = substr(line, RSTART + RLENGTH)
 193                 continue
 194             }
 195 
 196             len = RLENGTH
 197             lead = len % 3
 198             alt = lead > 0
 199 
 200             printf "%s", substr(line, 1, RSTART + lead - 1)
 201             line = substr(line, RSTART + lead)
 202             len -= lead
 203 
 204             while (len > 0) {
 205                 if (alt == 1) {
 206                     printf "%s%s\x1b[0m", style, substr(line, 1, 3)
 207                 } else {
 208                     printf "%s", substr(line, 1, 3)
 209                 }
 210 
 211                 alt = 1 - alt
 212                 line = substr(line, 4)
 213                 len -= 3
 214             }
 215         }
 216 
 217         print line
 218     }
 219 ' "$@"