Split hooks into separate files to reduce compiled size

This commit is contained in:
Ethan P 2020-04-08 03:19:58 -07:00
parent a91ee96e6a
commit 03971fe64e
No known key found for this signature in database
GPG Key ID: 6963FD04F6CF35EA
5 changed files with 126 additions and 106 deletions

50
lib/opt_hook_color.sh Normal file
View File

@ -0,0 +1,50 @@
#!/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
# -----------------------------------------------------------------------------
# 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 ;;
--color) {
case "$OPT_VAL" in
"") OPT_COLOR=true ;;
always | true) OPT_COLOR=true ;;
never | false) OPT_COLOR=false ;;
auto) return 0 ;;
*)
printc "%{RED}%s: '--color' expects value of 'auto', 'always', or 'never'%{CLEAR}\n" "$PROGRAM"
exit 1
;;
esac
} ;;
*) return 1 ;;
esac
printc_init "$OPT_COLOR"
return 0
}
# 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
}

47
lib/opt_hook_pager.sh Normal file
View File

@ -0,0 +1,47 @@
#!/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
# -----------------------------------------------------------------------------
# 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) : ;;
always) : ;;
never) SCRIPT_PAGER_CMD='' ;;
*)
printc "%{RED}%s: '--paging' expects value of 'auto', 'always', or 'never'%{CLEAR}\n" "$PROGRAM"
exit 1
;;
esac
} ;;
# Specify the pager.
--pager) {
shiftval
# [note]: These are both intentional.
# shellcheck disable=SC2034 disable=SC2206
{
SCRIPT_PAGER_CMD=($OPT_VAL)
PAGER_ARGS=()
}
} ;;
*) return 1 ;;
esac
}
}

25
lib/opt_hook_version.sh Normal file
View File

@ -0,0 +1,25 @@
#!/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
# -----------------------------------------------------------------------------
# Option parser hook: --version support.
# This will accept --version, which prints the version information and exits.
hook_version() {
SHIFTOPT_HOOKS+=("__shiftopt_hook__version")
__shiftopt_hook__version() {
if [[ "$OPT" = "--version" ]]; then
printf "%s %s\n\n%s\n%s\n" \
"$PROGRAM" \
"$PROGRAM_VERSION" \
"$PROGRAM_COPYRIGHT" \
"$PROGRAM_HOMEPAGE"
exit 0
fi
return 1
}
}

View File

@ -5,108 +5,6 @@
# Repository: https://github.com/eth-p/bat-extras # Repository: https://github.com/eth-p/bat-extras
# Issues: https://github.com/eth-p/bat-extras/issues # Issues: https://github.com/eth-p/bat-extras/issues
# ----------------------------------------------------------------------------- # -----------------------------------------------------------------------------
source "${LIB}/opt_hook_color.sh"
# ----------------------------------------------------------------------------- source "${LIB}/opt_hook_pager.sh"
# Hooks: source "${LIB}/opt_hook_version.sh"
# -----------------------------------------------------------------------------
# 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 ;;
--color) {
case "$OPT_VAL" in
"") OPT_COLOR=true ;;
always | true) OPT_COLOR=true ;;
never | false) OPT_COLOR=false ;;
auto) return 0 ;;
*)
printc "%{RED}%s: '--color' expects value of 'auto', 'always', or 'never'%{CLEAR}\n" "$PROGRAM"
exit 1
;;
esac
} ;;
*) return 1 ;;
esac
printc_init "$OPT_COLOR"
return 0
}
# 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) : ;;
always) : ;;
never) SCRIPT_PAGER_CMD='' ;;
*)
printc "%{RED}%s: '--paging' expects value of 'auto', 'always', or 'never'%{CLEAR}\n" "$PROGRAM"
exit 1
;;
esac
} ;;
# Specify the pager.
--pager) {
shiftval
# [note]: These are both intentional.
# shellcheck disable=SC2034 disable=SC2206
{
SCRIPT_PAGER_CMD=($OPT_VAL)
PAGER_ARGS=()
}
} ;;
*) return 1 ;;
esac
}
}
# Option parser hook: --version support.
# This will accept --version, which prints the version information and exits.
hook_version() {
SHIFTOPT_HOOKS+=("__shiftopt_hook__version")
__shiftopt_hook__version() {
if [[ "$OPT" = "--version" ]]; then
printf "%s %s\n\n%s\n%s\n" \
"$PROGRAM" \
"$PROGRAM_VERSION" \
"$PROGRAM_COPYRIGHT" \
"$PROGRAM_HOMEPAGE"
exit 0
fi
return 1
}
}

View File

@ -9,7 +9,7 @@
LIB="$(cd "$(dirname "${BASH_SOURCE[0]}")" && cd "$(dirname "$(readlink "${BASH_SOURCE[0]}" || echo ".")")/../lib" && pwd)" LIB="$(cd "$(dirname "${BASH_SOURCE[0]}")" && cd "$(dirname "$(readlink "${BASH_SOURCE[0]}" || echo ".")")/../lib" && pwd)"
source "${LIB}/constants.sh" source "${LIB}/constants.sh"
source "${LIB}/opt.sh" source "${LIB}/opt.sh"
source "${LIB}/opt_hooks.sh" source "${LIB}/opt_hook_version.sh"
source "${LIB}/str.sh" source "${LIB}/str.sh"
source "${LIB}/print.sh" source "${LIB}/print.sh"
# ----------------------------------------------------------------------------- # -----------------------------------------------------------------------------