Update all scripts to use constants, add --version support

This commit is contained in:
Ethan P 2020-04-08 02:56:17 -07:00
parent 0e18760ffc
commit a3d2df5927
No known key found for this signature in database
GPG Key ID: 6963FD04F6CF35EA
8 changed files with 56 additions and 3 deletions

18
lib/constants.sh Normal file
View File

@ -0,0 +1,18 @@
#!/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
# -----------------------------------------------------------------------------
EXECUTABLE_BAT="bat"
PROGRAM="$(basename "$0" .sh)"
PROGRAM_HOMEPAGE="https://github.com/eth-p/bat-extras"
PROGRAM_COPYRIGHT="Copyright (C) 2019-2020 eth-p | MIT License"
PROGRAM_VERSION="$({
TOP="$(dirname "$(dirname "${BASH_SOURCE[0]}")")"
printf "%s" "$(cat "${TOP}/version.txt" 2>/dev/null || echo "unknown")"
if [[ -e "${TOP}/.git" ]]; then
printf "%s-git (%s)" "" "$(git -C "${TOP}" rev-parse --short HEAD)"
fi
})"

View File

@ -5,7 +5,7 @@
# Repository: https://github.com/eth-p/bat-extras
# Issues: https://github.com/eth-p/bat-extras/issues
# -----------------------------------------------------------------------------
PROGRAM="$(basename "$0" .sh)"
source "${LIB}/constants.sh"
SHIFTOPT_HOOKS=()
# Sets the internal _ARGV, _ARGV_INDEX, and _ARGV_LAST variables used when

View File

@ -92,3 +92,21 @@ hook_pager() {
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

@ -8,6 +8,7 @@
# shellcheck disable=SC1090
LIB="$(cd "$(dirname "${BASH_SOURCE[0]}")" && cd "$(dirname "$(readlink "${BASH_SOURCE[0]}" || echo ".")")/../lib" && pwd)"
BAT="bat"
source "${LIB}/constants.sh"
source "${LIB}/print.sh"
source "${LIB}/pager.sh"
source "${LIB}/opt.sh"
@ -18,6 +19,7 @@ source "${LIB}/version.sh"
# -----------------------------------------------------------------------------
hook_color
hook_pager
hook_version
# -----------------------------------------------------------------------------
# Options:
# -----------------------------------------------------------------------------

View File

@ -9,6 +9,7 @@
LIB="$(cd "$(dirname "${BASH_SOURCE[0]}")" && cd "$(dirname "$(readlink "${BASH_SOURCE[0]}" || echo ".")")/../lib" && pwd)"
BAT="bat"
if [[ -n "${MANPAGER}" ]]; then BAT_PAGER="$MANPAGER"; fi
source "${LIB}/constants.sh"
source "${LIB}/pager.sh"
source "${LIB}/print.sh"
source "${LIB}/opt.sh"
@ -16,6 +17,7 @@ source "${LIB}/opt_hooks.sh"
# -----------------------------------------------------------------------------
hook_color
hook_pager
hook_version
# -----------------------------------------------------------------------------
MAN_ARGS=()
BAT_ARGS=()

View File

@ -8,7 +8,7 @@
# shellcheck disable=SC1090
LIB="$(cd "$(dirname "${BASH_SOURCE[0]}")" && cd "$(dirname "$(readlink "${BASH_SOURCE[0]}" || echo ".")")/../lib" && pwd)"
BAT="bat"
DOCS_URL="https://github.com/eth-p/bat-extras/blob/master/doc"
source "${LIB}/constants.sh"
source "${LIB}/opt.sh"
source "${LIB}/opt_hooks.sh"
source "${LIB}/print.sh"
@ -18,6 +18,7 @@ source "${LIB}/pager.sh"
# -----------------------------------------------------------------------------
hook_color
hook_pager
hook_version
# -----------------------------------------------------------------------------
# Watchers:
# -----------------------------------------------------------------------------
@ -209,7 +210,7 @@ fi
if [[ -z "$OPT_WATCHER" ]]; then
if ! OPT_WATCHER="$(determine_watcher)"; then
print_error "Your system does not have any supported watchers."
printc "Please read the documentation at %{BLUE}%s%{CLEAR} for more details.\n" "$DOCS_URL/batwatch.md" 1>&2
printc "Please read the documentation at %{BLUE}%s%{CLEAR} for more details.\n" "$PROGRAM_HOMEPAGE" 1>&2
exit 2
fi
else

View File

@ -8,10 +8,16 @@
# shellcheck disable=SC1090
LIB="$(cd "$(dirname "${BASH_SOURCE[0]}")" && cd "$(dirname "$(readlink "${BASH_SOURCE[0]}" || echo ".")")/../lib" && pwd)"
BAT="bat"
source "${LIB}/constants.sh"
source "${LIB}/opt.sh"
source "${LIB}/opt_hooks.sh"
source "${LIB}/str.sh"
source "${LIB}/print.sh"
# -----------------------------------------------------------------------------
# Init:
# -----------------------------------------------------------------------------
hook_version
# -----------------------------------------------------------------------------
# Formatters:
# -----------------------------------------------------------------------------

View File

@ -14,6 +14,12 @@ require_rg() {
fi
}
test:version() {
description "Test 'batgrep --version'"
snapshot stdout
snapshot stderr
}
test:regular_file() {
description "Search for a pattern in a regular file."
snapshot stdout