mirror of
https://github.com/eth-p/bat-extras.git
synced 2025-02-01 18:09:11 +01:00
Update all scripts to use constants, add --version support
This commit is contained in:
parent
0e18760ffc
commit
a3d2df5927
18
lib/constants.sh
Normal file
18
lib/constants.sh
Normal 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
|
||||||
|
})"
|
@ -5,7 +5,7 @@
|
|||||||
# 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
|
||||||
# -----------------------------------------------------------------------------
|
# -----------------------------------------------------------------------------
|
||||||
PROGRAM="$(basename "$0" .sh)"
|
source "${LIB}/constants.sh"
|
||||||
SHIFTOPT_HOOKS=()
|
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
|
||||||
|
@ -92,3 +92,21 @@ hook_pager() {
|
|||||||
esac
|
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
|
||||||
|
}
|
||||||
|
}
|
||||||
|
@ -8,6 +8,7 @@
|
|||||||
# shellcheck disable=SC1090
|
# shellcheck disable=SC1090
|
||||||
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)"
|
||||||
BAT="bat"
|
BAT="bat"
|
||||||
|
source "${LIB}/constants.sh"
|
||||||
source "${LIB}/print.sh"
|
source "${LIB}/print.sh"
|
||||||
source "${LIB}/pager.sh"
|
source "${LIB}/pager.sh"
|
||||||
source "${LIB}/opt.sh"
|
source "${LIB}/opt.sh"
|
||||||
@ -18,6 +19,7 @@ source "${LIB}/version.sh"
|
|||||||
# -----------------------------------------------------------------------------
|
# -----------------------------------------------------------------------------
|
||||||
hook_color
|
hook_color
|
||||||
hook_pager
|
hook_pager
|
||||||
|
hook_version
|
||||||
# -----------------------------------------------------------------------------
|
# -----------------------------------------------------------------------------
|
||||||
# Options:
|
# Options:
|
||||||
# -----------------------------------------------------------------------------
|
# -----------------------------------------------------------------------------
|
||||||
|
@ -9,6 +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)"
|
||||||
BAT="bat"
|
BAT="bat"
|
||||||
if [[ -n "${MANPAGER}" ]]; then BAT_PAGER="$MANPAGER"; fi
|
if [[ -n "${MANPAGER}" ]]; then BAT_PAGER="$MANPAGER"; fi
|
||||||
|
source "${LIB}/constants.sh"
|
||||||
source "${LIB}/pager.sh"
|
source "${LIB}/pager.sh"
|
||||||
source "${LIB}/print.sh"
|
source "${LIB}/print.sh"
|
||||||
source "${LIB}/opt.sh"
|
source "${LIB}/opt.sh"
|
||||||
@ -16,6 +17,7 @@ source "${LIB}/opt_hooks.sh"
|
|||||||
# -----------------------------------------------------------------------------
|
# -----------------------------------------------------------------------------
|
||||||
hook_color
|
hook_color
|
||||||
hook_pager
|
hook_pager
|
||||||
|
hook_version
|
||||||
# -----------------------------------------------------------------------------
|
# -----------------------------------------------------------------------------
|
||||||
MAN_ARGS=()
|
MAN_ARGS=()
|
||||||
BAT_ARGS=()
|
BAT_ARGS=()
|
||||||
|
@ -8,7 +8,7 @@
|
|||||||
# shellcheck disable=SC1090
|
# shellcheck disable=SC1090
|
||||||
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)"
|
||||||
BAT="bat"
|
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.sh"
|
||||||
source "${LIB}/opt_hooks.sh"
|
source "${LIB}/opt_hooks.sh"
|
||||||
source "${LIB}/print.sh"
|
source "${LIB}/print.sh"
|
||||||
@ -18,6 +18,7 @@ source "${LIB}/pager.sh"
|
|||||||
# -----------------------------------------------------------------------------
|
# -----------------------------------------------------------------------------
|
||||||
hook_color
|
hook_color
|
||||||
hook_pager
|
hook_pager
|
||||||
|
hook_version
|
||||||
# -----------------------------------------------------------------------------
|
# -----------------------------------------------------------------------------
|
||||||
# Watchers:
|
# Watchers:
|
||||||
# -----------------------------------------------------------------------------
|
# -----------------------------------------------------------------------------
|
||||||
@ -209,7 +210,7 @@ fi
|
|||||||
if [[ -z "$OPT_WATCHER" ]]; then
|
if [[ -z "$OPT_WATCHER" ]]; then
|
||||||
if ! OPT_WATCHER="$(determine_watcher)"; then
|
if ! OPT_WATCHER="$(determine_watcher)"; then
|
||||||
print_error "Your system does not have any supported watchers."
|
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
|
exit 2
|
||||||
fi
|
fi
|
||||||
else
|
else
|
||||||
|
@ -8,10 +8,16 @@
|
|||||||
# shellcheck disable=SC1090
|
# shellcheck disable=SC1090
|
||||||
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)"
|
||||||
BAT="bat"
|
BAT="bat"
|
||||||
|
source "${LIB}/constants.sh"
|
||||||
source "${LIB}/opt.sh"
|
source "${LIB}/opt.sh"
|
||||||
|
source "${LIB}/opt_hooks.sh"
|
||||||
source "${LIB}/str.sh"
|
source "${LIB}/str.sh"
|
||||||
source "${LIB}/print.sh"
|
source "${LIB}/print.sh"
|
||||||
# -----------------------------------------------------------------------------
|
# -----------------------------------------------------------------------------
|
||||||
|
# Init:
|
||||||
|
# -----------------------------------------------------------------------------
|
||||||
|
hook_version
|
||||||
|
# -----------------------------------------------------------------------------
|
||||||
# Formatters:
|
# Formatters:
|
||||||
# -----------------------------------------------------------------------------
|
# -----------------------------------------------------------------------------
|
||||||
|
|
||||||
|
@ -14,6 +14,12 @@ require_rg() {
|
|||||||
fi
|
fi
|
||||||
}
|
}
|
||||||
|
|
||||||
|
test:version() {
|
||||||
|
description "Test 'batgrep --version'"
|
||||||
|
snapshot stdout
|
||||||
|
snapshot stderr
|
||||||
|
}
|
||||||
|
|
||||||
test:regular_file() {
|
test:regular_file() {
|
||||||
description "Search for a pattern in a regular file."
|
description "Search for a pattern in a regular file."
|
||||||
snapshot stdout
|
snapshot stdout
|
||||||
|
Loading…
Reference in New Issue
Block a user