Fix small formatting and logic regressions

This commit is contained in:
Ethan P 2020-04-01 12:47:21 -07:00
parent f4765bfa54
commit 07c2cabc81
No known key found for this signature in database
GPG Key ID: 6963FD04F6CF35EA
9 changed files with 114 additions and 146 deletions

View File

@ -34,7 +34,7 @@ next() {
smsg() { smsg() {
case "$2" in case "$2" in
"SKIP") printc " %{YELLOW} %{DIM}%s [skipped]%{CLEAR}\n" "$1" 1>&2 ;; "SKIP") printc " %{YELLOW} %{DIM}%s [skipped]%{CLEAR}\n" "$1" 1>&2 ;;
*) printc " %{YELLOW} %s...%{CLEAR}\n" "$1" 1>&2 ;; *) printc " %{YELLOW} %s...%{CLEAR}\n" "$1" 1>&2 ;;
esac esac
} }
@ -63,6 +63,7 @@ step_read() {
# The processed file contents. # The processed file contents.
step_preprocess() { step_preprocess() {
local line local line
local docvar
while IFS='' read -r line; do while IFS='' read -r line; do
# Skip certain lines. # Skip certain lines.
[[ "$line" =~ ^LIB=.*$ ]] && continue [[ "$line" =~ ^LIB=.*$ ]] && continue
@ -75,7 +76,6 @@ step_preprocess() {
# Replace the DOCS_* variables. # Replace the DOCS_* variables.
if [[ "$line" =~ ^DOCS_[A-Z]+=.*$ ]]; then if [[ "$line" =~ ^DOCS_[A-Z]+=.*$ ]]; then
local docvar
docvar="$(cut -d'=' -f1 <<<"$line")" docvar="$(cut -d'=' -f1 <<<"$line")"
printf "%s=%q\n" "$docvar" "${!docvar}" printf "%s=%q\n" "$docvar" "${!docvar}"
continue continue
@ -143,7 +143,7 @@ step_compress() {
printf "(exec -a \"\$0\" bash -c 'eval \"\$(cat <&3)\"' \"\$0\" \"\$@\" 3< <(dd bs=1 if=\"\$0\" skip=::: 2>/dev/null | gunzip)); exit \$?;\n" printf "(exec -a \"\$0\" bash -c 'eval \"\$(cat <&3)\"' \"\$0\" \"\$@\" 3< <(dd bs=1 if=\"\$0\" skip=::: 2>/dev/null | gunzip)); exit \$?;\n"
})" })"
sed "s/:::/$(wc -c <<<"$wrapper" | bc)/" <<<"$wrapper" echo "${wrapper/:::/$(wc -c <<<"$wrapper" | sed 's/^[[:space:]]*//')}"
gzip gzip
smsg "Compressing" smsg "Compressing"
} }
@ -234,33 +234,16 @@ DOCS_URL="https://github.com/eth-p/bat-extras/blob/master/doc"
DOCS_MAINTAINER="eth-p <eth-p@hidden.email>" DOCS_MAINTAINER="eth-p <eth-p@hidden.email>"
while shiftopt; do while shiftopt; do
# shellcheck disable=SC2034
case "$OPT" in case "$OPT" in
--install) OPT_INSTALL=true ;; --install) OPT_INSTALL=true ;;
--compress) OPT_COMPRESS=true ;; --compress) OPT_COMPRESS=true ;;
--prefix) --prefix) shiftval; OPT_PREFIX="$OPT_VAL" ;;
shiftval --alternate-executable) shiftval; OPT_BAT="$OPT_VAL" ;;
OPT_PREFIX="$OPT_VAL" --minify) shiftval; OPT_MINIFY="$OPT_VAL" ;;
;; --no-verify) shiftval; OPT_VERIFY=false ;;
--alternate-executable) --docs:url) shiftval; DOCS_URL="$OPT_VAL" ;;
shiftval --docs:maintainer) shiftval; DOCS_MAINTAINER="$OPT_VAL" ;;
OPT_BAT="$OPT_VAL"
;;
--minify)
shiftval
OPT_MINIFY="$OPT_VAL"
;;
--no-verify)
shiftval
OPT_VERIFY=false
;;
--docs:url)
shiftval
DOCS_URL="$OPT_VAL"
;;
--docs:maintainer)
shiftval
DOCS_MAINTAINER="$OPT_VAL"
;;
*) *)
printc "%{RED}%s: unknown option '%s'%{CLEAR}" "$PROGRAM" "$OPT" printc "%{RED}%s: unknown option '%s'%{CLEAR}" "$PROGRAM" "$OPT"

View File

