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