Last active
February 23, 2025 05:25
-
-
Save snejus/cfc70e6bb10f1786fac5c7294150280f to your computer and use it in GitHub Desktop.
Highlight any help text in the stdin.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Highlight any help text in the stdin. | |
# It has been tested with various command line tools and should be able to handle most | |
# of them regardless of the language or framework used to write them. | |
# | |
# Since this is just a sed pattern, it works in any shell. For example, in my zsh setup | |
# I have a global alias | |
# | |
# export LESS=-FR # enable colors, exit if contents fit in one screen | |
# alias -g h='--help |& colorize_help | less' | |
# | |
# This way, simply appending 'h' to a command shows me its highlighted, readable help text. | |
# Since it runs sed a single time, it does not add much overhead. On my system, it adds | |
# around 1-5ms. | |
# | |
# Author: Sarunas Nejus, 2020 | |
# License: MIT | |
R=$'\033[0m' | |
B=$'\033[1m' | |
RB=$'\033[22m' # reset bold | |
BRED=$'\033[1;31m' | |
BGREEN=$'\033[1;32m' | |
BYELLOW=$'\033[1;33m' | |
BBLUE=$'\033[1;34m' | |
BMAGENTA=$'\033[1;35m' | |
BCYAN=$'\033[1;36m' | |
BGREY=$'\033[1;38;5;239m' | |
BINVGREY=$'\033[1;30;48;5;243m' | |
colorize_help() { | |
# args: | |
# info: colorize help text in the stdin | |
sed -r ' | |
# remove any previous ansi colors | |
s/(.(\[[0-9][^ m]*m)+)//g | |
# memorize current line | |
h | |
{ | |
# remove comment | |
s/ -- .*// | |
### Strings in single quotes | |
s/ ('\''[^'\'']+'\'')/ '"$BBLUE\1$R"'/g | |
### Required arguments | |
# -f arg, -f ARG | |
/((^|[[ ]-)(\w\b|[@?]) )(\w(\w|-)*\b)(\W\B|$)/{ | |
s//\1'"$BMAGENTA\4$R"'\6/g | |
# --flag=arg | |
# if a lower-case argument is given for both short and long flag, e.g. | |
# -f something, --file=something, ensure its color is consistent | |
# otherwise, lower-case argument following an equal sign is assumed to be | |
# a default value | |
s/(, --(\w|-)+\b=)([a-z-]+\b)(\W\B|$)/\1'"$BMAGENTA\3$R"'\4/g | |
} | |
# --flag=ARG | |
s/((^|[[ ]--)(\w|-)+\b=)([A-Z_-]*\b)(\W\B|$)/\1'"$BMAGENTA\4$R"'\5/g | |
# Within <> | |
s/<([^>…]+)>/'"$MAGENTA<$BMAGENTA\1$RB>$R"'/g | |
# Capitalized | |
/^[A-Z -]+$/!{ # skip lines that only have capitalized words | |
/([( ])(\$?\b[A-Z][-_A-Z0-9/]+)(\b[^:]|$)/s//\1'"$BMAGENTA\2$R"'\3/g | |
} | |
### Flags (optional) | |
# -f, --flag, --[no-]flag | |
s/(^|[[( ])((-?-((\w|\b-)+\b|\[(\w|[=-])+\]|[?@]| -)+)+)([^}|]|$)/\1'"$BYELLOW\2$R"'\7/g | |
# [=?argument], [=?ARGUMENT] | |
s/([ =])\[(=?(\w|[)(. -]| \| )+)\]/\1['"$BYELLOW\2$R"']/g | |
# ... | |
/\.{3}([^[:cntrl:]]|$)/s/\.{3}/'"$BYELLOW&$R"'/g | |
### Commands | |
# Usage: create | |
/([Uu]sage: )([-a-z0-9_]+)/s//\1'"$BBLUE\2$R"'/g | |
# ^ create ...... | |
/^( {,10})(([a-z0-9,_-]|\.\w)+( \([a-z?, ]+\))?)([ \t]{2,}| *$)/s//\1'"$BCYAN\2$R"'\5/ | |
### Default values | |
# =arg, =ARG, =arg|ARG | |
/=([A-Za-z0-9|-]+)/{ | |
s//='"$BGREEN\1$R"'/g | |
s/\|/'"$R&$BGREEN"'/g | |
} | |
# arg|other-arg | |
/([[< ])((-?\b)((\w|-)+\|)+(\w|-)+)\b/{ | |
s//\1'"$BGREEN\2$R"'/g | |
s/\|/'"$R&$BGREEN"'/g | |
} | |
### Specific values / arguments | |
# {arg,...} | |
/\{[^}]+\}/{ | |
s//'"$BGREEN&$R"'/ | |
s/,/'"$R,$BGREEN"'/g | |
} | |
### Brackets | |
s/([^[:cntrl:]a-z-]|0m)\[/\1'"$YELLOW\[$R"'/g | |
s/(\]+)([^]a-z]|$)/'"$YELLOW\1$R"'\2/g | |
### Group title | |
/^[A-Z][^[:cntrl:]a-z]+$/{ | |
s/ +$// | |
/^([A-Z][^,|<]+)$/s//'"$BINVGREY & $R"'/ | |
/^ # [A-Za-z]/d | |
} | |
} | |
### Comments | |
{ | |
# swap/memorise the non-comment bit processed above with the initial/memorised line | |
x | |
# remove non-comment bit and color the comment | |
/.* -- (.*)/{ | |
/.*[^ ].* -- (.*)/s//'"$BGREY -- \1$R"'/ | |
/[ ]+ -- (.*)/s//'"$BGREY \1$R"'/ | |
# if comment was not found, remove the duplicate command | |
s/ [a-z]+ *$// | |
# color keywords | |
s/([ (])_([^, ]+)/\1'"$BBLUE\2$BGREY"'/g | |
# color strings in single quotes | |
s/ ('\''[^'\'']+'\'')/ '"$BBLUE\1$BGREY"'/g | |
# color TODOs in red | |
s/\bTODO\b/'"$BRED&$BGREY"'/g | |
s/$/'"$R"'/ | |
# append it to the memorised non-comment bit | |
H | |
} | |
} | |
# retrieve the entire thing and join it | |
x; s/\n// | |
' | |
} | |
# vim: ft=zsh |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Examples
sed
grep
bat (Rust)
yay (Go)
pip (Python)
git-merge(1) manual