File: nn.py
   1 #!/usr/bin/python
   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 # Notes
  27 #
  28 # String-slicing was a major source of inefficiencies in this script, making
  29 # it viable only for small inputs; it's not clear what the stdlib offers to
  30 # loop over sub-strings without copying data, which is really needed in this
  31 # case.
  32 #
  33 # String-slicing seems to be amortized in more recent versions of Python.
  34 #
  35 # In the end the code has become much uglier by using explicit index-pairs,
  36 # which are used/updated all over to avoid copying sub-strings.
  37 
  38 
  39 from io import SEEK_CUR
  40 from sys import argv, exit, stderr, stdin, stdout
  41 
  42 
  43 info = '''
  44 nn [option...] [filepaths/URIs...]
  45 
  46 
  47 Nice Numbers restyles all runs of 4+ digits by alternating ANSI-styles
  48 every 3-digit group, so long numbers become easier to read at a glance.
  49 
  50 All (optional) leading options start with either single or double-dash,
  51 and most of them change the style/color used. Some of the options are,
  52 shown in their single-dash form:
  53 
  54     -h, -help    show this help message
  55 
  56     -b          use a blue color
  57     -blue       use a blue color
  58     -bold       bold-style digits
  59     -g          use a green color
  60     -gray       use a gray color (default)
  61     -green      use a green color
  62     -hi         use a highlighting/inverse style
  63     -highlight  use a highlighting/inverse style
  64     -hilite     use a highlighting/inverse style
  65     -inverse    use a highlighting/inverse style
  66     -m          use a magenta color
  67     -magenta    use a magenta color
  68     -o          use an orange color
  69     -orange     use an orange color
  70     -p          use a purple color
  71     -purple     use a purple color
  72     -r          use a red color
  73     -red        use a red color
  74     -u          underline digits
  75     -underline  underline digits
  76 '''
  77 
  78 # handle standard help cmd-line options, quitting right away in that case
  79 if len(argv) > 1 and argv[1] in ('-h', '--h', '-help', '--help'):
  80     print(info.strip())
  81     exit(0)
  82 
  83 
  84 # names_aliases normalizes lookup keys for table names2styles
  85 names_aliases = {
  86     'b': 'blue',
  87     'g': 'green',
  88     'm': 'magenta',
  89     'o': 'orange',
  90     'p': 'purple',
  91     'r': 'red',
  92     'u': 'underline',
  93 
  94     'bb': 'blueback',
  95     'bg': 'greenback',
  96     'bm': 'magentaback',
  97     'bo': 'orangeback',
  98     'bp': 'purpleback',
  99     'br': 'redback',
 100 
 101     'gb': 'greenback',
 102     'mb': 'magentaback',
 103     'ob': 'orangeback',
 104     'pb': 'purpleback',
 105     'rb': 'redback',
 106 
 107     'hi': 'inverse',
 108     'inv': 'inverse',
 109 
 110     'flip': 'inverse',
 111     'swap': 'inverse',
 112 
 113     'reset': 'plain',
 114     'highlight': 'inverse',
 115     'hilite': 'inverse',
 116     'invert': 'inverse',
 117     'inverted': 'inverse',
 118     'swapped': 'inverse',
 119 
 120     'under': 'underline',
 121 
 122     'bgblue': 'blueback',
 123     'bggray': 'grayback',
 124     'bggreen': 'greenback',
 125     'bgmagenta': 'magentaback',
 126     'bgorange': 'orangeback',
 127     'bgpurple': 'purpleback',
 128     'bgred': 'redback',
 129 
 130     'bluebg': 'blueback',
 131     'graybg': 'grayback',
 132     'greenbg': 'greenback',
 133     'magentabg': 'magentaback',
 134     'orangebg': 'orangeback',
 135     'purplebg': 'purpleback',
 136     'redbg': 'redback',
 137 
 138     'backblue': 'blueback',
 139     'backgray': 'grayback',
 140     'backgreen': 'greenback',
 141     'backmagenta': 'magentaback',
 142     'backorange': 'orangeback',
 143     'backpurple': 'purpleback',
 144     'backred': 'redback',
 145 }
 146 
 147 # names2styles matches color/style names to their ANSI-style strings
 148 names2styles = {
 149     'blue': '\x1b[38;2;0;95;215m',
 150     'bold': '\x1b[1m',
 151     'gray': '\x1b[38;2;168;168;168m',
 152     'green': '\x1b[38;2;0;135;95m',
 153     'inverse': '\x1b[7m',
 154     'magenta': '\x1b[38;2;215;0;255m',
 155     'orange': '\x1b[38;2;215;95;0m',
 156     'plain': '\x1b[0m',
 157     'purple': '\x1b[38;2;135;95;255m',
 158     'red': '\x1b[38;2;204;0;0m',
 159     'underline': '\x1b[4m',
 160 
 161     'blueback': '\x1b[48;2;0;95;215m\x1b[38;2;238;238;238m',
 162     'grayback': '\x1b[48;2;168;168;168m\x1b[38;2;238;238;238m',
 163     'greenback': '\x1b[48;2;0;135;95m\x1b[38;2;238;238;238m',
 164     'magentaback': '\x1b[48;2;215;0;255m\x1b[38;2;238;238;238m',
 165     'orangeback': '\x1b[48;2;215;95;0m\x1b[38;2;238;238;238m',
 166     'purpleback': '\x1b[48;2;135;95;255m\x1b[38;2;238;238;238m',
 167     'redback': '\x1b[48;2;204;0;0m\x1b[38;2;238;238;238m',
 168 }
 169 
 170 
 171 def restyle_line(w, line: str, style: str) -> None:
 172     'Alternate styles for runs of digits in the string given.'
 173 
 174     start = 0
 175     end = len(line)
 176     if end > 1 and line[end - 2] == '\r' and line[end - 1] == '\n':
 177         end -= 2
 178     elif end > 0 and line[end - 1] == '\n':
 179         end -= 1
 180 
 181     while True:
 182         # see if line is over
 183         if start >= end:
 184             w.write('\n')
 185             return
 186 
 187         # find where the next run of digits starts, if present
 188         i = -1
 189         for j in range(start, end):
 190             if line[j].isdigit():
 191                 i = j
 192                 break
 193 
 194         # check if rest of the line has no more digits
 195         if i < 0:
 196             w.write(line[start:end])
 197             w.write('\n')
 198             return
 199 
 200         # some ANSI-style sequences use 4-digit numbers, which are long
 201         # enough for this script to mangle
 202         is_ansi = i >= 2 and line[i-2] == '\x1b' and line[i-1] == '['
 203 
 204         # emit line up to right before the next run of digits starts
 205         w.write(line[start:i])
 206         start = i
 207 
 208         # find where/if the current run of digits ends
 209         i = -1
 210         for j in range(start, end):
 211             if not line[j].isdigit():
 212                 i = j
 213                 break
 214 
 215         # check if rest of the line has only digits in it
 216         if i < 0:
 217             if not is_ansi:
 218                 restyle_digits(w, line, start, end, style)
 219             else:
 220                 w.write(line[start:end])
 221             w.write('\n')
 222             return
 223 
 224         # emit digits using alternate styling, and advance past them
 225         if not is_ansi:
 226             restyle_digits(w, line, start, i, style)
 227         else:
 228             w.write(line[start:i])
 229         start = i
 230 
 231 
 232 def restyle_digits(w, digits: str, start: int, end: int, style: str) -> None:
 233     'Alternate styles on 3-item chunks from the string given.'
 234 
 235     diff = end - start
 236 
 237     # it's overall quicker to just emit short-enough digit-runs verbatim
 238     if diff < 4:
 239         w.write(digits[start:end])
 240         return
 241 
 242     # emit leading chunk of digits, which is the only one which
 243     # can have fewer than 3 items
 244     lead = diff % 3
 245     w.write(digits[start:start + lead])
 246 
 247     # the rest of the sub-string now has a multiple of 3 items left
 248     start += lead
 249 
 250     # start by styling the next digit-group only if there was a
 251     # non-empty leading group at the start of the full digit-run
 252     use_style = lead > 0
 253 
 254     # alternate styles until the string is over
 255     while start < end:
 256         # the digits left are always a multiple of 3
 257         stop = start + 3
 258 
 259         if use_style:
 260             w.write(style)
 261             w.write(digits[start:stop])
 262             w.write('\x1b[0m')
 263         else:
 264             w.write(digits[start:stop])
 265 
 266         # switch style and advance to the next 3-digit chunk
 267         use_style = not use_style
 268         start = stop
 269 
 270 
 271 def seems_url(s: str) -> bool:
 272     protocols = ('https://', 'http://', 'file://', 'ftp://', 'data:')
 273     return any(s.startswith(p) for p in protocols)
 274 
 275 
 276 def handle_lines(w, src, style: str, live: bool) -> None:
 277     for line in src:
 278         restyle_line(w, line, style)
 279         if live:
 280             w.flush()
 281 
 282 
 283 args = argv[1:]
 284 style = names2styles['gray']
 285 
 286 # handle leading style/color option, if present
 287 if len(args) > 0 and args[0].startswith('-'):
 288     s = args[0].lstrip('-')
 289     if s in names_aliases:
 290         s = names_aliases[s]
 291 
 292     if s in names2styles:
 293         style = names2styles[s]
 294         args = args[1:]
 295 
 296 if len(args) > 0 and args[0] == '--':
 297     args = args[1:]
 298 
 299 if args.count('-') > 1:
 300     msg = 'reading from `-` (standard input) more than once not allowed'
 301     print(msg, file=stderr)
 302     exit(1)
 303 
 304 if any(seems_url(e) for e in args):
 305     from io import TextIOWrapper
 306     from urllib.request import urlopen
 307 
 308 try:
 309     stdout.seek(0, SEEK_CUR)
 310     live = False
 311 except:
 312     live = True
 313 
 314 try:
 315     for path in args:
 316         if path == '-':
 317             handle_lines(stdout, stdin, style, live)
 318             continue
 319 
 320         if seems_url(path):
 321             with urlopen(path) as inp:
 322                 with TextIOWrapper(inp, encoding='utf-8') as txt:
 323                     handle_lines(stdout, txt, style, live)
 324             continue
 325 
 326         with open(path, encoding='utf-8') as inp:
 327             handle_lines(stdout, inp, style, live)
 328 
 329     if len(args) == 0:
 330         handle_lines(stdout, stdin, style, live)
 331 except BrokenPipeError:
 332     # quit quietly, instead of showing a confusing error message
 333     stderr.close()
 334     exit(0)
 335 except KeyboardInterrupt:
 336     exit(2)
 337 except Exception as e:
 338     print(str(e), file=stderr)
 339     exit(1)