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

@ -63,6 +63,7 @@ step_read() {
# The processed file contents.
step_preprocess() {
local line
local docvar
while IFS='' read -r line; do
# Skip certain lines.
[[ "$line" =~ ^LIB=.*$ ]] && continue
@ -75,7 +76,6 @@ step_preprocess() {
# Replace the DOCS_* variables.
if [[ "$line" =~ ^DOCS_[A-Z]+=.*$ ]]; then
local docvar
docvar="$(cut -d'=' -f1 <<<"$line")"
printf "%s=%q\n" "$docvar" "${!docvar}"
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"
})"
sed "s/:::/$(wc -c <<<"$wrapper" | bc)/" <<<"$wrapper"
echo "${wrapper/:::/$(wc -c <<<"$wrapper" | sed 's/^[[:space:]]*//')}"
gzip
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>"
while shiftopt; do
# shellcheck disable=SC2034
case "$OPT" in
--install) OPT_INSTALL=true ;;
--compress) OPT_COMPRESS=true ;;
--prefix)
shiftval
OPT_PREFIX="$OPT_VAL"
;;
--alternate-executable)
shiftval
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"
;;
--prefix) shiftval; OPT_PREFIX="$OPT_VAL" ;;
--alternate-executable) shiftval; 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"

View File

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

View File

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

View File

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

View File

@ -66,14 +66,10 @@ version_compare__recurse() {
case "$operator" in
-eq) [[ "$v_major" -ne "$c_major" ]] && return 1 ;;
-ne) [[ "$v_major" -ne "$c_major" ]] && return 0 ;;
-ge | -gt)
[[ "$v_major" -lt "$c_major" ]] && return 1
[[ "$v_major" -gt "$c_major" ]] && return 0
;;
-le | -lt)
[[ "$v_major" -gt "$c_major" ]] && return 1
[[ "$v_major" -lt "$c_major" ]] && return 0
;;
-ge | -gt) [[ "$v_major" -lt "$c_major" ]] && return 1
[[ "$v_major" -gt "$c_major" ]] && return 0 ;;
-le | -lt) [[ "$v_major" -gt "$c_major" ]] && return 1
[[ "$v_major" -lt "$c_major" ]] && return 0 ;;
esac
version_compare__recurse "$v_minor" "$operator" "$c_minor"

View File

@ -47,14 +47,9 @@ while shiftopt; do
-i | --ignore-case) OPT_CASE_SENSITIVITY="--ignore-case" ;;
-s | --case-sensitive) OPT_CASE_SENSITIVITY="--case-sensitive" ;;
-S | --smart-case) OPT_CASE_SENSITIVITY="--smart-case" ;;
-A | --after-context)
shiftval
OPT_CONTEXT_AFTER="$OPT_VAL"
;;
-B | --before-context)
shiftval
OPT_CONTEXT_BEFORE="$OPT_VAL"
;;
-A | --after-context) shiftval; OPT_CONTEXT_AFTER="$OPT_VAL" ;;
-B | --before-context) shiftval; OPT_CONTEXT_BEFORE="$OPT_VAL" ;;
-C | --context)
shiftval
OPT_CONTEXT_BEFORE="$OPT_VAL"
@ -74,7 +69,9 @@ while shiftopt; do
--multiline-dotall | \
--ignore | --no-ignore | \
--crlf | --no-crlf | \
--hidden | --no-hidden) RG_ARGS+=("$OPT") ;;
--hidden | --no-hidden)
RG_ARGS+=("$OPT")
;;
-E | --encoding | \
-g | --glob | \
@ -123,6 +120,7 @@ while shiftopt; do
FILES+=("$OPT")
fi
} ;;
esac
done
@ -168,11 +166,10 @@ if "$OPT_SEARCH_PATTERN"; then
SCRIPT_PAGER_ARGS+=(-p "$PATTERN")
fi
elif is_pager_disabled; then
print_error "$(
print_error "%s %s %s" \
"The -p/--search-pattern option requires a pager, but" \
"the pager was explicitly disabled by \$BAT_PAGER or the" \
"--paging option."
)"
exit 1
else
print_error "Unsupported pager '%s' for option -p/--search-pattern" \
@ -185,6 +182,7 @@ fi
# Main:
# -----------------------------------------------------------------------------
main() {
# shellcheck disable=SC2034
FOUND_FILES=()
FOUND=0
FIRST_PRINT=true
@ -212,6 +210,7 @@ main() {
echo "$SEP"
}
# shellcheck disable=SC2034
while IFS=':' read -r file line column; do
((FOUND++))

View File

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

View File

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

View File

@ -146,13 +146,13 @@ process_file() {
local ext="$2"
local fext="$ext"
local lang="${ext:1}"
local formatter
if [[ -n "$OPT_LANGUAGE" ]]; then
lang="$OPT_LANGUAGE"
fext="$(map_language_to_extension "$lang")"
fi
local formatter
formatter="$(map_extension_to_formatter "$fext")"
echo "FORMATTER >>> $formatter"
if [[ "$formatter" = "none" ]]; then
@ -168,17 +168,21 @@ process_file() {
local status
local data_raw
local data_formatted
# shellcheck disable=SC2094 disable=SC2181
if [[ "$file" = "-" ]]; then
data_raw="$(cat -)"
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_file --language="$lang" - <<<"$data_raw"
return 1
fi
else
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_file --language="$lang" "$file"
return 1
@ -201,15 +205,9 @@ while shiftopt; do
case "$OPT" in
# Language options
-l)
shiftval
OPT_LANGUAGE="${OPT_VAL}"
;;
-l) shiftval; OPT_LANGUAGE="${OPT_VAL}" ;;
-l*) OPT_LANGUAGE="${OPT:2}" ;;
--language)
shiftval
OPT_LANGUAGE="$OPT_VAL"
;;
--language) shiftval; OPT_LANGUAGE="$OPT_VAL" ;;
# bat options
-*) {
@ -237,4 +235,4 @@ for file in "${FILES[@]}"; do
done
# Exit.
exit "$EXIT"
exit "$FAIL"