plugins/less-pretty-cat: use `_command_exists`

Addresses bash-it/bash-it#1632

Alsö, code style cleanup: quote variables, handle unbound parameters,  &c.
pull/1957/head
John D Pell 2021-09-09 16:16:54 -07:00
parent d6c4c0cc88
commit 55be49e887
1 changed files with 11 additions and 11 deletions

View File

@ -1,13 +1,9 @@
# shellcheck shell=bash
cite about-plugin
about-plugin 'pygmentize instead of cat to terminal if possible'
if $(command -v pygmentize &> /dev/null) ; then
# get the full paths to binaries
CAT_BIN=$(which cat)
LESS_BIN=$(which less)
BASH_IT_CCAT_STYLE="${BASH_IT_CCAT_STYLE:=default}"
BASH_IT_CLESS_STYLE="${BASH_IT_CLESS_STYLE:=default}"
if _command_exists pygmentize
then
# pigmentize cat and less outputs - call them ccat and cless to avoid that
# especially cat'ed output in scripts gets mangled with pygemtized meta characters
function ccat()
@ -15,17 +11,21 @@ if $(command -v pygmentize &> /dev/null) ; then
about 'runs either pygmentize or cat on each file passed in'
param '*: files to concatenate (as normally passed to cat)'
example 'cat mysite/manage.py dir/text-file.txt'
for var;
local file
: "${BASH_IT_CCAT_STYLE:=default}"
for file in "$@"
do
pygmentize -f 256 -O style="$BASH_IT_CCAT_STYLE" -g "$var" 2>/dev/null || "$CAT_BIN" "$var";
pygmentize -f 256 -O style="$BASH_IT_CCAT_STYLE" -g "$file" 2>/dev/null || command cat "$file";
done
}
function cless()
{
about 'it pigments the file passed in and passes it to less for pagination'
about 'pigments the file passed in and passes it to less for pagination'
param '$1: the file to paginate with less'
example 'less mysite/manage.py'
pygmentize -f 256 -O style="$BASH_IT_CLESS_STYLE" -g $* | "$LESS_BIN" -R
: "${BASH_IT_CLESS_STYLE:=default}"
pygmentize -f 256 -O style="$BASH_IT_CLESS_STYLE" -g "$@" | command less -R
}
fi