#!/bin/sh # The MIT License (MIT) # # Copyright (c) 2026 pacman64 # # Permission is hereby granted, free of charge, to any person obtaining a copy # of this software and associated documentation files (the "Software"), to deal # in the Software without restriction, including without limitation the rights # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell # copies of the Software, and to permit persons to whom the Software is # furnished to do so, subject to the following conditions: # # The above copyright notice and this permission notice shall be included in # all copies or substantial portions of the Software. # # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE # SOFTWARE. # hima [options...] [regexes...] # # # HIlight MAtches ANSI-styles matching regular expressions along lines read # from the standard input. The regular-expression mode used is a superset of # the commonly-used "extended-mode". # # All ANSI-style sequences from stdin are removed, to avoid messing those up: # the only ANSI styles left in the output will be from the matches, if any. # # Also, multiple matches in a line never overlap: at each step along a line, # the earliest-starting match among the regexes always wins, as the order # regexes are given among the arguments never matters. # # The options are, available both in single and double-dash versions # # -h, -help show this help message # -f, -filter filter out (ignore) lines with no matches # -i, -ins match regexes case-insensitively filter=0 case_insensitive=0 for arg in "$@"; do if [ "${arg}" = '--' ]; then shift break fi case "$1" in -f|--f|-filter|--filter) filter=1 shift continue ;; -h|--h|-help|--help) awk '/^# +hima /, /^$/ { gsub(/^# ?/, ""); print }' "$0" exit 0 ;; -i|--i|-ins|--ins) case_insensitive=1 shift continue ;; esac break done [ "$1" = '--' ] && shift command='awk' if { [ -p /dev/stdout ] || [ -t 1 ]; } && [ -e /usr/bin/stdbuf ]; then command='stdbuf -oL awk' fi ci=' BEGIN { if (IGNORECASE == "") { m = "your `awk` command lacks case-insensitive regex-matching" print(m) > "/dev/stderr" exit 125 } IGNORECASE = 1 } ' if [ "${case_insensitive}" -eq 0 ]; then ci='' fi expr="$(awk ' BEGIN { for (i = 1; i < ARGC; i++) { if (i > 1) printf "|" printf "(%s)", ARGV[i] } exit } ' "$@")" ${command} -v expr="${expr}" -v filter="${filter}" "${ci}"' { # ignore cursor-movers and style-changers gsub(/\x1b\[[0-9;]*[A-Za-z]/, "") # ignore OSC sequences gsub(/\x1b\][^\x1b]+\x1b\\/, "") if (filter && $0 !~ expr) next gsub(expr, "\x1b[7m&\x1b[0m") print } '