File: cbe.sh 1 #!/bin/sh 2 3 # The MIT License (MIT) 4 # 5 # Copyright © 2020-2025 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 # Any ANSI-styles from the original input lines are ignored. 34 # 35 # The case-insensitive-comparison option is any of `-i`, `--i`, `-ins`, or 36 # `--ins`. The help option is `-h`, `--h`, `-help`, or `--help`. 37 38 39 40 sensitive=1 41 case "$1" in 42 -h|--h|-help|--help) 43 awk '/^# +cbe /, /^$/ { gsub(/^# ?/, ""); print }' "$0" 44 exit 0 45 ;; 46 -i|--i|-ins|--ins) 47 sensitive=0 48 shift 49 ;; 50 esac 51 52 [ "$1" = "--" ] && shift 53 54 command="awk" 55 if [ -p 1 ] || [ -t 1 ]; then 56 command="stdbuf -oL awk" 57 fi 58 59 ${command} -v sensitive="${sensitive}" ' 60 BEGIN { 61 palette[n++] = "\x1b[38;2;0;95;215m" # blue 62 palette[n++] = "\x1b[38;2;215;95;0m" # orange 63 palette[n++] = "\x1b[38;2;135;95;255m" # purple 64 palette[n++] = "\x1b[38;2;0;175;215m" # cyan 65 palette[n++] = "\x1b[38;2;255;135;255m" # pink 66 palette[n++] = "\x1b[38;2;0;135;95m" # green 67 palette[n++] = "\x1b[38;2;204;0;0m" # red 68 palette[n++] = "\x1b[38;2;168;168;168m" # gray 69 palcount = length(palette) 70 n = 0 71 } 72 73 { 74 # ignore carriage-returns, which may interfere with later regexes 75 gsub(/\r$/, "") 76 # ignore cursor-movers and style-changers 77 gsub(/\x1b\[[0-9;]*[A-Za-z]/, "") 78 79 match($0, /\.[A-Za-z][A-Za-z0-9_-]*$/) 80 ext = substr($0, RSTART, RLENGTH) 81 82 if (ext == "") { 83 print 84 next 85 } 86 if (!sensitive) ext = tolower(ext) 87 88 style = ext2style[ext] 89 if (style == "") { 90 style = palette[n % palcount] 91 ext2style[ext] = style 92 n++ 93 } 94 printf "%s%s\x1b[0m\n", style, $0 95 } 96 ' "$@"