Refactor common options into opt hooks

This commit is contained in:
Ethan P 2019-09-25 14:48:21 -07:00
parent d776b7acbc
commit 9c10ef4871
No known key found for this signature in database
GPG Key ID: 6963FD04F6CF35EA
5 changed files with 102 additions and 52 deletions

View File

@ -6,6 +6,7 @@
# Issues: https://github.com/eth-p/bat-extras/issues
# -----------------------------------------------------------------------------
PROGRAM="$(basename "$0" .sh)"
SHIFTOPT_HOOKS=()
# Sets the internal _ARGV, _ARGV_INDEX, and _ARGV_LAST variables used when
# parsing options with the shiftopt and shiftval functions.
@ -42,6 +43,17 @@ shiftopt() {
# Pop array.
((_ARGV_INDEX++))
# Handle hooks.
local hook
for hook in "${SHIFTOPT_HOOKS[@]}"; do
if "$hook"; then
shiftopt
return $?
fi
done
# Return.
return 0
}

75
lib/opt_hooks.sh Normal file
View File

@ -0,0 +1,75 @@
#!/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.
# This will accept --no-color, or --color.
# 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; printc_init "$OPT_COLOR";;
--color) {
case "$OPT_VAL" in
auto) :;;
always|true) OPT_COLOR=true; printc_init "$OPT_COLOR";;
never|false) OPT_COLOR=false; printc_init "$OPT_COLOR";;
esac
};;
*) return 1;;
esac
}
# 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
# Specify paging.
--no-pager) shiftval; SCRIPT_PAGER_CMD='';;
--paging) shiftval; {
case "$OPT_VAL" in
auto) :;;
never) SCRIPT_PAGER_CMD='';;
always) :;;
esac
};;
# Specify the pager.
--pager) {
shiftval;
SCRIPT_PAGER_CMD=($OPT_VAL);
PAGER_ARGS=()
};;
*) return 1;;
esac
}
}

View File

@ -29,41 +29,6 @@ fi
# -----------------------------------------------------------------------------
# Parse arguments for the pager.
# This should be called inside the shiftopt/shiftval loop.
#
# Example:
# while shiftopt; do
# case "$OPT" in
# # ...
# esac
# pager_shiftopt && continue
# done
pager_shiftopt() {
case "$OPT" in
# Specify paging.
--no-pager) shiftval; SCRIPT_PAGER_CMD='';;
--paging) shiftval; {
case "$OPT_VAL" in
never) SCRIPT_PAGER_CMD='';;
always) :;;
auto) :;;
esac
};;
# Specify the pager.
--pager) {
shiftval;
SCRIPT_PAGER_CMD=($OPT_VAL);
PAGER_ARGS=()
};;
*) return 1;;
esac
return 0
}
# Executes a command or function, and pipes its output to the pager (if exists).
#
# Returns: The exit code of the command.

View File

@ -19,12 +19,12 @@ printc() {
# Initializes the color tags for printc.
#
# Arguments:
# color=on -- Turns on color output.
# color=off -- Turns off color output.
# true -- Turns on color output.
# false -- Turns off color output.
printc_init() {
case "$1" in
color=on) _PRINTC_PATTERN="$_PRINTC_PATTERN_ANSI";;
color=off) _PRINTC_PATTERN="$_PRINTC_PATTERN_PLAIN";;
true) _PRINTC_PATTERN="$_PRINTC_PATTERN_ANSI";;
false) _PRINTC_PATTERN="$_PRINTC_PATTERN_PLAIN";;
"") {
_PRINTC_PATTERN_ANSI=""

View File

@ -10,11 +10,16 @@ BAT="bat"
source "${LIB}/print.sh"
source "${LIB}/pager.sh"
source "${LIB}/opt.sh"
source "${LIB}/opt_hooks.sh"
source "${LIB}/version.sh"
# -----------------------------------------------------------------------------
# Init:
# -----------------------------------------------------------------------------
hook_color
hook_pager
# -----------------------------------------------------------------------------
# Options:
# -----------------------------------------------------------------------------
SEP="$(printc "%{DIM}%$(tput cols)s%{CLEAR}" | sed "s/ /─/g")"
RG_ARGS=()
BAT_ARGS=()
PATTERN=""
@ -24,7 +29,6 @@ OPT_CONTEXT_AFTER=2
OPT_FOLLOW=true
OPT_SNIP=""
OPT_HIGHLIGHT=true
OPT_COLOR=false
BAT_STYLE="header,numbers"
# Set options based on the bat version.
@ -32,11 +36,6 @@ if version_compare "$(bat_version)" -gt "0.12"; then
OPT_SNIP=",snip"
fi
# Set options based on tty.
if [[ -t 1 ]]; then
OPT_COLOR=true
fi
# Parse arguments.
while shiftopt; do
case "$OPT" in
@ -74,15 +73,9 @@ while shiftopt; do
--no-follow) OPT_FOLLOW=false;;
--no-snip) OPT_SNIP="";;
--no-highlight) OPT_HIGHLIGHT=false;;
--color) OPT_COLOR=true;;
--no-color) OPT_COLOR=false;;
# ???
-*) {
if pager_shiftopt; then
continue
fi
printc "%{RED}%s: unknown option '%s'%{CLEAR}\n" "$PROGRAM" "$OPT" 1>&2
exit 1
};;
@ -103,6 +96,9 @@ if [[ -z "$PATTERN" ]]; then
exit 1
fi
# Generate separator.
SEP="$(printc "%{DIM}%$(tput cols)s%{CLEAR}" | sed "s/ /─/g")"
# Append ripgrep and bat arguments.
if "$OPT_FOLLOW"; then
RG_ARGS+=("--follow")
@ -110,6 +106,8 @@ fi
if "$OPT_COLOR"; then
BAT_ARGS+=("--color=always")
else
BAT_ARGS+=("--color=never")
fi
if [[ "$OPT_CONTEXT_BEFORE" -eq 0 && "$OPT_CONTEXT_AFTER" -eq 0 ]]; then