@ -20,26 +20,22 @@ hook_color() {
SHIFTOPT_HOOKS+=("__shiftopt_hook__color") SHIFTOPT_HOOKS+=("__shiftopt_hook__color")
__shiftopt_hook__color() { __shiftopt_hook__color() {
case "$OPT" in case "$OPT" in
--no-color)
OPT_COLOR=false --no-color) OPT_COLOR=false ;;
printc_init "$OPT_COLOR"
;;
--color) { --color) {
case "$OPT_VAL" in case "$OPT_VAL" in
auto) : ;; always | true) OPT_COLOR=true ;;
always | true) never | false) OPT_COLOR=false ;;
OPT_COLOR=true
printc_init "$OPT_COLOR" auto) return 0 ;;
;;
never | false)
OPT_COLOR=false
printc_init "$OPT_COLOR"
;;
esac esac
} ;; } ;;
*) return 1 ;; *) return 1 ;;
esac esac
printc_init "$OPT_COLOR"
return 0
} }
# Default color support. # Default color support.
@ -59,27 +55,29 @@ hook_pager() {
SHIFTOPT_HOOKS+=("__shiftopt_hook__pager") SHIFTOPT_HOOKS+=("__shiftopt_hook__pager")
__shiftopt_hook__pager() { __shiftopt_hook__pager() {
case "$OPT" in case "$OPT" in
# Specify paging. # Specify paging.
--no-pager) --no-pager) shiftval; SCRIPT_PAGER_CMD='' ;;
--paging) {
shiftval shiftval
SCRIPT_PAGER_CMD=''
;; case "$OPT_VAL" in
--paging) auto) : ;;
shiftval always) : ;;
{ never) SCRIPT_PAGER_CMD='' ;;
case "$OPT_VAL" in esac
auto) : ;; } ;;
never) SCRIPT_PAGER_CMD='' ;;
always) : ;;
esac
}
;;
# Specify the pager. # Specify the pager.
--pager) { --pager) {
shiftval shiftval
SCRIPT_PAGER_CMD=($OPT_VAL)
PAGER_ARGS=() # [note]: These are both intentional.
# shellcheck disable=SC2034 disable=SC2206
{
SCRIPT_PAGER_CMD=($OPT_VAL)
PAGER_ARGS=()
}
} ;; } ;;
*) return 1 ;; *) return 1 ;;

View File

@ -77,6 +77,8 @@ fi
# Prefer the bat pager. # Prefer the bat pager.
if [[ -n "${BAT_PAGER+x}" ]]; then if [[ -n "${BAT_PAGER+x}" ]]; then
# [note]: This is intentional.
# shellcheck disable=SC2206
SCRIPT_PAGER_CMD=($BAT_PAGER) SCRIPT_PAGER_CMD=($BAT_PAGER)
SCRIPT_PAGER_ARGS=() SCRIPT_PAGER_ARGS=()
fi fi

View File

@ -23,10 +23,10 @@ printc() {
# false -- Turns off color output. # false -- Turns off color output.
printc_init() { printc_init() {
case "$1" in case "$1" in
true) _PRINTC_PATTERN="$_PRINTC_PATTERN_ANSI" ;; true) _PRINTC_PATTERN="$_PRINTC_PATTERN_ANSI" ;;
false) _PRINTC_PATTERN="$_PRINTC_PATTERN_PLAIN" ;; false) _PRINTC_PATTERN="$_PRINTC_PATTERN_PLAIN" ;;
"") { "[DEFINE]") {
_PRINTC_PATTERN_ANSI="" _PRINTC_PATTERN_ANSI=""
_PRINTC_PATTERN_PLAIN="" _PRINTC_PATTERN_PLAIN=""
@ -37,7 +37,7 @@ printc_init() {
continue continue
fi fi
ansi="$(sed 's/\\/\\\\/' <<<"$ansi")" ansi="${ansi/\\/\\\\}"
_PRINTC_PATTERN_PLAIN="${_PRINTC_PATTERN_PLAIN}s/%{${name}}//g;" _PRINTC_PATTERN_PLAIN="${_PRINTC_PATTERN_PLAIN}s/%{${name}}//g;"
_PRINTC_PATTERN_ANSI="${_PRINTC_PATTERN_ANSI}s/%{${name}}/${ansi}/g;" _PRINTC_PATTERN_ANSI="${_PRINTC_PATTERN_ANSI}s/%{${name}}/${ansi}/g;"
@ -71,7 +71,7 @@ print_error() {
# ----------------------------------------------------------------------------- # -----------------------------------------------------------------------------
# Initialization: # Initialization:
# ----------------------------------------------------------------------------- # -----------------------------------------------------------------------------
printc_init <<END printc_init "[DEFINE]" <<END
CLEAR \x1B[0m CLEAR \x1B[0m
RED \x1B[31m RED \x1B[31m
GREEN \x1B[32m GREEN \x1B[32m

View File

@ -64,16 +64,12 @@ version_compare__recurse() {
# Compare the versions. # Compare the versions.
# This is an early escape case. # This is an early escape case.
case "$operator" in case "$operator" in
-eq) [[ "$v_major" -ne "$c_major" ]] && return 1 ;; -eq) [[ "$v_major" -ne "$c_major" ]] && return 1 ;;
-ne) [[ "$v_major" -ne "$c_major" ]] && return 0 ;; -ne) [[ "$v_major" -ne "$c_major" ]] && return 0 ;;
-ge | -gt) -ge | -gt) [[ "$v_major" -lt "$c_major" ]] && return 1
[[ "$v_major" -lt "$c_major" ]] && return 1 [[ "$v_major" -gt "$c_major" ]] && return 0 ;;
[[ "$v_major" -gt "$c_major" ]] && return 0 -le | -lt) [[ "$v_major" -gt "$c_major" ]] && return 1
;; [[ "$v_major" -lt "$c_major" ]] && return 0 ;;
-le | -lt)
[[ "$v_major" -gt "$c_major" ]] && return 1
[[ "$v_major" -lt "$c_major" ]] && return 0
;;
esac esac
version_compare__recurse "$v_minor" "$operator" "$c_minor" version_compare__recurse "$v_minor" "$operator" "$c_minor"

View File

@ -44,17 +44,12 @@ while shiftopt; do
case "$OPT" in case "$OPT" in
# ripgrep options # ripgrep options
-i | --ignore-case) OPT_CASE_SENSITIVITY="--ignore-case" ;; -i | --ignore-case) OPT_CASE_SENSITIVITY="--ignore-case" ;;
-s | --case-sensitive) OPT_CASE_SENSITIVITY="--case-sensitive" ;; -s | --case-sensitive) OPT_CASE_SENSITIVITY="--case-sensitive" ;;
-S | --smart-case) OPT_CASE_SENSITIVITY="--smart-case" ;; -S | --smart-case) OPT_CASE_SENSITIVITY="--smart-case" ;;
-A | --after-context) -A | --after-context) shiftval; OPT_CONTEXT_AFTER="$OPT_VAL" ;;
shiftval -B | --before-context) shiftval; OPT_CONTEXT_BEFORE="$OPT_VAL" ;;
OPT_CONTEXT_AFTER="$OPT_VAL"
;;
-B | --before-context)
shiftval
OPT_CONTEXT_BEFORE="$OPT_VAL"
;;
-C | --context) -C | --context)
shiftval shiftval
OPT_CONTEXT_BEFORE="$OPT_VAL" OPT_CONTEXT_BEFORE="$OPT_VAL"
@ -67,23 +62,25 @@ while shiftopt; do
;; ;;
-U | --multiline | \ -U | --multiline | \
-P | --pcre2 | \ -P | --pcre2 | \
-z | --search-zip | \ -z | --search-zip | \
-w | --word-regexp | \ -w | --word-regexp | \
--one-file-system | \ --one-file-system | \
--multiline-dotall | \ --multiline-dotall | \
--ignore | --no-ignore | \ --ignore | --no-ignore | \
--crlf | --no-crlf | \ --crlf | --no-crlf | \
--hidden | --no-hidden) RG_ARGS+=("$OPT") ;; --hidden | --no-hidden)
RG_ARGS+=("$OPT")
;;
-E | --encoding | \ -E | --encoding | \
-g | --glob | \ -g | --glob | \
-t | --type | \ -t | --type | \
-T | --type-not | \ -T | --type-not | \
-m | --max-count | \ -m | --max-count | \
--max-depth | \ --max-depth | \
--iglob | \ --iglob | \
--ignore-file) --ignore-file)
shiftval shiftval
RG_ARGS+=("$OPT" "$OPT_VAL") RG_ARGS+=("$OPT" "$OPT_VAL")
;; ;;
@ -91,11 +88,11 @@ while shiftopt; do
# bat options # bat options
# Script options # Script options
--no-follow) OPT_FOLLOW=false ;; --no-follow) OPT_FOLLOW=false ;;
--no-snip) OPT_SNIP="" ;; --no-snip) OPT_SNIP="" ;;
--no-highlight) OPT_HIGHLIGHT=false ;; --no-highlight) OPT_HIGHLIGHT=false ;;
-p | --search-pattern) OPT_SEARCH_PATTERN=true ;; -p | --search-pattern) OPT_SEARCH_PATTERN=true ;;
--no-search-pattern) OPT_SEARCH_PATTERN=false ;; --no-search-pattern) OPT_SEARCH_PATTERN=false ;;
# Option forwarding # Option forwarding
--rg:*) { --rg:*) {
@ -123,6 +120,7 @@ while shiftopt; do
FILES+=("$OPT") FILES+=("$OPT")
fi fi
} ;; } ;;
esac esac
done done
@ -168,11 +166,10 @@ if "$OPT_SEARCH_PATTERN"; then
SCRIPT_PAGER_ARGS+=(-p "$PATTERN") SCRIPT_PAGER_ARGS+=(-p "$PATTERN")
fi fi
elif is_pager_disabled; then elif is_pager_disabled; then
print_error "$( print_error "%s %s %s" \
"The -p/--search-pattern option requires a pager, but" \ "The -p/--search-pattern option requires a pager, but" \
"the pager was explicitly disabled by \$BAT_PAGER or the" \ "the pager was explicitly disabled by \$BAT_PAGER or the" \
"--paging option." "--paging option."
)"
exit 1 exit 1
else else
print_error "Unsupported pager '%s' for option -p/--search-pattern" \ print_error "Unsupported pager '%s' for option -p/--search-pattern" \
@ -185,6 +182,7 @@ fi
# Main: # Main:
# ----------------------------------------------------------------------------- # -----------------------------------------------------------------------------
main() { main() {
# shellcheck disable=SC2034
FOUND_FILES=() FOUND_FILES=()
FOUND=0 FOUND=0
FIRST_PRINT=true FIRST_PRINT=true
@ -212,6 +210,7 @@ main() {
echo "$SEP" echo "$SEP"
} }
# shellcheck disable=SC2034
while IFS=':' read -r file line column; do while IFS=':' read -r file line column; do
((FOUND++)) ((FOUND++))

View File

@ -1,4 +1,5 @@
#!/usr/bin/env bash #!/usr/bin/env bash
# shellcheck disable=SC2155
# ----------------------------------------------------------------------------- # -----------------------------------------------------------------------------
# bat-extras | Copyright (C) 2019 eth-p | MIT License # bat-extras | Copyright (C) 2019 eth-p | MIT License
# #
@ -26,13 +27,11 @@ else
BAT_ARGS=("--color=never --decorations=never") BAT_ARGS=("--color=never --decorations=never")
fi fi
# ----------------------------------------------------------------------------- # -----------------------------------------------------------------------------
export MANPAGER export MANPAGER='sh -c "col -bx | '"$(printf "%q" "$BAT")"' --language=man --style=grid '"${BAT_ARGS[*]}"'"'
MANPAGER='sh -c "col -bx | '"$(printf "%q" "$BAT")"' --language=man --style=grid '"${BAT_ARGS[*]}"'"'
export MANROFFOPT='-c' export MANROFFOPT='-c'
if [[ -n "${SCRIPT_PAGER_CMD}" ]]; then if [[ -n "${SCRIPT_PAGER_CMD}" ]]; then
export BAT_PAGER export BAT_PAGER="$(printf "%q " "${SCRIPT_PAGER_CMD[@]}" "${SCRIPT_PAGER_ARGS[@]}")"
BAT_PAGER="$(printf "%q " "${SCRIPT_PAGER_CMD[@]}" "${SCRIPT_PAGER_ARGS[@]}")"
else else
unset BAT_PAGER unset BAT_PAGER
fi fi

View File

@ -162,16 +162,10 @@ while shiftopt; do
case "$OPT" in case "$OPT" in
# Script options # Script options
--watcher) --watcher) shiftval; OPT_WATCHER="$OPT_VAL" ;;
shiftval --clear) OPT_CLEAR=true ;;
OPT_WATCHER="$OPT_VAL" --no-clear) OPT_CLEAR=false ;;
;; --terminal-width) shiftval; TERM_WIDTH="$OPT_VAL" ;;
--clear) OPT_CLEAR=true ;;
--no-clear) OPT_CLEAR=false ;;
--terminal-width)
shiftval
TERM_WIDTH="$OPT_VAL"
;;
# bat/Pager options # bat/Pager options
-*) BAT_ARGS+=("$OPT=$OPT_VAL") ;; -*) BAT_ARGS+=("$OPT=$OPT_VAL") ;;
@ -212,8 +206,7 @@ fi
# ----------------------------------------------------------------------------- # -----------------------------------------------------------------------------
# Determine the watcher. # Determine the watcher.
if [[ -z "$OPT_WATCHER" ]]; then if [[ -z "$OPT_WATCHER" ]]; then
OPT_WATCHER="$(determine_watcher)" if ! OPT_WATCHER="$(determine_watcher)"; then
if ! "$OPT_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" "$DOCS_URL/batwatch.md" 1>&2
exit 2 exit 2

