bat-extras/lib/opt_hooks.sh

87 lines
1.9 KiB
Bash
Raw Normal View History

2019-09-25 23:48:21 +02:00
#!/usr/bin/env bash
# -----------------------------------------------------------------------------
# bat-extras | Copyright (C) 2019 eth-p | MIT License
#
# Repository: https://github.com/eth-p/bat-extras
# Issues: https://github.com/eth-p/bat-extras/issues
# -----------------------------------------------------------------------------
# -----------------------------------------------------------------------------
# Hooks:
# -----------------------------------------------------------------------------
# Option parser hook: color support.
2020-03-31 23:51:00 +02:00
# This will accept --no-color or --color.
2019-09-25 23:48:21 +02:00
# It will also try to accept --color=never|always|auto.
#
# The variable OPT_COLOR will be set depending on whether or not a TTY is
# detected and whether or not --color/--no-color is specified.
hook_color() {
SHIFTOPT_HOOKS+=("__shiftopt_hook__color")
__shiftopt_hook__color() {
case "$OPT" in
--no-color) OPT_COLOR=false ;;
2020-03-31 23:51:00 +02:00
--color) {
case "$OPT_VAL" in
always | true) OPT_COLOR=true ;;
never | false) OPT_COLOR=false ;;
auto) return 0 ;;
2020-03-31 23:51:00 +02:00
esac
} ;;
2019-09-25 23:48:21 +02:00
2020-03-31 23:51:00 +02:00
*) return 1 ;;
2019-09-25 23:48:21 +02:00
esac
printc_init "$OPT_COLOR"
return 0
2019-09-25 23:48:21 +02:00
}
# Default color support.
if [[ -z "$OPT_COLOR" ]]; then
if [[ -t 1 ]]; then
OPT_COLOR=true
else
OPT_COLOR=false
fi
printc_init "$OPT_COLOR"
fi
}
# Option parser hook: pager support.
# This will accept --pager='pager', --no-pager
hook_pager() {
SHIFTOPT_HOOKS+=("__shiftopt_hook__pager")
__shiftopt_hook__pager() {
case "$OPT" in
2020-03-31 23:51:00 +02:00
# Specify paging.
--no-pager) shiftval; SCRIPT_PAGER_CMD='' ;;
--paging) {
2020-03-31 23:51:00 +02:00
shiftval
case "$OPT_VAL" in
auto) : ;;
always) : ;;
never) SCRIPT_PAGER_CMD='' ;;
esac
} ;;
2019-09-25 23:48:21 +02:00
2020-03-31 23:51:00 +02:00
# Specify the pager.
--pager) {
shiftval
# [note]: These are both intentional.
# shellcheck disable=SC2034 disable=SC2206
{
SCRIPT_PAGER_CMD=($OPT_VAL)
PAGER_ARGS=()
}
2020-03-31 23:51:00 +02:00
} ;;
2019-09-25 23:48:21 +02:00
2020-03-31 23:51:00 +02:00
*) return 1 ;;
2019-09-25 23:48:21 +02:00
esac
}
}