github.phpd.cn/thought-machine/please@v12.2.0+incompatible/build_defs/ansi_escapes.build_defs (about) 1 def replace_ansi_escapes(name:str, srcs:list, deps:list=None, visibility:list=None): 2 """Replaces ANSI escape sequences in a source file. 3 4 In the file they're written as pseudo-shell variables like ${BOLD_WHITE}, 5 which is a lot easier to read than \\x1b[37;1m. 6 7 Does not currently support passing arbitrary rules to `srcs`. For various minor 8 reasons (mostly involving Go coverage/tracebacks) it would not be ideal to 9 rename them too much anyway. 10 """ 11 # Backslashes are deliberately repeated many times to get through all the levels of escaping needed. 12 REPLACEMENTS = { 13 'BOLD': '\\\\x1b[1m', 14 'BOLD_GREY': '\\\\x1b[30;1m', 15 'BOLD_RED': '\\\\x1b[31;1m', 16 'BOLD_GREEN': '\\\\x1b[32;1m', 17 'BOLD_YELLOW': '\\\\x1b[33;1m', 18 'BOLD_BLUE': '\\\\x1b[34;1m', 19 'BOLD_MAGENTA': '\\\\x1b[35;1m', 20 'BOLD_CYAN': '\\\\x1b[36;1m', 21 'BOLD_WHITE': '\\\\x1b[37;1m', 22 'GREY': '\\\\x1b[30m', 23 'RED': '\\\\x1b[31m', 24 'GREEN': '\\\\x1b[32m', 25 'YELLOW': '\\\\x1b[33m', 26 'BLUE': '\\\\x1b[34m', 27 'MAGENTA': '\\\\x1b[35m', 28 'CYAN': '\\\\x1b[36m', 29 'WHITE': '\\\\x1b[37m', 30 'WHITE_ON_RED': '\\\\x1b[37;41;1m', 31 'RED_NO_BG': '\\\\x1b[31;49;1m', 32 'RESET': '\\\\x1b[0m', 33 'ERASE_AFTER': '\\\\x1b[K', 34 } 35 return build_rule( 36 name = name, 37 srcs = srcs, 38 outs = srcs, 39 cmd = 'for SRC in $SRCS; do sed %s $SRC > $(basename $SRC); done' % ' '.join(["-e 's/${%s}/%s/g'" % (k, v) for k, v in sorted(REPLACEMENTS.items())]), 40 deps = deps, 41 visibility = visibility, 42 )