#!/usr/bin/python # The MIT License (MIT) # # Copyright (c) 2026 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. from io import SEEK_CUR from itertools import islice from re import compile, Match, Pattern, IGNORECASE from sys import argv, exit, stderr, stdin, stdout from typing import List, Tuple info = ''' coma [regex/style pairs...] COlor MAtches colors/styles regex matches everywhere they're found, using the named-style associated to the regex in its argument-pair. Lines not matching any regex stay verbatim. The colors/styles available are: blue blueback bold boldback gray grayback green greenback inverse magenta magentaback orange orangeback purple purpleback red redback underline Some style aliases are: b blue bb blueback g green gb greenback m magenta mb magentaback o orange ob orangeback p purple pb purpleback r red rb redback u underline hi inverse (highlight) ''' # ansi_re matches ANSI-style sequences, so they're only matched `around` ansi_re = compile('\x1b\\[[0-9;]*[A-Za-z]') def fail(msg, code: int = 1) -> None: 'Show the error message given, and quit the app right away.' print(f'\x1b[31m{msg}\x1b[0m', file=stderr) exit(code) def match(src: str, start: int, stop: int) -> Tuple[Match, str]: first = None style = '' for expr, st in pairs: m = expr.search(src, start, stop) if not m or m.start() == m.end(): continue if not first or m.start() < first.start(): first = m style = st return first, style def style_line(w, s: str, live: bool) -> None: # start is used outside the regex-match loop to handle trailing parts # in lines start = 0 # replace all regex-matches on the line by surrounding each matched # substring with ANSI styles/resets while True: m = ansi_re.search(s, start) if not m: start = style_chunk(w, s, start, len(s)) break stop = m.start() start = style_chunk(w, s, start, stop) # don't forget the last part of the line, or the whole line stop = m.end() w.write(s[start:stop]) start = stop # don't forget the last part of the line, or the whole line w.write(s[start:]) w.write('\n') if live: w.flush() def style_chunk(w, s: str, start: int, stop: int) -> int: while True: m, style = match(s, start, stop) if not m: return start i = m.start() j = m.end() # part before match w.write(s[start:i]) # current match w.write(style) w.write(s[i:j]) w.write('\x1b[0m') # the end of the match is the start of the `rest` of the string start = j # names_aliases normalizes lookup keys for table names2styles names_aliases = { 'b': 'blue', 'g': 'green', 'm': 'magenta', 'o': 'orange', 'p': 'purple', 'r': 'red', 'u': 'underline', 'bb': 'blueback', 'bg': 'greenback', 'bm': 'magentaback', 'bo': 'orangeback', 'bp': 'purpleback', 'br': 'redback', 'gb': 'greenback', 'mb': 'magentaback', 'ob': 'orangeback', 'pb': 'purpleback', 'rb': 'redback', 'hi': 'inverse', 'inv': 'inverse', 'mag': 'magenta', 'du': 'doubleunderline', 'flip': 'inverse', 'swap': 'inverse', 'reset': 'plain', 'highlight': 'inverse', 'hilite': 'inverse', 'invert': 'inverse', 'inverted': 'inverse', 'swapped': 'inverse', 'dunderline': 'doubleunderline', 'dunderlined': 'doubleunderline', 'strikethrough': 'strike', 'strikethru': 'strike', 'struck': 'strike', 'underlined': 'underline', 'bblue': 'blueback', 'bgray': 'grayback', 'bgreen': 'greenback', 'bmagenta': 'magentaback', 'borange': 'orangeback', 'bpurple': 'purpleback', 'bred': 'redback', 'bgblue': 'blueback', 'bggray': 'grayback', 'bggreen': 'greenback', 'bgmag': 'magentaback', 'bgmagenta': 'magentaback', 'bgorange': 'orangeback', 'bgpurple': 'purpleback', 'bgred': 'redback', 'bluebg': 'blueback', 'graybg': 'grayback', 'greenbg': 'greenback', 'magbg': 'magentaback', 'magentabg': 'magentaback', 'orangebg': 'orangeback', 'purplebg': 'purpleback', 'redbg': 'redback', 'backblue': 'blueback', 'backgray': 'grayback', 'backgreen': 'greenback', 'backmag': 'magentaback', 'backmagenta': 'magentaback', 'backorange': 'orangeback', 'backpurple': 'purpleback', 'backred': 'redback', } # names2styles matches color/style names to their ANSI-style strings names2styles = { 'blue': '\x1b[38;2;0;95;215m', 'bold': '\x1b[1m', 'doubleunderline': '\x1b[21m', 'gray': '\x1b[38;2;168;168;168m', 'green': '\x1b[38;2;0;135;95m', 'inverse': '\x1b[7m', 'magenta': '\x1b[38;2;215;0;255m', 'orange': '\x1b[38;2;215;95;0m', 'plain': '\x1b[0m', 'purple': '\x1b[38;2;135;95;255m', 'red': '\x1b[38;2;204;0;0m', 'strike': '\x1b[9m', 'underline': '\x1b[4m', 'blueback': '\x1b[48;2;0;95;215m\x1b[38;2;238;238;238m', 'grayback': '\x1b[48;2;168;168;168m\x1b[38;2;238;238;238m', 'greenback': '\x1b[48;2;0;135;95m\x1b[38;2;238;238;238m', 'magentaback': '\x1b[48;2;215;0;255m\x1b[38;2;238;238;238m', 'orangeback': '\x1b[48;2;215;95;0m\x1b[38;2;238;238;238m', 'purpleback': '\x1b[48;2;135;95;255m\x1b[38;2;238;238;238m', 'redback': '\x1b[48;2;204;0;0m\x1b[38;2;238;238;238m', } if len(argv) > 1 and argv[1] in ('-h', '--h', '-help', '--help'): print(info.strip('\n')) exit(0) flags = 0 args = argv[1:] if len(args) > 0 and args[0] in ('-i', '--i', '-ins', '--ins'): flags = IGNORECASE args = args[1:] if len(args) > 0 and args[0] == '--': args = args[1:] # ensure an even number of args, after any leading options if len(args) % 2 != 0: msg = 'expected an even number of args as regex/style pairs' fail(f'{msg}, but got {len(args)} instead') # make regex/ANSI-style pairs which are directly usable pairs: List[Tuple[Pattern, str]] = [] for src, name in zip(islice(args, 0, None, 2), islice(args, 1, None, 2)): try: expr = compile(src, flags=flags) except Exception as e: fail(e) if name in names_aliases: name = names_aliases[name] if not name in names2styles: fail(f'style named {name} not supported') pairs.append((expr, names2styles[name])) try: stdout.seek(0, SEEK_CUR) live = False except: live = True try: for line in stdin: line = line.rstrip('\r\n').rstrip('\n') style_line(stdout, line, live) except BrokenPipeError: # quit quietly, instead of showing a confusing error message pass except KeyboardInterrupt: exit(2) except Exception as e: fail(e, 1)