Skip to content

Instantly share code, notes, and snippets.

@snejus
Last active June 24, 2024 20:45
Show Gist options
  • Save snejus/cfc70e6bb10f1786fac5c7294150280f to your computer and use it in GitHub Desktop.
Save snejus/cfc70e6bb10f1786fac5c7294150280f to your computer and use it in GitHub Desktop.
Highlight any help text in the stdin.
# 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
# everything within single quotes in blue
s/ ('\''[^'\'']+'\'')/ '"$BBLUE\1$R"'/g
# memorize current line
h
{
# remove comment
s/ -- .*//
### Required arguments
# -f arg / -f ARG
s/((^|[[ ]-)\w\b )(\w(\w|-)*\b)(\W\B|$)/\1'"$BMAGENTA\3$R"'\5/g
# --flag=arg / --flag=ARG
s/((^|[[ ]--)(\w|-)+\b=)(\w(\w|-)*\b)(\W\B|$)/\1'"$BMAGENTA\4$R"'\6/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
# ...
s/\.{3}/'"$BYELLOW&$R"'/g
# [=?argument] or [=?ARGUMENT]
s/([ =])\[(=?([a-z)(-]+|[A-Z)(-]+))\]/\1['"$BYELLOW\2$R"']/g
### Commands
# Usage: create
/([Uu]sage: )([-a-z_]+)/s//\1'"$BBLUE\2$R"'/g
# ^ create ......
/^( {,10})(([a-z0-9,_-]|\.\w)+)([ \t]{2,}| *$)/s//\1'"$BCYAN\2$R"'\4/
### Default values
# =arg|=ARG
/[A-Z]+:/s//'"$BGREEN&$R"'/g
### Specific values / arguments
# {arg,...}
/\{[^}]+\}/{
s//'"$BGREEN&$R"'/
s/,/'"$R,$BGREEN"'/g
}
# arg|other-arg
/((\w|-)+\|)+(\w|-)+/{
s//'"$BGREEN&$R"'/g
s/\|/'"$R|$BGREEN"'/g
}
### Punctuation
s/([^[:cntrl:]a-z-]|0m)\[/\1'"$YELLOW\[$R"'/g
s/(\]+)([^]a-z]|$)/'"$YELLOW\1$R"'\2/g
### Group title
/^[A-Z -][^[:cntrl:]a-z]+$/{
/^([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
s/$/'"$R"'/
# append it to the memorised non-comment bit
H
}
}
# retrieve the entire thing and join it
x; s/\n//
'
}
# vim: ft=zsh
@snejus
Copy link
Author

snejus commented Jun 24, 2024

Examples

sed

image

grep

image

bat (Rust)

image

yay (Go)

image

pip (Python)

image

git-merge(1) manual

image

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment