Add "poll" watcher to batwatch

This commit is contained in:
Ethan P 2019-10-25 13:50:10 -07:00
parent a0fcb97ff9
commit 80a07980aa
No known key found for this signature in database
GPG Key ID: 6963FD04F6CF35EA

View File

@ -21,7 +21,7 @@ hook_pager
# Watchers: # Watchers:
# ----------------------------------------------------------------------------- # -----------------------------------------------------------------------------
WATCHERS=("entr") WATCHERS=("entr" "poll")
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
@ -46,6 +46,87 @@ watcher_entr_supported() {
} }
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
POLL_STAT_VARIANT=''
POLL_STAT_COMMAND=()
determine_stat_variant() {
if [[ -n "$POLL_STAT_VARIANT" ]]; then
return 0
fi
# Try GNU stat.
if stat -c '%z' "$0" &>/dev/null; then
POLL_STAT_COMMAND=(stat -c '%z')
POLL_STAT_VARIANT='gnu'
return 0
fi
# Try BSD stat.
if stat -f '%m' "$0" &>/dev/null; then
POLL_STAT_COMMAND=(stat -f '%m')
POLL_STAT_VARIANT='bsd'
return 0
fi
return 1
}
watcher_poll_watch() {
determine_stat_variant
local files=("$@")
local times=()
# Get the initial modified times.
local file
local time
local modified=true
for file in "${files[@]}"; do
time="$("${POLL_STAT_COMMAND[@]}" "$file")"
times+=("$time")
done
# Display files.
while true; do
if "$modified"; then
modified=false
if [[ "$OPT_CLEAR" = "true" ]]; then
clear
fi
"$BAT" "${BAT_ARGS[@]}" \
--terminal-width="$TERM_WIDTH" \
--paging=never \
"${files[@]}"
fi
local i=0
for file in "${files[@]}"; do
time="$("${POLL_STAT_COMMAND[@]}" "$file")"
if [[ "$time" -ne "${times[$i]}" ]]; then
times[$i]="$time"
modified=true
fi
((i++))
done
sleep 1
done
"${POLL_STAT_COMMAND[@]}" "$@"
local ts
}
watcher_poll_supported() {
determine_stat_variant
return $?
}
# ----------------------------------------------------------------------------- # -----------------------------------------------------------------------------
# Functions: # Functions:
# ----------------------------------------------------------------------------- # -----------------------------------------------------------------------------
@ -132,11 +213,16 @@ if [[ -z "$OPT_WATCHER" ]]; then
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" "$DOCS_URL/batwatch.md" 1>&2
exit 2 exit 2
fi fi
fi else
if ! type "watcher_${OPT_WATCHER}_supported" &>/dev/null; then
printc "%{RED}[%s error]%{CLEAR}: Unknown watcher: '%s'\n" "$PROGRAM" "$OPT_WATCHER" 1>&2
exit 1
fi
if ! type "watcher_${OPT_WATCHER}_supported" &>/dev/null; then if ! "watcher_${OPT_WATCHER}_supported" &>/dev/null; then
printc "%{RED}[%s error]%{CLEAR}: Unknown watcher: '%s'\n" "$PROGRAM" "$OPT_WATCHER" 1>&2 printc "%{RED}[%s error]%{CLEAR}: Unsupported watcher: '%s'\n" "$PROGRAM" "$OPT_WATCHER" 1>&2
exit 1 exit 1
fi
fi fi
# Run the main function. # Run the main function.