File: nn.sh 1 #!/bin/sh 2 3 # The MIT License (MIT) 4 # 5 # Copyright © 2020-2025 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 show this help message 37 # -help show this help message 38 # 39 # -b use a blue color 40 # -blue use a blue color 41 # -bold bold-style digits 42 # -g use a green color 43 # -gray use a gray color (default) 44 # -green use a green color 45 # -hi use a highlighting/inverse style 46 # -highlight use a highlighting/inverse style 47 # -hilite use a highlighting/inverse style 48 # -inverse use a highlighting/inverse style 49 # -m use a magenta color 50 # -magenta use a magenta color 51 # -o use an orange color 52 # -orange use an orange color 53 # -p use a purple color 54 # -purple use a purple color 55 # -r use a red color 56 # -red use a red color 57 # -u underline digits 58 # -underline underline digits 59 60 61 case "$1" in 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 # general case to handle actual styles 72 awk -v name="${name}" ' 73 BEGIN { 74 orig = name 75 gsub(/^-{1,2}/, "", name) 76 77 # alias-lookup table 78 a["r"] = "red" 79 a["g"] = "green" 80 a["b"] = "blue" 81 a["o"] = "orange" 82 a["p"] = "purple" 83 a["m"] = "magenta" 84 a["h"] = "invert" 85 a["i"] = "invert" 86 a["u"] = "underline" 87 a["or"] = "orange" 88 a["ma"] = "magenta" 89 a["hi"] = "invert" 90 a["in"] = "invert" 91 a["un"] = "underline" 92 a["inv"] = "invert" 93 a["mag"] = "magenta" 94 a["grey"] = "gray" 95 a["inverse"] = "invert" 96 a["inverted"] = "invert" 97 a["hilite"] = "invert" 98 a["hilited"] = "invert" 99 a["highlight"] = "invert" 100 a["highlighted"] = "invert" 101 a["underlined"] = "underline" 102 a["bb"] = "blueback" 103 a["gb"] = "greenback" 104 a["mb"] = "magentaback" 105 a["ob"] = "orangeback" 106 a["pb"] = "purpleback" 107 a["rb"] = "redback" 108 a["bg"] = "greenback" 109 a["bm"] = "magentaback" 110 a["bo"] = "orangeback" 111 a["bp"] = "purpleback" 112 a["br"] = "redback" 113 a["bluebg"] = "blueback" 114 a["graybg"] = "grayback" 115 a["greenbg"] = "greenback" 116 a["greyback"] = "grayback" 117 a["greybg"] = "grayback" 118 a["magentabg"] = "magentaback" 119 a["orangebg"] = "orangeback" 120 a["purplebg"] = "purpleback" 121 a["redbg"] = "redback" 122 a["magback"] = "magentaback" 123 a["magbg"] = "magentaback" 124 a["orback"] = "orangeback" 125 a["orbg"] = "orangeback" 126 a["purback"] = "purpleback" 127 a["purbg"] = "purpleback" 128 129 # style-lookup table 130 s["red"] = "\x1b[38;2;204;0;0m" 131 s["green"] = "\x1b[38;2;0;135;95m" 132 s["blue"] = "\x1b[38;2;0;95;215m" 133 s["orange"] = "\x1b[38;2;215;95;0m" 134 s["purple"] = "\x1b[38;2;135;95;255m" 135 s["magenta"] = "\x1b[38;2;215;0;255m" 136 s["gray"] = "\x1b[38;2;168;168;168m" 137 s["bold"] = "\x1b[1m" 138 s["invert"] = "\x1b[7m" 139 s["italic"] = "\x1b[3m" 140 s["underline"] = "\x1b[4m" 141 s["blueback"] = "\x1b[48;2;0;95;215m\x1b[38;2;238;238;238m" 142 s["grayback"] = "\x1b[48;2;168;168;168m\x1b[38;2;238;238;238m" 143 s["greenback"] = "\x1b[48;2;0;135;95m\x1b[38;2;238;238;238m" 144 s["magentaback"] = "\x1b[48;2;215;0;255m\x1b[38;2;238;238;238m" 145 s["orangeback"] = "\x1b[48;2;215;95;0m\x1b[38;2;238;238;238m" 146 s["purpleback"] = "\x1b[48;2;135;95;255m\x1b[38;2;238;238;238m" 147 s["redback"] = "\x1b[48;2;204;0;0m\x1b[38;2;238;238;238m" 148 149 # resolve aliases 150 if (a[name] != "") name = a[name] 151 152 # handle unsupported style-names with an error 153 if (s[name] == "") { 154 fmt = "\x1b[31munsupported style/color name `%s`\x1b[0m\n" 155 printf fmt, orig > "/dev/stderr" 156 exit 1 157 } 158 159 # match ANSI-code to the name 160 style = s[name] 161 } 162 163 # (re)style all long-enough digit-runs/numbers in lines 164 { 165 # ignore screen/buffer-switching ANSI-codes, to avoid corrupting them 166 gsub(/\x1b\[([0-9]{4,}[A-Za-z])/, "") 167 168 line = $0 169 170 # restyle digits-runs which are longer than 3 171 while (match(line, /[0-9]{4,}/)) { 172 # give up on digit-runs which are unusually long, to mitigate the 173 # quadratic time-complexity of slicing strings in a loop 174 if (RLENGTH > 1000) { 175 printf "%s", substr(line, 1, RSTART + RLENGTH) 176 line = substr(line, RSTART + RLENGTH) 177 continue 178 } 179 180 len = RLENGTH 181 lead = len % 3 182 alt = lead > 0 183 184 printf "%s", substr(line, 1, RSTART + lead - 1) 185 line = substr(line, RSTART + lead) 186 len -= lead 187 188 while (len > 0) { 189 if (alt == 1) { 190 printf "%s%s\x1b[0m", style, substr(line, 1, 3) 191 } else { 192 printf "%s", substr(line, 1, 3) 193 } 194 195 alt = 1 - alt 196 line = substr(line, 4) 197 len -= 3 198 } 199 } 200 201 printf "%s\n", line; fflush() 202 } 203 ' "$@"