From a3d2df592754546a8bf46f2a915c9c7ae8d83ca1 Mon Sep 17 00:00:00 2001 From: Ethan P Date: Wed, 8 Apr 2020 02:56:17 -0700 Subject: [PATCH] Update all scripts to use constants, add --version support --- lib/constants.sh | 18 ++++++++++++++++++ lib/opt.sh | 2 +- lib/opt_hooks.sh | 18 ++++++++++++++++++ src/batgrep.sh | 2 ++ src/batman.sh | 2 ++ src/batwatch.sh | 5 +++-- src/prettybat.sh | 6 ++++++ test/suite/batgrep.sh | 6 ++++++ 8 files changed, 56 insertions(+), 3 deletions(-) create mode 100644 lib/constants.sh diff --git a/lib/constants.sh b/lib/constants.sh new file mode 100644 index 0000000..fa0c3b5 --- /dev/null +++ b/lib/constants.sh @@ -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 +})" diff --git a/lib/opt.sh b/lib/opt.sh index f913567..ce0657c 100644 --- a/lib/opt.sh +++ b/lib/opt.sh @@ -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 diff --git a/lib/opt_hooks.sh b/lib/opt_hooks.sh index 9865ed1..d370ebe 100644 --- a/lib/opt_hooks.sh +++ b/lib/opt_hooks.sh @@ -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 + } +} diff --git a/src/batgrep.sh b/src/batgrep.sh index 4b45083..475e39d 100755 --- a/src/batgrep.sh +++ b/src/batgrep.sh @@ -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: # ----------------------------------------------------------------------------- diff --git a/src/batman.sh b/src/batman.sh index ee518bb..a0799ab 100755 --- a/src/batman.sh +++ b/src/batman.sh @@ -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=() diff --git a/src/batwatch.sh b/src/batwatch.sh index 2c09ca7..dd516c0 100755 --- a/src/batwatch.sh +++ b/src/batwatch.sh @@ -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 diff --git a/src/prettybat.sh b/src/prettybat.sh index e9bd1d9..73faba9 100755 --- a/src/prettybat.sh +++ b/src/prettybat.sh @@ -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: # ----------------------------------------------------------------------------- diff --git a/test/suite/batgrep.sh b/test/suite/batgrep.sh index 6915b9b..9a2d5aa 100644 --- a/test/suite/batgrep.sh +++ b/test/suite/batgrep.sh @@ -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