From 55be49e8871194ec8c34a2ffa9f6589775e6d773 Mon Sep 17 00:00:00 2001 From: John D Pell Date: Thu, 9 Sep 2021 16:16:54 -0700 Subject: [PATCH 1/3] plugins/less-pretty-cat: use `_command_exists` MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Addresses bash-it/bash-it#1632 Alsö, code style cleanup: quote variables, handle unbound parameters, &c. --- plugins/available/less-pretty-cat.plugin.bash | 22 +++++++++---------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/plugins/available/less-pretty-cat.plugin.bash b/plugins/available/less-pretty-cat.plugin.bash index b6b9d1f0..bfe0fe09 100644 --- a/plugins/available/less-pretty-cat.plugin.bash +++ b/plugins/available/less-pretty-cat.plugin.bash @@ -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 From 58576483773e3f90f0044b078f8a6d9b8b61a163 Mon Sep 17 00:00:00 2001 From: John D Pell Date: Wed, 22 Sep 2021 14:16:23 -0700 Subject: [PATCH 2/3] plugins/less-pretty-cat: simplify code flow Convert from indented if-block to return then unindented code. This should have basically one line change at the top, one line removed at the bottom, and then all whitespace. --- clean_files.txt | 1 + plugins/available/less-pretty-cat.plugin.bash | 49 +++++++++---------- 2 files changed, 25 insertions(+), 25 deletions(-) diff --git a/clean_files.txt b/clean_files.txt index 2242ef5e..240c6059 100644 --- a/clean_files.txt +++ b/clean_files.txt @@ -90,6 +90,7 @@ plugins/available/history-substring-search.plugin.bash plugins/available/history.plugin.bash plugins/available/hub.plugin.bash plugins/available/jump.plugin.bash +plugins/available/less-pretty-cat.plugin.bash plugins/available/node.plugin.bash plugins/available/nodenv.plugin.bash plugins/available/plenv.plugin.bash diff --git a/plugins/available/less-pretty-cat.plugin.bash b/plugins/available/less-pretty-cat.plugin.bash index bfe0fe09..cde646c5 100644 --- a/plugins/available/less-pretty-cat.plugin.bash +++ b/plugins/available/less-pretty-cat.plugin.bash @@ -2,30 +2,29 @@ cite about-plugin about-plugin 'pygmentize instead of cat to terminal if possible' -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() - { - 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' - local file - : "${BASH_IT_CCAT_STYLE:=default}" +_command_exists pygmentize || return - for file in "$@" - do - pygmentize -f 256 -O style="$BASH_IT_CCAT_STYLE" -g "$file" 2>/dev/null || command cat "$file"; - done - } +# 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() { + about 'runs either pygmentize or cat on each file passed in' + param '*: files to concatenate (as normally passed to cat)' + example 'ccat mysite/manage.py dir/text-file.txt' - function cless() - { - 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' - : "${BASH_IT_CLESS_STYLE:=default}" - pygmentize -f 256 -O style="$BASH_IT_CLESS_STYLE" -g "$@" | command less -R - } -fi + local file + : "${BASH_IT_CCAT_STYLE:=default}" + + for file in "$@"; do + pygmentize -f 256 -O style="$BASH_IT_CCAT_STYLE" -g "$file" 2> /dev/null || command cat "$file" + done +} + +function cless() { + about 'pigments the file passed in and passes it to less for pagination' + param '1: the file to paginate with less' + example 'cless mysite/manage.py' + + : "${BASH_IT_CLESS_STYLE:=default}" + + pygmentize -f 256 -O style="$BASH_IT_CLESS_STYLE" -g "$@" | command less -R +} From 2ddb40751f5dcdfbeb196a6d24deac599f39411f Mon Sep 17 00:00:00 2001 From: John D Pell Date: Thu, 23 Sep 2021 15:42:23 -0700 Subject: [PATCH 3/3] plugin/less-pretty-cat: remove `|| cat` The logic to run `cat` if `pygmentize` fails seems useless, so just remove it. --- plugins/available/less-pretty-cat.plugin.bash | 17 +++++------------ 1 file changed, 5 insertions(+), 12 deletions(-) diff --git a/plugins/available/less-pretty-cat.plugin.bash b/plugins/available/less-pretty-cat.plugin.bash index cde646c5..139e5188 100644 --- a/plugins/available/less-pretty-cat.plugin.bash +++ b/plugins/available/less-pretty-cat.plugin.bash @@ -7,24 +7,17 @@ _command_exists pygmentize || return # 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() { - about 'runs either pygmentize or cat on each file passed in' + about 'runs pygmentize on each file passed in' param '*: files to concatenate (as normally passed to cat)' example 'ccat mysite/manage.py dir/text-file.txt' - local file - : "${BASH_IT_CCAT_STYLE:=default}" - - for file in "$@"; do - pygmentize -f 256 -O style="$BASH_IT_CCAT_STYLE" -g "$file" 2> /dev/null || command cat "$file" - done + pygmentize -f 256 -O style="${BASH_IT_CCAT_STYLE:-default}" -g "$@" } function cless() { - about 'pigments the file passed in and passes it to less for pagination' - param '1: the file to paginate with less' + about 'pigments the files passed in and passes to less for pagination' + param '*: the files to paginate with less' example 'cless mysite/manage.py' - : "${BASH_IT_CLESS_STYLE:=default}" - - pygmentize -f 256 -O style="$BASH_IT_CLESS_STYLE" -g "$@" | command less -R + pygmentize -f 256 -O style="${BASH_IT_CLESS_STYLE:-default}" -g "$@" | command less -R }