View File

@ -93,19 +93,19 @@ map_language_to_extension() {
local ext=".txt" local ext=".txt"
case "$1" in case "$1" in
sh | bash) ext=".sh" ;; sh | bash) ext=".sh" ;;
js | es6 | es) ext=".js" ;; js | es6 | es) ext=".js" ;;
jsx) ext=".jsx" ;; jsx) ext=".jsx" ;;
ts) ext=".ts" ;; ts) ext=".ts" ;;
tsx) ext=".tsx" ;; tsx) ext=".tsx" ;;
css) ext=".css" ;; css) ext=".css" ;;
scss) ext=".scss" ;; scss) ext=".scss" ;;
sass) ext=".sass" ;; sass) ext=".sass" ;;
html | htm | shtml | xhtml) ext=".html" ;; html | htm | shtml | xhtml) ext=".html" ;;
json) ext=".json" ;; json) ext=".json" ;;
md | mdown | markdown) ext=".md" ;; md | mdown | markdown) ext=".md" ;;
yaml | yml) ext=".yml" ;; yaml | yml) ext=".yml" ;;
rust | rs) ext=".rs" ;; rust | rs) ext=".rs" ;;
esac esac
echo "$ext" echo "$ext"
@ -146,13 +146,13 @@ process_file() {
local ext="$2" local ext="$2"
local fext="$ext" local fext="$ext"
local lang="${ext:1}" local lang="${ext:1}"
local formatter
if [[ -n "$OPT_LANGUAGE" ]]; then if [[ -n "$OPT_LANGUAGE" ]]; then
lang="$OPT_LANGUAGE" lang="$OPT_LANGUAGE"
fext="$(map_language_to_extension "$lang")" fext="$(map_language_to_extension "$lang")"
fi fi
local formatter
formatter="$(map_extension_to_formatter "$fext")" formatter="$(map_extension_to_formatter "$fext")"
echo "FORMATTER >>> $formatter" echo "FORMATTER >>> $formatter"
if [[ "$formatter" = "none" ]]; then if [[ "$formatter" = "none" ]]; then
@ -168,17 +168,21 @@ process_file() {
local status local status
local data_raw local data_raw
local data_formatted local data_formatted
# shellcheck disable=SC2094 disable=SC2181
if [[ "$file" = "-" ]]; then if [[ "$file" = "-" ]]; then
data_raw="$(cat -)" data_raw="$(cat -)"
data_formatted="$("formatter_${formatter}_process" "$file" 2>/dev/null <<<"$data_raw")" data_formatted="$("formatter_${formatter}_process" "$file" 2>/dev/null <<<"$data_raw")"
if "$data_formatted"; then
if [[ $? -ne 0 ]]; then
print_warning "'STDIN': Unable to format with '%s'" "$formatter" print_warning "'STDIN': Unable to format with '%s'" "$formatter"
print_file --language="$lang" - <<<"$data_raw" print_file --language="$lang" - <<<"$data_raw"
return 1 return 1
fi fi
else else
data_formatted="$("formatter_${formatter}_process" "$file" <"$file")" data_formatted="$("formatter_${formatter}_process" "$file" <"$file")"
if ! "$data_formatted"; then
if [[ $? -ne 0 ]]; then
print_warning "'%s': Unable to format with '%s'" "$file" "$formatter" print_warning "'%s': Unable to format with '%s'" "$file" "$formatter"
print_file --language="$lang" "$file" print_file --language="$lang" "$file"
return 1 return 1
@ -201,15 +205,9 @@ while shiftopt; do
case "$OPT" in case "$OPT" in
# Language options # Language options
-l) -l) shiftval; OPT_LANGUAGE="${OPT_VAL}" ;;
shiftval -l*) OPT_LANGUAGE="${OPT:2}" ;;
OPT_LANGUAGE="${OPT_VAL}" --language) shiftval; OPT_LANGUAGE="$OPT_VAL" ;;
;;
-l*) OPT_LANGUAGE="${OPT:2}" ;;
--language)
shiftval
OPT_LANGUAGE="$OPT_VAL"
;;
# bat options # bat options
-*) { -*) {
@ -237,4 +235,4 @@ for file in "${FILES[@]}"; do
done done
# Exit. # Exit.
exit "$EXIT" exit "$FAIL"