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