File: cbe.sh 1 #!/bin/sh 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 # cbe [options...] [files...] 27 # 28 # Color By (file) Extension makes all lines look different, using different 29 # ANSI-styles as it finds new trailing file-extensions. Since its list of 30 # styles is limited, these will start being reused, given enough unique 31 # extensions. 32 # 33 # A colorblind-friendly palette which avoids red is available instead of the 34 # default one, if either environment variable COLORBLIND or COLOR_BLIND is 35 # declared and set to 1. 36 # 37 # The options are, available both in single and double-dash versions 38 # 39 # -h, -help show this help message 40 # -i, -ins compare lines case-insensitively 41 42 43 44 insensitive=0 45 case "$1" in 46 -h|--h|-help|--help) 47 awk '/^# +cbe /, /^$/ { gsub(/^# ?/, ""); print }' "$0" 48 exit 0 49 ;; 50 51 -i|--i|-ins|--ins|-insensitive|--insensitive) 52 insensitive=1 53 shift 54 ;; 55 56 -) ;; 57 58 --) shift ;; 59 60 -*) 61 printf "%s: unsupported option '%s'\n" "$0" "$1" >&2 62 exit 1 63 ;; 64 esac 65 66 # show all non-existing files given 67 failed=0 68 for arg in "$@"; do 69 [ "${arg}" = "-" ] && continue 70 [ -e "${arg}" ] && continue 71 printf "no file named \"%s\"\n" "${arg}" >&2 72 failed=1 73 done 74 75 [ "${failed}" -gt 0 ] && exit 2 76 77 flush=0 78 if [ -p /dev/stdout ] || [ -t 1 ]; then 79 flush=1 80 fi 81 82 awk -v flush="${flush}" -v ci="${insensitive}" ' 83 BEGIN { 84 if (ci && IGNORECASE == "") { 85 msg = "this variant of AWK lacks case-insensitive regex-matching" 86 print(msg) > "/dev/stderr" 87 exit 125 88 } 89 if (ci) IGNORECASE = 1 90 91 cb = ENVIRON["COLORBLIND"] != 0 || ENVIRON["COLOR_BLIND"] != 0 92 93 palette[c++] = "\x1b[48;2;0;95;215m\x1b[38;2;238;238;238m" # blue 94 palette[c++] = "\x1b[48;2;215;95;0m\x1b[38;2;238;238;238m" # orange 95 palette[c++] = "\x1b[48;2;135;95;255m\x1b[38;2;238;238;238m" # purple 96 palette[c++] = "\x1b[48;2;0;175;215m\x1b[38;2;238;238;238m" # cyan 97 palette[c++] = "\x1b[48;2;255;135;255m\x1b[38;2;238;238;238m" # pink 98 palette[c++] = "\x1b[48;2;0;135;95m\x1b[38;2;238;238;238m" # green 99 if (!cb) palette[c++] = "\x1b[48;2;204;0;0m\x1b[38;2;238;238;238m" # red 100 palette[c++] = "\x1b[48;2;168;168;168m\x1b[38;2;238;238;238m" # gray 101 } 102 103 { 104 gsub(/\r$/, "") 105 106 # ignore cursor-movers and style-changers 107 # gsub(/\x1b\[[0-9;]*[A-Za-z]/, "") 108 109 if (match($0, /\.[A-Za-z][A-Za-z0-9_-]*/)) { 110 ext = substr($0, RSTART + 1, RLENGTH - 1) 111 style = ext2style[ext] 112 reset = ext2reset[ext] 113 114 if (style == "") { 115 style = palette[n++ % c] 116 reset = "\x1b[0m" style 117 ext2style[ext] = style 118 ext2reset[ext] = reset 119 } 120 121 gsub(/\x1b\[0m/, reset) 122 printf "%s%s\x1b[0m\n", style, $0 123 if (flush) fflush() 124 next 125 } 126 127 print 128 if (flush) fflush() 129 } 130 ' "$@"