File: jawk.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 # jawk [good...] [bad...] [meh...] [filenames...] 27 # 28 # Judge with AWK colors lines using up to 3 (optional) AWK conditions, named 29 # `good` (green), `bad` (red), and `meh` (gray) respectively. Lines failing 30 # all the conditions given are shown back verbatim. 31 # 32 # The good style is a colorblind-friendly blue if the environment variable 33 # COLORBLIND is declared and set to 1. 34 35 36 case "$1" in 37 -h|--h|-help|--help) 38 awk '/^# +jawk /, /^$/ { gsub(/^# ?/, ""); print }' "$0" 39 exit 0 40 ;; 41 esac 42 43 [ "$1" = "--" ] && shift 44 45 command="awk" 46 if [ -p 1 ] || [ -t 1 ]; then 47 command="stdbuf -oL awk" 48 fi 49 50 good="${1:-0}" 51 bad="${2:-0}" 52 meh="${3:-0}" 53 54 [ $# -gt 0 ] && shift 55 [ $# -gt 0 ] && shift 56 [ $# -gt 0 ] && shift 57 58 ${command} ' 59 BEGIN { 60 # normal good-style is green, colorblind-friendly good-style is blue 61 good_style = ENVIRON["COLORBLIND"] != 0 ? 62 "\x1b[38;2;0;95;215m" : 63 "\x1b[38;2;0;135;95m" 64 good_fmt = good_style "%s\x1b[0m\n" 65 good_reset = "\x1b[0m" good_style 66 } 67 68 { low = lower = tolower($0) } 69 70 '"${good}"' { 71 gsub(/\x1b\[0m/, good_reset) 72 printf good_fmt, $0 73 next 74 } 75 76 '"${bad}"' { 77 gsub(/\x1b\[0m/, "\x1b[0m\x1b[38;2;204;0;0m") 78 printf "\x1b[38;2;204;0;0m%s\x1b[0m\n", $0 79 next 80 } 81 82 '"${meh}"' { 83 gsub(/\x1b\[0m/, "\x1b[0m\x1b[38;2;168;168;168m") 84 printf "\x1b[38;2;168;168;168m%s\x1b[0m\n", $0 85 next 86 } 87 88 1 89 ' "$@"