Skip to content

Instantly share code, notes, and snippets.

@ryo1kato
Created December 20, 2020 17:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ryo1kato/1f9b51326eb8eb0b9d10912b705e58e0 to your computer and use it in GitHub Desktop.
Save ryo1kato/1f9b51326eb8eb0b9d10912b705e58e0 to your computer and use it in GitHub Desktop.
color pager
#!/bin/bash
# color pager
# run command with appropriate color option (like --color-output for 'jq') and pipe to a pager
if which lv >/dev/null 2>&1
then
pager=(lv -c)
else
pager=(less -R)
fi
set -u -e
help () {
echo "Usage: ${0##*/} {grep|jq|...} [OPTIONS...]"
}
[ $# -eq 0 ] && { help; exit 1; }
case $1 in
-h|--help)
echo "Usage: ${0##*/} [grep|jq]"
exit 1
;;
esac
cmd=$1
shift
color=()
case $cmd in
grep|[fe]grep|cargo)
color=('--color=always');;
jq)
color=('--color-output');;
hmlgrep|amlgrep|jq-grep|ls)
color=('--color');;
esac
set +u # allow ${color[@]} to be empty
case "$cmd" in
# --color option has to be suffixed after subcommand for cargo
cargo) args=("$@" "${color[@]}");;
*) args=("${color[@]}" "$@");;
esac
"$cmd" "${args[@]}" 2>&1 | "${pager[@]}"
exit ${PIPESTATUS[0]}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment