#!/bin/sh # The MIT License (MIT) # # Copyright © 2020-2025 pacman64 # # Permission is hereby granted, free of charge, to any person obtaining a copy # of this software and associated documentation files (the “Software”), to deal # in the Software without restriction, including without limitation the rights # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell # copies of the Software, and to permit persons to whom the Software is # furnished to do so, subject to the following conditions: # # The above copyright notice and this permission notice shall be included in # all copies or substantial portions of the Software. # # THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE # SOFTWARE. # nn [option...] [files...] # # # Nice Numbers restyles all runs of 4+ digits by alternating ANSI-styles # every 3-digit group, so long numbers become easier to read at a glance. # # All (optional) leading options start with either single or double-dash, # and most of them change the style/color used. Some of the options are, # shown in their single-dash form: # # -h show this help message # -help show this help message # # -b use a blue color # -blue use a blue color # -bold bold-style digits # -g use a green color # -gray use a gray color (default) # -green use a green color # -hi use a highlighting/inverse style # -highlight use a highlighting/inverse style # -hilite use a highlighting/inverse style # -inverse use a highlighting/inverse style # -m use a magenta color # -magenta use a magenta color # -o use an orange color # -orange use an orange color # -p use a purple color # -purple use a purple color # -r use a red color # -red use a red color # -u underline digits # -underline underline digits # handle help options case "$1" in -h|--h|-help|--help) awk '/^# +nn /, /^$/ { gsub(/^# ?/, ""); print }' "$0" exit 0 ;; esac name="${1:-gray}" [ $# -gt 0 ] && shift # general case to handle actual styles awk -v name="${name}" ' BEGIN { orig = name gsub(/^-{1,2}/, "", name) # alias-lookup table a["r"] = "red" a["g"] = "green" a["b"] = "blue" a["o"] = "orange" a["p"] = "purple" a["m"] = "magenta" a["h"] = "invert" a["i"] = "invert" a["u"] = "underline" a["or"] = "orange" a["ma"] = "magenta" a["hi"] = "invert" a["in"] = "invert" a["un"] = "underline" a["inv"] = "invert" a["mag"] = "magenta" a["grey"] = "gray" a["inverse"] = "invert" a["inverted"] = "invert" a["hilite"] = "invert" a["hilited"] = "invert" a["highlight"] = "invert" a["highlighted"] = "invert" a["underlined"] = "underline" a["bb"] = "blueback" a["gb"] = "greenback" a["mb"] = "magentaback" a["ob"] = "orangeback" a["pb"] = "purpleback" a["br"] = "redback" a["bg"] = "greenback" a["bm"] = "magentaback" a["bo"] = "orangeback" a["bp"] = "purpleback" a["br"] = "redback" a["bluebg"] = "blueback" a["graybg"] = "grayback" a["greenbg"] = "greenback" a["greyback"] = "grayback" a["greybg"] = "grayback" a["magentabg"] = "magentaback" a["orangebg"] = "orangeback" a["purplebg"] = "purpleback" a["redbg"] = "redback" a["magback"] = "magentaback" a["magbg"] = "magentaback" a["orback"] = "orangeback" a["orbg"] = "orangeback" a["purback"] = "purpleback" a["purbg"] = "purpleback" # style-lookup table s["red"] = "\x1b[38;2;204;0;0m" s["green"] = "\x1b[38;2;0;135;95m" s["blue"] = "\x1b[38;2;0;95;215m" s["orange"] = "\x1b[38;2;215;95;0m" s["purple"] = "\x1b[38;2;135;95;255m" s["magenta"] = "\x1b[38;2;215;0;255m" s["gray"] = "\x1b[38;2;168;168;168m" s["bold"] = "\x1b[1m" s["invert"] = "\x1b[7m" s["italic"] = "\x1b[3m" s["underline"] = "\x1b[4m" s["blueback"] = "\x1b[48;2;0;95;215m\x1b[38;2;238;238;238m" s["grayback"] = "\x1b[48;2;168;168;168m\x1b[38;2;238;238;238m" s["greenback"] = "\x1b[48;2;0;135;95m\x1b[38;2;238;238;238m" s["magentaback"] = "\x1b[48;2;215;0;255m\x1b[38;2;238;238;238m" s["orangeback"] = "\x1b[48;2;215;95;0m\x1b[38;2;238;238;238m" s["purpleback"] = "\x1b[48;2;135;95;255m\x1b[38;2;238;238;238m" s["redback"] = "\x1b[48;2;204;0;0m\x1b[38;2;238;238;238m" # resolve aliases if (a[name] != "") name = a[name] # handle unsupported style-names with an error if (s[name] == "") { fmt = "\x1b[31munsupported style/color name `%s`\x1b[0m\n" printf fmt, orig > "/dev/stderr" exit 1 } # match ANSI-code to the name style = s[name] } # (re)style all long-enough digit-runs/numbers in lines { # ignore screen/buffer-switching ANSI-codes, to avoid corrupting them gsub(/\x1b\[([0-9]{4,}[A-Za-z])/, "") line = $0 # restyle digits-runs which are longer than 3 while (match(line, /[0-9]{4,}/)) { # give up on digit-runs which are unusually long, to mitigate the # quadratic time-complexity of slicing strings in a loop if (RLENGTH > 1000) { printf "%s", substr(line, 1, RSTART + RLENGTH) line = substr(line, RSTART + RLENGTH) continue } len = RLENGTH lead = len % 3 alt = lead > 0 printf "%s", substr(line, 1, RSTART + lead - 1) line = substr(line, RSTART + lead) len -= lead while (len > 0) { if (alt == 1) { printf "%s%s\x1b[0m", style, substr(line, 1, 3) } else { printf "%s", substr(line, 1, 3) } alt = 1 - alt line = substr(line, 4) len -= 3 } } printf "%s\n", line; fflush() } ' "$@"