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 # handle leading options 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 awk -v sensitive="${sensitive}" ' 53 BEGIN { 54 palette[n++] = "\x1b[38;2;0;95;215m" # blue 55 palette[n++] = "\x1b[38;2;215;95;0m" # orange 56 palette[n++] = "\x1b[38;2;135;95;255m" # purple 57 palette[n++] = "\x1b[38;2;0;175;215m" # cyan 58 palette[n++] = "\x1b[38;2;255;135;255m" # pink 59 palette[n++] = "\x1b[38;2;0;135;95m" # green 60 palette[n++] = "\x1b[38;2;204;0;0m" # red 61 palette[n++] = "\x1b[38;2;168;168;168m" # gray 62 palcount = length(palette) 63 n = 0 64 } 65 66 { 67 # ignore carriage-returns, which may interfere with later regexes 68 gsub(/\r$/, "") 69 # ignore cursor-movers and style-changers 70 gsub(/\x1b\[[0-9;]*[A-Za-z]/, "") 71 72 match($0, /\.[A-Za-z][A-Za-z0-9_-]*$/) 73 ext = substr($0, RSTART, RLENGTH) 74 75 if (ext == "") { 76 print; fflush() 77 next 78 } 79 if (!sensitive) ext = tolower(ext) 80 81 style = ext2style[ext] 82 if (style == "") { 83 style = palette[n % palcount] 84 ext2style[ext] = style 85 n++ 86 } 87 printf "%s%s\x1b[0m\n", style, $0; fflush() 88 } 89 ' "$@"