#!/bin/sh # 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. # cbe [options...] [files...] # # Color By (file) Extension makes all lines look different, using different # ANSI-styles as it finds new trailing file-extensions. Since its list of # styles is limited, these will start being reused, given enough unique # extensions. # # The case-insensitive-comparison option is any of `-i`, `--i`, `-ins`, or # `--ins`. The help option is `-h`, `--h`, `-help`, or `--help`. insensitive=0 case "$1" in -h|--h|-help|--help) awk '/^# +cbe /, /^$/ { gsub(/^# ?/, ""); print }' "$0" exit 0 ;; -i|--i|-ins|--ins|-insensitive|--insensitive) insensitive=1 shift ;; esac [ "$1" = '--' ] && shift command='awk' if { [ -p /dev/stdout ] || [ -t 1 ]; } && [ -e /usr/bin/stdbuf ]; then command='stdbuf -oL awk' fi ${command} -v ci="${insensitive}" ' BEGIN { if (ci && IGNORECASE == "") { msg = "this variant of AWK lacks case-insensitive regex-matching" print(msg) > "/dev/stderr" exit 125 } if (ci) IGNORECASE = 1 palette[c++] = "\x1b[48;2;0;95;215m\x1b[38;2;238;238;238m" # blue palette[c++] = "\x1b[48;2;215;95;0m\x1b[38;2;238;238;238m" # orange palette[c++] = "\x1b[48;2;135;95;255m\x1b[38;2;238;238;238m" # purple palette[c++] = "\x1b[48;2;0;175;215m\x1b[38;2;238;238;238m" # cyan palette[c++] = "\x1b[48;2;255;135;255m\x1b[38;2;238;238;238m" # pink palette[c++] = "\x1b[48;2;0;135;95m\x1b[38;2;238;238;238m" # green palette[c++] = "\x1b[48;2;204;0;0m\x1b[38;2;238;238;238m" # red palette[c++] = "\x1b[48;2;168;168;168m\x1b[38;2;238;238;238m" # gray } { gsub(/\r$/, "") # ignore cursor-movers and style-changers # gsub(/\x1b\[[0-9;]*[A-Za-z]/, "") if (match($0, /\.[A-Za-z][A-Za-z0-9_-]*/)) { ext = substr($0, RSTART + 1, RLENGTH - 1) style = ext2style[ext] reset = ext2reset[ext] if (style == "") { style = palette[n++ % c] reset = "\x1b[0m" style ext2style[ext] = style ext2reset[ext] = reset } gsub(/\x1b\[0m/, reset) printf "%s%s\x1b[0m\n", style, $0 next } print } ' "$@"