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 buffered=0 49 case_insensitive=0 50 51 while [ $# -gt 0 ]; do 52 if [ "$1" = '--' ]; then 53 shift 54 break 55 fi 56 57 case "$1" in 58 -b|--b|-buffered|--buffered) 59 buffered=1 60 shift 61 continue 62 ;; 63 64 -f|--f|-filter|--filter) 65 filter=1 66 shift 67 continue 68 ;; 69 70 -fi|--fi|-if|--if) 71 filter=1 72 case_insensitive=1 73 shift 74 continue 75 ;; 76 77 -h|--h|-help|--help) 78 awk '/^# +hima /, /^$/ { gsub(/^# ?/, ""); print }' "$0" 79 exit 0 80 ;; 81 82 -i|--i|-ins|--ins|-insensitive|--insensitive) 83 case_insensitive=1 84 shift 85 continue 86 ;; 87 esac 88 89 break 90 done 91 92 [ "$1" = '--' ] && shift 93 94 flush=0 95 if [ "${buffered}" -eq 0 ] && { [ -p /dev/stdout ] || [ -t 1 ]; }; then 96 flush=1 97 fi 98 99 ci=' 100 BEGIN { 101 if (IGNORECASE == "") { 102 m = "your `awk` command lacks case-insensitive regex-matching" 103 print(m) > "/dev/stderr" 104 exit 125 105 } 106 IGNORECASE = 1 107 } 108 ' 109 if [ "${case_insensitive}" -eq 0 ]; then 110 ci='' 111 fi 112 113 expr="$(awk ' 114 BEGIN { 115 for (i = 1; i < ARGC; i++) { 116 if (i > 1) printf "|" 117 printf "(%s)", ARGV[i] 118 } 119 exit 120 } 121 ' "$@")" 122 123 awk -v flush="${flush}" -v expr="${expr}" -v filter="${filter}" "${ci}"' 124 { 125 # ignore cursor-movers and style-changers 126 gsub(/\x1b\[[0-9;]*[A-Za-z]/, "") 127 128 # ignore OSC sequences 129 gsub(/\x1b\][^\x1b]+\x1b\\/, "") 130 131 if (filter && $0 !~ expr) next 132 133 gsub(expr, "\x1b[7m&\x1b[0m") 134 print 135 if (flush) fflush() 136 } 137 '