File: hima.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 # hima [options...] [regexes...] 27 # 28 # 29 # HIlight MAtches ANSI-styles matching regular expressions along lines read 30 # from the standard input. The regular-expression mode used is a superset of 31 # the commonly-used "extended-mode". 32 # 33 # All ANSI-style sequences from stdin are removed, to avoid messing those up: 34 # the only ANSI styles left in the output will be from the matches, if any. 35 # 36 # Also, multiple matches in a line never overlap: at each step along a line, 37 # the earliest-starting match among the regexes always wins, as the order 38 # regexes are given among the arguments never matters. 39 # 40 # The options are, available both in single and double-dash versions 41 # 42 # -h, -help show this help message 43 # -f, -filter filter out (ignore) lines with no matches 44 # -i, -ins match regexes case-insensitively 45 46 47 filter=0 48 case_insensitive=0 49 50 for arg in "$@"; do 51 if [ "${arg}" = '--' ]; then 52 shift 53 break 54 fi 55 56 case "$1" in 57 -f|--f|-filter|--filter) 58 filter=1 59 shift 60 continue 61 ;; 62 63 -h|--h|-help|--help) 64 awk '/^# +hima /, /^$/ { gsub(/^# ?/, ""); print }' "$0" 65 exit 0 66 ;; 67 68 -i|--i|-ins|--ins) 69 case_insensitive=1 70 shift 71 continue 72 ;; 73 esac 74 75 break 76 done 77 78 [ "$1" = '--' ] && shift 79 80 command='awk' 81 if { [ -p /dev/stdout ] || [ -t 1 ]; } && [ -e /usr/bin/stdbuf ]; then 82 command='stdbuf -oL awk' 83 fi 84 85 ci=' 86 BEGIN { 87 if (IGNORECASE == "") { 88 m = "your `awk` command lacks case-insensitive regex-matching" 89 print(m) > "/dev/stderr" 90 exit 125 91 } 92 IGNORECASE = 1 93 } 94 ' 95 if [ "${case_insensitive}" -eq 0 ]; then 96 ci='' 97 fi 98 99 expr="$(awk ' 100 BEGIN { 101 for (i = 1; i < ARGC; i++) { 102 if (i > 1) printf "|" 103 printf "(%s)", ARGV[i] 104 } 105 exit 106 } 107 ' "$@")" 108 109 ${command} -v expr="${expr}" -v filter="${filter}" "${ci}"' 110 { 111 # ignore cursor-movers and style-changers 112 gsub(/\x1b\[[0-9;]*[A-Za-z]/, "") 113 114 # ignore OSC sequences 115 gsub(/\x1b\][^\x1b]+\x1b\\/, "") 116 117 if (filter && $0 !~ expr) next 118 119 gsub(expr, "\x1b[7m&\x1b[0m") 120 print 121 } 122 '