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