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