mirror of
https://github.com/eth-p/bat-extras.git
synced 2025-06-20 19:57:46 +02:00
Refactor common options into opt hooks
This commit is contained in:
parent
d776b7acbc
commit
9c10ef4871
12
lib/opt.sh
12
lib/opt.sh
@ -6,6 +6,7 @@
|
|||||||
# Issues: https://github.com/eth-p/bat-extras/issues
|
# Issues: https://github.com/eth-p/bat-extras/issues
|
||||||
# -----------------------------------------------------------------------------
|
# -----------------------------------------------------------------------------
|
||||||
PROGRAM="$(basename "$0" .sh)"
|
PROGRAM="$(basename "$0" .sh)"
|
||||||
|
SHIFTOPT_HOOKS=()
|
||||||
|
|
||||||
# Sets the internal _ARGV, _ARGV_INDEX, and _ARGV_LAST variables used when
|
# Sets the internal _ARGV, _ARGV_INDEX, and _ARGV_LAST variables used when
|
||||||
# parsing options with the shiftopt and shiftval functions.
|
# parsing options with the shiftopt and shiftval functions.
|
||||||
@ -42,6 +43,17 @@ shiftopt() {
|
|||||||
|
|
||||||
# Pop array.
|
# Pop array.
|
||||||
((_ARGV_INDEX++))
|
((_ARGV_INDEX++))
|
||||||
|
|
||||||
|
# Handle hooks.
|
||||||
|
local hook
|
||||||
|
for hook in "${SHIFTOPT_HOOKS[@]}"; do
|
||||||
|
if "$hook"; then
|
||||||
|
shiftopt
|
||||||
|
return $?
|
||||||
|
fi
|
||||||
|
done
|
||||||
|
|
||||||
|
# Return.
|
||||||
return 0
|
return 0
|
||||||
}
|
}
|
||||||
|
|
||||||
|
75
lib/opt_hooks.sh
Normal file
75
lib/opt_hooks.sh
Normal 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
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
35
lib/pager.sh
35
lib/pager.sh
@ -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).
|
# Executes a command or function, and pipes its output to the pager (if exists).
|
||||||
#
|
#
|
||||||
# Returns: The exit code of the command.
|
# Returns: The exit code of the command.
|
||||||
|
@ -19,12 +19,12 @@ printc() {
|
|||||||
# Initializes the color tags for printc.
|
# Initializes the color tags for printc.
|
||||||
#
|
#
|
||||||
# Arguments:
|
# Arguments:
|
||||||
# color=on -- Turns on color output.
|
# true -- Turns on color output.
|
||||||
# color=off -- Turns off color output.
|
# false -- Turns off color output.
|
||||||
printc_init() {
|
printc_init() {
|
||||||
case "$1" in
|
case "$1" in
|
||||||
color=on) _PRINTC_PATTERN="$_PRINTC_PATTERN_ANSI";;
|
true) _PRINTC_PATTERN="$_PRINTC_PATTERN_ANSI";;
|
||||||
color=off) _PRINTC_PATTERN="$_PRINTC_PATTERN_PLAIN";;
|
false) _PRINTC_PATTERN="$_PRINTC_PATTERN_PLAIN";;
|
||||||
|
|
||||||
"") {
|
"") {
|
||||||
_PRINTC_PATTERN_ANSI=""
|
_PRINTC_PATTERN_ANSI=""
|
||||||
|
@ -10,11 +10,16 @@ BAT="bat"
|
|||||||
source "${LIB}/print.sh"
|
source "${LIB}/print.sh"
|
||||||
source "${LIB}/pager.sh"
|
source "${LIB}/pager.sh"
|
||||||
source "${LIB}/opt.sh"
|
source "${LIB}/opt.sh"
|
||||||
|
source "${LIB}/opt_hooks.sh"
|
||||||
source "${LIB}/version.sh"
|
source "${LIB}/version.sh"
|
||||||
# -----------------------------------------------------------------------------
|
# -----------------------------------------------------------------------------
|
||||||
|
# Init:
|
||||||
|
# -----------------------------------------------------------------------------
|
||||||
|
hook_color
|
||||||
|
hook_pager
|
||||||
|
# -----------------------------------------------------------------------------
|
||||||
# Options:
|
# Options:
|
||||||
# -----------------------------------------------------------------------------
|
# -----------------------------------------------------------------------------
|
||||||
SEP="$(printc "%{DIM}%$(tput cols)s%{CLEAR}" | sed "s/ /─/g")"
|
|
||||||
RG_ARGS=()
|
RG_ARGS=()
|
||||||
BAT_ARGS=()
|
BAT_ARGS=()
|
||||||
PATTERN=""
|
PATTERN=""
|
||||||
@ -24,7 +29,6 @@ OPT_CONTEXT_AFTER=2
|
|||||||
OPT_FOLLOW=true
|
OPT_FOLLOW=true
|
||||||
OPT_SNIP=""
|
OPT_SNIP=""
|
||||||
OPT_HIGHLIGHT=true
|
OPT_HIGHLIGHT=true
|
||||||
OPT_COLOR=false
|
|
||||||
BAT_STYLE="header,numbers"
|
BAT_STYLE="header,numbers"
|
||||||
|
|
||||||
# Set options based on the bat version.
|
# Set options based on the bat version.
|
||||||
@ -32,11 +36,6 @@ if version_compare "$(bat_version)" -gt "0.12"; then
|
|||||||
OPT_SNIP=",snip"
|
OPT_SNIP=",snip"
|
||||||
fi
|
fi
|
||||||
|
|
||||||
# Set options based on tty.
|
|
||||||
if [[ -t 1 ]]; then
|
|
||||||
OPT_COLOR=true
|
|
||||||
fi
|
|
||||||
|
|
||||||
# Parse arguments.
|
# Parse arguments.
|
||||||
while shiftopt; do
|
while shiftopt; do
|
||||||
case "$OPT" in
|
case "$OPT" in
|
||||||
@ -74,15 +73,9 @@ while shiftopt; do
|
|||||||
--no-follow) OPT_FOLLOW=false;;
|
--no-follow) OPT_FOLLOW=false;;
|
||||||
--no-snip) OPT_SNIP="";;
|
--no-snip) OPT_SNIP="";;
|
||||||
--no-highlight) OPT_HIGHLIGHT=false;;
|
--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
|
printc "%{RED}%s: unknown option '%s'%{CLEAR}\n" "$PROGRAM" "$OPT" 1>&2
|
||||||
exit 1
|
exit 1
|
||||||
};;
|
};;
|
||||||
@ -103,6 +96,9 @@ if [[ -z "$PATTERN" ]]; then
|
|||||||
exit 1
|
exit 1
|
||||||
fi
|
fi
|
||||||
|
|
||||||
|
# Generate separator.
|
||||||
|
SEP="$(printc "%{DIM}%$(tput cols)s%{CLEAR}" | sed "s/ /─/g")"
|
||||||
|
|
||||||
# Append ripgrep and bat arguments.
|
# Append ripgrep and bat arguments.
|
||||||
if "$OPT_FOLLOW"; then
|
if "$OPT_FOLLOW"; then
|
||||||
RG_ARGS+=("--follow")
|
RG_ARGS+=("--follow")
|
||||||
@ -110,6 +106,8 @@ fi
|
|||||||
|
|
||||||
if "$OPT_COLOR"; then
|
if "$OPT_COLOR"; then
|
||||||
BAT_ARGS+=("--color=always")
|
BAT_ARGS+=("--color=always")
|
||||||
|
else
|
||||||
|
BAT_ARGS+=("--color=never")
|
||||||
fi
|
fi
|
||||||
|
|
||||||
if [[ "$OPT_CONTEXT_BEFORE" -eq 0 && "$OPT_CONTEXT_AFTER" -eq 0 ]]; then
|
if [[ "$OPT_CONTEXT_BEFORE" -eq 0 && "$OPT_CONTEXT_AFTER" -eq 0 ]]; then
|
||||||
|
Loading…
x
Reference in New Issue
Block a user