mirror of
https://github.com/eth-p/bat-extras.git
synced 2025-02-14 16:19:12 +01:00
batwatch changes to reveal a bug and add --help
- Add `-h` and `--help` flags to print out basic usage. - Refactor determine_stat_variant so that it checks to make sure the value from `stat` is a epoch timestamp, and exit noisily if it is not. - Change `determine_watcher` to set the `OPT_WATCHER` global directory directly instead of echoing it then being called from a subshell. There was an errror that the subshell was obscuring. - Add a test to reflect the bug.
This commit is contained in:
parent
149f47fa66
commit
4c09718a46
@ -18,10 +18,25 @@ source "${LIB}/pager.sh"
|
|||||||
# -----------------------------------------------------------------------------
|
# -----------------------------------------------------------------------------
|
||||||
# Init:
|
# Init:
|
||||||
# -----------------------------------------------------------------------------
|
# -----------------------------------------------------------------------------
|
||||||
|
# Option parser hook: --help support.
|
||||||
|
# This will accept -h or --help, which prints the usage information and exits.
|
||||||
|
hook_help() {
|
||||||
|
SHIFTOPT_HOOKS+=("__shiftopt_hook__help")
|
||||||
|
__shiftopt_hook__help() {
|
||||||
|
if [[ "$OPT" = "--help" ]] || [[ "$OPT" = "-h" ]]; then
|
||||||
|
echo 'Usage: batwatch [--watcher entr|poll][--[no-]clear] <file> [<file> ...]'
|
||||||
|
exit 0
|
||||||
|
fi
|
||||||
|
|
||||||
|
return 1
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
hook_color
|
hook_color
|
||||||
hook_pager
|
hook_pager
|
||||||
hook_version
|
hook_version
|
||||||
hook_width
|
hook_width
|
||||||
|
hook_help
|
||||||
# -----------------------------------------------------------------------------
|
# -----------------------------------------------------------------------------
|
||||||
# Watchers:
|
# Watchers:
|
||||||
# -----------------------------------------------------------------------------
|
# -----------------------------------------------------------------------------
|
||||||
@ -60,19 +75,26 @@ determine_stat_variant() {
|
|||||||
return 0
|
return 0
|
||||||
fi
|
fi
|
||||||
|
|
||||||
# Try GNU stat.
|
local varient name cmd ts
|
||||||
if stat -c '%z' "$0" &>/dev/null; then
|
|
||||||
POLL_STAT_COMMAND=(stat -c '%z')
|
|
||||||
POLL_STAT_VARIANT='gnu'
|
|
||||||
return 0
|
|
||||||
fi
|
|
||||||
|
|
||||||
# Try BSD stat.
|
for varient in "gnu -c %z" "bsd -f %m"; do
|
||||||
if stat -f '%m' "$0" &>/dev/null; then
|
name="${varient%% *}" cmd="stat ${varient#* }"
|
||||||
POLL_STAT_COMMAND=(stat -f '%m')
|
|
||||||
POLL_STAT_VARIANT='bsd'
|
# keep the results of the stash command
|
||||||
return 0
|
if read -r ts < \
|
||||||
fi
|
<( ${cmd} "$0" 2>/dev/null ); then
|
||||||
|
|
||||||
|
# verify that the value is an epoch timetamp
|
||||||
|
# before proceeding
|
||||||
|
if [[ "${ts}" =~ ^[0-9]+$ ]]; then
|
||||||
|
|
||||||
|
POLL_STAT_COMMAND=( ${cmd} )
|
||||||
|
POLL_STAT_VARIANT="$name"
|
||||||
|
return 0
|
||||||
|
|
||||||
|
fi
|
||||||
|
fi
|
||||||
|
done
|
||||||
|
|
||||||
return 1
|
return 1
|
||||||
}
|
}
|
||||||
@ -139,7 +161,7 @@ determine_watcher() {
|
|||||||
local watcher
|
local watcher
|
||||||
for watcher in "${WATCHERS[@]}"; do
|
for watcher in "${WATCHERS[@]}"; do
|
||||||
if "watcher_${watcher}_supported"; then
|
if "watcher_${watcher}_supported"; then
|
||||||
echo "$watcher"
|
OPT_WATCHER="$watcher"
|
||||||
return 0
|
return 0
|
||||||
fi
|
fi
|
||||||
done
|
done
|
||||||
@ -153,6 +175,7 @@ determine_watcher() {
|
|||||||
BAT_ARGS=()
|
BAT_ARGS=()
|
||||||
FILES=()
|
FILES=()
|
||||||
FILES_HAS_DIRECTORY=false
|
FILES_HAS_DIRECTORY=false
|
||||||
|
OPT_HELP=false
|
||||||
OPT_CLEAR=true
|
OPT_CLEAR=true
|
||||||
OPT_WATCHER=""
|
OPT_WATCHER=""
|
||||||
|
|
||||||
@ -169,6 +192,7 @@ while shiftopt; do
|
|||||||
--watcher) shiftval; OPT_WATCHER="$OPT_VAL" ;;
|
--watcher) shiftval; OPT_WATCHER="$OPT_VAL" ;;
|
||||||
--clear) OPT_CLEAR=true ;;
|
--clear) OPT_CLEAR=true ;;
|
||||||
--no-clear) OPT_CLEAR=false ;;
|
--no-clear) OPT_CLEAR=false ;;
|
||||||
|
-h|--help) OPT_HELP=true ;;
|
||||||
|
|
||||||
# bat/Pager options
|
# bat/Pager options
|
||||||
-*) BAT_ARGS+=("$OPT=$OPT_VAL") ;;
|
-*) BAT_ARGS+=("$OPT=$OPT_VAL") ;;
|
||||||
@ -209,7 +233,7 @@ fi
|
|||||||
# -----------------------------------------------------------------------------
|
# -----------------------------------------------------------------------------
|
||||||
# Determine the watcher.
|
# Determine the watcher.
|
||||||
if [[ -z "$OPT_WATCHER" ]]; then
|
if [[ -z "$OPT_WATCHER" ]]; then
|
||||||
if ! OPT_WATCHER="$(determine_watcher)"; then
|
if ! 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" "$PROGRAM_HOMEPAGE" 1>&2
|
printc "Please read the documentation at %{BLUE}%s%{CLEAR} for more details.\n" "$PROGRAM_HOMEPAGE" 1>&2
|
||||||
exit 2
|
exit 2
|
||||||
@ -228,8 +252,8 @@ fi
|
|||||||
|
|
||||||
# Run the main function.
|
# Run the main function.
|
||||||
main() {
|
main() {
|
||||||
"watcher_${OPT_WATCHER}_watch" "${FILES[@]}"
|
"watcher_${OPT_WATCHER}_watch" "${FILES[@]}"
|
||||||
return $?
|
return $?
|
||||||
}
|
}
|
||||||
|
|
||||||
pager_exec main
|
pager_exec main
|
||||||
|
1
test/snapshot/batwatch/test_help.stdout.snapshot
Normal file
1
test/snapshot/batwatch/test_help.stdout.snapshot
Normal file
@ -0,0 +1 @@
|
|||||||
|
Usage: batwatch [--watcher entr|poll][--[no-]clear] <file> [<file> ...]
|
@ -1,12 +1,32 @@
|
|||||||
|
#!/bin/bash
|
||||||
|
|
||||||
setup() {
|
setup() {
|
||||||
use_shim 'batwatch'
|
use_shim 'batwatch'
|
||||||
}
|
}
|
||||||
|
|
||||||
test:version() {
|
test:version() {
|
||||||
description "Test 'batwatch --version'"
|
description "Test 'batwatch --version'"
|
||||||
snapshot stdout
|
snapshot stdout
|
||||||
snapshot stderr
|
snapshot stderr
|
||||||
|
|
||||||
batwatch --version | awk 'FNR <= 1 { print $1 }'
|
batwatch --version | awk 'FNR <= 1 { print $1 }'
|
||||||
batwatch --version | awk 'p{print} /^$/ { p=1 }'
|
batwatch --version | awk 'p{print} /^$/ { p=1 }'
|
||||||
}
|
}
|
||||||
|
|
||||||
|
test:help() {
|
||||||
|
description "Test 'batwatch --help'"
|
||||||
|
snapshot stdout
|
||||||
|
batwatch --help
|
||||||
|
|
||||||
|
assert batwatch --help
|
||||||
|
batwatch --help | grep -q 'Usage'
|
||||||
|
}
|
||||||
|
|
||||||
|
test:watcher() {
|
||||||
|
description "Test 'batwatch <file>'"
|
||||||
|
fail "No longer fails silently due to incorrect flag to stat."
|
||||||
|
|
||||||
|
# Refactored to get here, but when it passes it will loop forever.
|
||||||
|
|
||||||
|
assert batwatch $0
|
||||||
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user