File: gbm.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 # gbm [options...] [good regex...] [bad regex...] [meh regex...] [files...] 27 # 28 # Good, Bad, Meh colors lines using up to 3 regular expressions, keeping all 29 # other input lines verbatim. The colors are green or blue for `good` lines, 30 # red for `bad` lines, and gray for `meh` lines. 31 # 32 # For the `good` matches, a colorblind-friendly blue is used instead of green 33 # if either environment variable COLORBLIND or COLOR_BLIND is declared and set 34 # to 1. 35 # 36 # The handy case-insensitive shortcut options may cause this tool to fail, 37 # if the main AWK tool installed doesn't support the special IGNORECASE 38 # variable. 39 # 40 # The options are, available both in single and double-dash versions 41 # 42 # -h, -help show this help message 43 # -i, -ins match regexes case-insensitively; may fail the default `awk` 44 45 46 buf=0 47 ins=0 48 49 while [ $# -gt 0 ]; do 50 case "$1" in 51 -b|--b|-buffered|--buffered) buf=1; shift; continue ;; 52 53 -h|--h|-help|--help) 54 awk '/^# +gbm /, /^$/ { gsub(/^# ?/, ""); print }' "$0" 55 exit 0 56 ;; 57 58 -i|--i|-ins|--ins|-insensitive|--insensitive) ins=1; shift; continue ;; 59 60 --) shift; break ;; 61 62 -*) 63 printf "unsupported option '%s'\n" "$1" >&2 64 exit 1 65 ;; 66 esac 67 68 break 69 done 70 71 # show all non-existing files given: first 3 args are conditions, not files 72 i=0 73 fail=0 74 for a in "$@"; do 75 i=$(( i + 1 )) 76 [ "${i}" -lt 4 ] && continue 77 [ "$a" = "-" ] && continue 78 [ -e "$a" ] && continue 79 printf "no file named \"%s\"\n" "$a" >&2 80 fail=1 81 done 82 83 [ "${fail}" -gt 0 ] && exit 2 84 85 flush=0 86 if [ "${buf}" -eq 0 ] && { [ -p /dev/stdout ] || [ -t 1 ]; }; then 87 flush=1 88 fi 89 90 awk -v flush="${flush}" -v ins="${ins}" ' 91 BEGIN { 92 got_good = ARGC > 1 93 got_bad = ARGC > 2 94 got_meh = ARGC > 3 95 good = ARGV[1] 96 bad = ARGV[2] 97 meh = ARGV[3] 98 delete ARGV[1] 99 delete ARGV[2] 100 delete ARGV[3] 101 102 if (ins && IGNORECASE == "") { 103 msg = "this variant of AWK lacks case-insensitive regex-matching" 104 print(msg) > "/dev/stderr" 105 exit 125 106 } 107 if (ins) IGNORECASE = 1 108 109 # normal good-style is green, colorblind-friendly good-style is blue 110 cb = ENVIRON["COLORBLIND"] != 0 || ENVIRON["COLOR_BLIND"] != 0 111 gs = cb ? "\x1b[38;2;0;95;215m" : "\x1b[38;2;0;135;95m" 112 good_fmt = gs "%s\x1b[0m\n" 113 good_reset = "\x1b[0m" gs 114 115 bs = "\x1b[38;2;204;0;0m" 116 bad_fmt = bs "%s\x1b[0m\n" 117 bad_reset = "\x1b[0m" bs 118 119 ms = "\x1b[38;2;168;168;168m" 120 meh_fmt = ms "%s\x1b[0m\n" 121 meh_reset = "\x1b[0m" ms 122 } 123 124 got_good && $0 ~ good { 125 gsub(/\x1b\[0m/, good_reset) 126 printf good_fmt, $0 127 if (flush) fflush() 128 next 129 } 130 131 got_bad && $0 ~ bad { 132 gsub(/\x1b\[0m/, bad_reset) 133 printf bad_fmt, $0 134 if (flush) fflush() 135 next 136 } 137 138 got_meh && $0 ~ meh { 139 gsub(/\x1b\[0m/, meh_reset) 140 printf meh_fmt, $0 141 if (flush) fflush() 142 next 143 } 144 145 { 146 print 147 if (flush) fflush() 148 } 149 ' "$@"