Format with vs-shell-format

This commit is contained in:
Éfrit 2020-03-31 23:51:00 +02:00
parent 30f5508bc3
commit 73aafa56e1
16 changed files with 306 additions and 255 deletions

1
.gitignore vendored
View File

@ -10,4 +10,3 @@
/.circleci/.config.yml /.circleci/.config.yml
.download .download
bin bin

5
.gitmodules vendored
View File

@ -1,4 +1,3 @@
[submodule "best"] [submodule "best"]
path = .test-framework path = .test-framework
url = https://github.com/eth-p/best.git url = https://github.com/eth-p/best.git

View File

@ -33,8 +33,8 @@ next() {
# Prints a build step message. # Prints a build step message.
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
} }
@ -58,7 +58,7 @@ step_read() {
# #
# Input: # Input:
# The original file contents. # The original file contents.
# #
# Output: # Output:
# The processed file contents. # The processed file contents.
step_preprocess() { step_preprocess() {
@ -66,7 +66,7 @@ step_preprocess() {
while IFS='' read -r line; do while IFS='' read -r line; do
# Skip certain lines. # Skip certain lines.
[[ "$line" =~ ^LIB=.*$ ]] && continue [[ "$line" =~ ^LIB=.*$ ]] && continue
# Replace the BAT variable with the build option. # Replace the BAT variable with the build option.
if [[ "$line" =~ ^BAT=.*$ ]]; then if [[ "$line" =~ ^BAT=.*$ ]]; then
printf "BAT=%q\n" "$OPT_BAT" printf "BAT=%q\n" "$OPT_BAT"
@ -75,7 +75,7 @@ step_preprocess() {
# Replace the DOCS_* variables. # Replace the DOCS_* variables.
if [[ "$line" =~ ^DOCS_[A-Z]+=.*$ ]]; then if [[ "$line" =~ ^DOCS_[A-Z]+=.*$ ]]; then
local docvar="$(cut -d'=' -f1 <<< "$line")" local docvar="$(cut -d'=' -f1 <<<"$line")"
printf "%s=%q\n" "$docvar" "${!docvar}" printf "%s=%q\n" "$docvar" "${!docvar}"
continue continue
fi fi
@ -97,7 +97,7 @@ step_preprocess() {
# Forward data. # Forward data.
echo "$line" echo "$line"
done done
smsg "Preprocessing" smsg "Preprocessing"
} }
@ -141,7 +141,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" sed "s/:::/$(wc -c <<<"$wrapper" | bc)/" <<<"$wrapper"
gzip gzip
smsg "Compressing" smsg "Compressing"
} }
@ -233,17 +233,37 @@ DOCS_MAINTAINER="eth-p <eth-p@hidden.email>"
while shiftopt; do while shiftopt; do
case "$OPT" in case "$OPT" in
--install) OPT_INSTALL=true;; --install) OPT_INSTALL=true ;;
--compress) OPT_COMPRESS=true;; --compress) OPT_COMPRESS=true ;;
--prefix) shiftval; OPT_PREFIX="$OPT_VAL";; --prefix)
--alternate-executable) shiftval; OPT_BAT="$OPT_VAL";; shiftval
--minify) shiftval; OPT_MINIFY="$OPT_VAL";; OPT_PREFIX="$OPT_VAL"
--no-verify) shiftval; OPT_VERIFY=false;; ;;
--docs:url) shiftval; DOCS_URL="$OPT_VAL";; --alternate-executable)
--docs:maintainer) shiftval; DOCS_MAINTAINER="$OPT_VAL";; shiftval
OPT_BAT="$OPT_VAL"
*) printc "%{RED}%s: unknown option '%s'%{CLEAR}" "$PROGRAM" "$OPT"; ;;
exit 1;; --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"
exit 1
;;
esac esac
done done
@ -290,17 +310,17 @@ printc "%{YELLOW}Building scripts...%{CLEAR}\n" 1>&2
file_i=0 file_i=0
file_n="${#SOURCES[@]}" file_n="${#SOURCES[@]}"
for file in "${SOURCES[@]}"; do for file in "${SOURCES[@]}"; do
((file_i++)) || true; ((file_i++)) || true
filename="$(basename "$file" .sh)" filename="$(basename "$file" .sh)"
printc " %{YELLOW}[%s/%s] %{MAGENTA}%s%{CLEAR}\n" "$file_i" "$file_n" "$file" 1>&2 printc " %{YELLOW}[%s/%s] %{MAGENTA}%s%{CLEAR}\n" "$file_i" "$file_n" "$file" 1>&2
step_read "$file" |\ step_read "$file" |
next step_preprocess |\ next step_preprocess |
next step_minify |\ next step_minify |
next step_compress |\ next step_compress |
next step_write "${BIN}/${filename}" |\ next step_write "${BIN}/${filename}" |
next step_write_install "${OPT_PREFIX}/bin/${filename}" |\ next step_write_install "${OPT_PREFIX}/bin/${filename}" |
cat >/dev/null cat >/dev/null
done done
@ -312,4 +332,3 @@ if "$OPT_VERIFY"; then
"${HERE}/test.sh" --compiled "${HERE}/test.sh" --compiled
exit $? exit $?
fi fi

View File

@ -11,13 +11,13 @@ SHIFTOPT_HOOKS=()
# Sets the internal _ARGV, _ARGV_INDEX, and _ARGV_LAST variables used when # Sets the internal _ARGV, _ARGV_INDEX, and _ARGV_LAST variables used when
# parsing options with the shiftopt and shiftval functions. # parsing options with the shiftopt and shiftval functions.
setargs() { setargs() {
_ARGV=("$@") _ARGV=("$@")
_ARGV_LAST="$((${#_ARGV[@]} - 1))" _ARGV_LAST="$((${#_ARGV[@]} - 1))"
_ARGV_INDEX=0 _ARGV_INDEX=0
} }
# Gets the next option passed to the script. # Gets the next option passed to the script.
# #
# Variables: # Variables:
# OPT -- The option name. # OPT -- The option name.
# #
@ -35,7 +35,7 @@ shiftopt() {
[[ "$_ARGV_INDEX" -gt "$_ARGV_LAST" ]] && return 1 [[ "$_ARGV_INDEX" -gt "$_ARGV_LAST" ]] && return 1
OPT="${_ARGV[$_ARGV_INDEX]}" OPT="${_ARGV[$_ARGV_INDEX]}"
unset OPT_VAL unset OPT_VAL
if [[ "$OPT" =~ ^--[a-zA-Z0-9_-]+=.* ]]; then if [[ "$OPT" =~ ^--[a-zA-Z0-9_-]+=.* ]]; then
OPT_VAL="${OPT#*=}" OPT_VAL="${OPT#*=}"
OPT="${OPT%%=*}" OPT="${OPT%%=*}"
@ -53,7 +53,6 @@ shiftopt() {
fi fi
done done
# Return.
return 0 return 0
} }
@ -81,7 +80,5 @@ shiftval() {
fi fi
} }
# ----------------------------------------------------------------------------- # -----------------------------------------------------------------------------
setargs "$@" setargs "$@"

View File

@ -6,13 +6,12 @@
# Issues: https://github.com/eth-p/bat-extras/issues # Issues: https://github.com/eth-p/bat-extras/issues
# ----------------------------------------------------------------------------- # -----------------------------------------------------------------------------
# ----------------------------------------------------------------------------- # -----------------------------------------------------------------------------
# Hooks: # Hooks:
# ----------------------------------------------------------------------------- # -----------------------------------------------------------------------------
# Option parser hook: color support. # Option parser hook: color support.
# This will accept --no-color, or --color. # This will accept --no-color or --color.
# It will also try to accept --color=never|always|auto. # It will also try to accept --color=never|always|auto.
# #
# The variable OPT_COLOR will be set depending on whether or not a TTY is # The variable OPT_COLOR will be set depending on whether or not a TTY is
@ -21,16 +20,25 @@ 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; printc_init "$OPT_COLOR";; --no-color)
--color) { OPT_COLOR=false
case "$OPT_VAL" in printc_init "$OPT_COLOR"
auto) :;; ;;
always|true) OPT_COLOR=true; printc_init "$OPT_COLOR";; --color) {
never|false) OPT_COLOR=false; printc_init "$OPT_COLOR";; case "$OPT_VAL" in
esac auto) : ;;
};; always | true)
OPT_COLOR=true
printc_init "$OPT_COLOR"
;;
never | false)
OPT_COLOR=false
printc_init "$OPT_COLOR"
;;
esac
} ;;
*) return 1;; *) return 1 ;;
esac esac
} }
@ -51,25 +59,30 @@ 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) shiftval; SCRIPT_PAGER_CMD='';; --no-pager)
--paging) shiftval; { shiftval
SCRIPT_PAGER_CMD=''
;;
--paging)
shiftval
{
case "$OPT_VAL" in case "$OPT_VAL" in
auto) :;; auto) : ;;
never) SCRIPT_PAGER_CMD='';; never) SCRIPT_PAGER_CMD='' ;;
always) :;; always) : ;;
esac esac
};; }
;;
# Specify the pager. # Specify the pager.
--pager) { --pager) {
shiftval; shiftval
SCRIPT_PAGER_CMD=($OPT_VAL); SCRIPT_PAGER_CMD=($OPT_VAL)
PAGER_ARGS=() PAGER_ARGS=()
};; } ;;
*) return 1;; *) return 1 ;;
esac esac
} }
} }

View File

@ -6,13 +6,13 @@
# Issues: https://github.com/eth-p/bat-extras/issues # Issues: https://github.com/eth-p/bat-extras/issues
# ----------------------------------------------------------------------------- # -----------------------------------------------------------------------------
# Returns 0 (true) if the current pager is less, otherwise 1 (false) # Returns 0 (true) if the current pager is less, otherwise 1 (false).
is_pager_less() { is_pager_less() {
[[ "$(pager_name)" = "less" ]] [[ "$(pager_name)" = "less" ]]
return $? return $?
} }
# Returns 0 (true) if the current pager is disabled, otherwise 1 (false) # Returns 0 (true) if the current pager is disabled, otherwise 1 (false).
is_pager_disabled() { is_pager_disabled() {
[[ -z "$(pager_name)" ]] [[ -z "$(pager_name)" ]]
return $? return $?
@ -24,7 +24,7 @@ pager_name() {
if [[ -z "$_SCRIPT_PAGER_NAME" ]]; then if [[ -z "$_SCRIPT_PAGER_NAME" ]]; then
local output="$("${SCRIPT_PAGER_CMD[0]}" --version 2>&1)" local output="$("${SCRIPT_PAGER_CMD[0]}" --version 2>&1)"
if head -n 1 <<< "$output" | grep '^less \d' &>/dev/null; then if head -n 1 <<<"$output" | grep '^less \d' &>/dev/null; then
_SCRIPT_PAGER_NAME="less" _SCRIPT_PAGER_NAME="less"
else else
_SCRIPT_PAGER_NAME="$(basename "${SCRIPT_PAGER_CMD[0]}")" _SCRIPT_PAGER_NAME="$(basename "${SCRIPT_PAGER_CMD[0]}")"
@ -34,7 +34,7 @@ pager_name() {
echo "$_SCRIPT_PAGER_NAME" echo "$_SCRIPT_PAGER_NAME"
} }
# Executes a command or function, and pipes its output to the pager (if exists). # Executes a command or function, and pipes its output to the pager (if it exists).
# #
# Returns: The exit code of the command. # Returns: The exit code of the command.
# Example: # Example:
@ -51,7 +51,7 @@ pager_exec() {
fi fi
} }
# Displays the output of a command or function inside the pager (if exists). # Displays the output of a command or function inside the pager (if it exists).
# #
# Example: # Example:
# bat | pager_display # bat | pager_display
@ -85,4 +85,3 @@ if ! [[ -t 1 ]]; then
SCRIPT_PAGER_CMD=() SCRIPT_PAGER_CMD=()
SCRIPT_PAGER_ARGS=() SCRIPT_PAGER_ARGS=()
fi fi

View File

@ -13,7 +13,7 @@
# printc "%{RED}This is red %s.%{CLEAR}\n" "text" # printc "%{RED}This is red %s.%{CLEAR}\n" "text"
# #
printc() { printc() {
printf "$(sed "$_PRINTC_PATTERN" <<< "$1")" "${@:2}" printf "$(sed "$_PRINTC_PATTERN" <<<"$1")" "${@:2}"
} }
# Initializes the color tags for printc. # Initializes the color tags for printc.
@ -23,36 +23,36 @@ 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" ;;
"") { "") {
_PRINTC_PATTERN_ANSI="" _PRINTC_PATTERN_ANSI=""
_PRINTC_PATTERN_PLAIN="" _PRINTC_PATTERN_PLAIN=""
local name local name
local ansi local ansi
while read -r name ansi; do while read -r name ansi; do
if [[ -z "${name}" && -z "${ansi}" ]] || [[ "${name:0:1}" = "#" ]]; then if [[ -z "${name}" && -z "${ansi}" ]] || [[ "${name:0:1}" = "#" ]]; then
continue continue
fi
ansi="$(sed 's/\\/\\\\/' <<< "$ansi")"
_PRINTC_PATTERN_PLAIN="${_PRINTC_PATTERN_PLAIN}s/%{${name}}//g;"
_PRINTC_PATTERN_ANSI="${_PRINTC_PATTERN_ANSI}s/%{${name}}/${ansi}/g;"
done
if [ -t 1 ]; then
_PRINTC_PATTERN="$_PRINTC_PATTERN_ANSI"
else
_PRINTC_PATTERN="$_PRINTC_PATTERN_PLAIN"
fi fi
};;
ansi="$(sed 's/\\/\\\\/' <<<"$ansi")"
_PRINTC_PATTERN_PLAIN="${_PRINTC_PATTERN_PLAIN}s/%{${name}}//g;"
_PRINTC_PATTERN_ANSI="${_PRINTC_PATTERN_ANSI}s/%{${name}}/${ansi}/g;"
done
if [ -t 1 ]; then
_PRINTC_PATTERN="$_PRINTC_PATTERN_ANSI"
else
_PRINTC_PATTERN="$_PRINTC_PATTERN_PLAIN"
fi
} ;;
esac esac
} }
# Print a warning message to STDERR. # Print a warning message to stderr.
# Arguments: # Arguments:
# 1 -- The printc formatting string. # 1 -- The printc formatting string.
# ... -- The printc formatting arguments. # ... -- The printc formatting arguments.
@ -60,7 +60,7 @@ print_warning() {
printc "%{YELLOW}[%s warning]%{CLEAR}: $1%{CLEAR}\n" "$PROGRAM" "${@:2}" 1>&2 printc "%{YELLOW}[%s warning]%{CLEAR}: $1%{CLEAR}\n" "$PROGRAM" "${@:2}" 1>&2
} }
# Print an error message to STDERR. # Print an error message to stderr.
# Arguments: # Arguments:
# 1 -- The printc formatting string. # 1 -- The printc formatting string.
# ... -- The printc formatting arguments. # ... -- The printc formatting arguments.
@ -82,4 +82,3 @@ printc_init <<END
DIM \x1B[2m DIM \x1B[2m
END END

View File

@ -8,11 +8,10 @@
# Converts a string to lower case. # Converts a string to lower case.
tolower() { tolower() {
tr "[[:upper:]]" "[[:lower:]]" <<< "$1" tr "[[:upper:]]" "[[:lower:]]" <<<"$1"
} }
# Converts a string to upper case. # Converts a string to upper case.
toupper() { toupper() {
tr "[[:lower:]]" "[[:upper:]]" <<< "$1" tr "[[:lower:]]" "[[:upper:]]" <<<"$1"
} }

View File

@ -49,7 +49,7 @@ version_compare__recurse() {
# Compare the versions specially if the final number has been reached. # Compare the versions specially if the final number has been reached.
if [[ -z "$v_minor" && -z "$c_minor" ]]; then if [[ -z "$v_minor" && -z "$c_minor" ]]; then
[ "$v_major" $operator "$c_major" ]; [ "$v_major" $operator "$c_major" ];
return $?; return $?
fi fi
# Insert zeroes where there are missing numbers. # Insert zeroes where there are missing numbers.
@ -64,15 +64,17 @@ 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) [[ "$v_major" -lt "$c_major" ]] && return 1; -ge | -gt)
[[ "$v_major" -gt "$c_major" ]] && return 0;; [[ "$v_major" -lt "$c_major" ]] && return 1
-le|-lt) [[ "$v_major" -gt "$c_major" ]] && return 1; [[ "$v_major" -gt "$c_major" ]] && return 0
[[ "$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

@ -43,71 +43,86 @@ fi
while shiftopt; do 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) shiftval; OPT_CONTEXT_AFTER="$OPT_VAL";; -A | --after-context)
-B|--before-context) shiftval; OPT_CONTEXT_BEFORE="$OPT_VAL";; shiftval
-C|--context) shiftval; OPT_CONTEXT_BEFORE="$OPT_VAL"; OPT_CONTEXT_AFTER="$OPT_VAL"
OPT_CONTEXT_AFTER="$OPT_VAL";; ;;
-B | --before-context)
shiftval
OPT_CONTEXT_BEFORE="$OPT_VAL"
;;
-C | --context)
shiftval
OPT_CONTEXT_BEFORE="$OPT_VAL"
OPT_CONTEXT_AFTER="$OPT_VAL"
;;
-F|--fixed-strings) OPT_FIXED_STRINGS=true; RG_ARGS+=("$OPT");; -F | --fixed-strings)
OPT_FIXED_STRINGS=true
RG_ARGS+=("$OPT")
;;
-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) shiftval; RG_ARGS+=("$OPT" "$OPT_VAL");; --ignore-file)
shiftval
RG_ARGS+=("$OPT" "$OPT_VAL")
;;
# 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:*) {
if [[ "${OPT:5:1}" = "-" ]]; then if [[ "${OPT:5:1}" = "-" ]]; then
RG_ARGS+=("${OPT:5}") RG_ARGS+=("${OPT:5}")
else else
RG_ARGS+=("--${OPT:5}") RG_ARGS+=("--${OPT:5}")
fi fi
if [[ -n "$OPT_VAL" ]]; then if [[ -n "$OPT_VAL" ]]; then
RG_ARGS+=("$OPT_VAL") RG_ARGS+=("$OPT_VAL")
fi fi
};; } ;;
# ??? # ???
-*) { -*) {
printc "%{RED}%s: unknown option '%s'%{CLEAR}\n" "$PROGRAM" "$OPT" 1>&2 printc "%{RED}%s: unknown option '%s'%{CLEAR}\n" "$PROGRAM" "$OPT" 1>&2
exit 1 exit 1
};; } ;;
# Search # Search
*) { *) {
if [ -z "$PATTERN" ]; then if [ -z "$PATTERN" ]; then
PATTERN="$OPT" PATTERN="$OPT"
else else
FILES+=("$OPT") FILES+=("$OPT")
fi fi
};; } ;;
esac esac
done done
@ -126,7 +141,7 @@ if [[ -n "$OPT_CASE_SENSITIVITY" ]]; then
fi fi
if "$OPT_FOLLOW"; then if "$OPT_FOLLOW"; then
RG_ARGS+=("--follow") RG_ARGS+=("--follow")
fi fi
if "$OPT_COLOR"; then if "$OPT_COLOR"; then
@ -155,13 +170,13 @@ if "$OPT_SEARCH_PATTERN"; then
elif is_pager_disabled; then elif is_pager_disabled; then
print_error "$( print_error "$(
echo "The -p/--search-pattern option requires a pager, but" \ echo "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" \
"$(pager_name)" "$(pager_name)"
exit 1 exit 1
fi fi
fi fi
@ -186,12 +201,12 @@ main() {
# Print the file. # Print the file.
"$BAT" "${BAT_ARGS[@]}" \ "$BAT" "${BAT_ARGS[@]}" \
"${LAST_LR[@]}" \ "${LAST_LR[@]}" \
"${LAST_LH[@]}" \ "${LAST_LH[@]}" \
--style="${BAT_STYLE}${OPT_SNIP}" \ --style="${BAT_STYLE}${OPT_SNIP}" \
--paging=never \ --paging=never \
--terminal-width="$COLS" \ --terminal-width="$COLS" \
"$LAST_FILE" "$LAST_FILE"
# Print the separator. # Print the separator.
echo "$SEP" echo "$SEP"
@ -206,7 +221,7 @@ main() {
LAST_LR=() LAST_LR=()
LAST_LH=() LAST_LH=()
fi fi
# Calculate the context line numbers. # Calculate the context line numbers.
line_start=$((line - OPT_CONTEXT_BEFORE)) line_start=$((line - OPT_CONTEXT_BEFORE))
line_end=$((line + OPT_CONTEXT_AFTER)) line_end=$((line + OPT_CONTEXT_AFTER))
@ -225,4 +240,3 @@ main() {
pager_exec main pager_exec main
exit $? exit $?

View File

@ -37,4 +37,3 @@ fi
command man "${MAN_ARGS[@]}" command man "${MAN_ARGS[@]}"
exit $? exit $?

View File

@ -23,7 +23,7 @@ hook_pager
WATCHERS=("entr" "poll") WATCHERS=("entr" "poll")
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
watcher_entr_watch() { watcher_entr_watch() {
ENTR_ARGS=() ENTR_ARGS=()
@ -45,8 +45,7 @@ watcher_entr_supported() {
return $? return $?
} }
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
POLL_STAT_VARIANT='' POLL_STAT_VARIANT=''
POLL_STAT_COMMAND=() POLL_STAT_COMMAND=()
@ -96,7 +95,7 @@ watcher_poll_watch() {
if [[ "$OPT_CLEAR" = "true" ]]; then if [[ "$OPT_CLEAR" = "true" ]]; then
clear clear
fi fi
"$BAT" "${BAT_ARGS[@]}" \ "$BAT" "${BAT_ARGS[@]}" \
--terminal-width="$TERM_WIDTH" \ --terminal-width="$TERM_WIDTH" \
--paging=never \ --paging=never \
@ -106,7 +105,7 @@ watcher_poll_watch() {
local i=0 local i=0
for file in "${files[@]}"; do for file in "${files[@]}"; do
time="$("${POLL_STAT_COMMAND[@]}" "$file")" time="$("${POLL_STAT_COMMAND[@]}" "$file")"
if [[ "$time" -ne "${times[$i]}" ]]; then if [[ "$time" -ne "${times[$i]}" ]]; then
times[$i]="$time" times[$i]="$time"
modified=true modified=true
@ -139,7 +138,7 @@ determine_watcher() {
return 0 return 0
fi fi
done done
return 1 return 1
} }
@ -162,26 +161,32 @@ fi
while shiftopt; do while shiftopt; do
case "$OPT" in case "$OPT" in
# Script Options # Script options
--watcher) shiftval; OPT_WATCHER="$OPT_VAL";; --watcher)
--clear) OPT_CLEAR=true;; shiftval
--no-clear) OPT_CLEAR=false;; OPT_WATCHER="$OPT_VAL"
--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") ;;
# Files # Files
*) { *) {
FILES+=("$OPT") FILES+=("$OPT")
};; } ;;
esac esac
done done
if [[ -z "$FILES" ]]; then if [[ -z "$FILES" ]]; then
print_error "no files provided" print_error "no files provided"
exit 1 exit 1
fi fi
for file in "${FILES[@]}"; do for file in "${FILES[@]}"; do
@ -233,4 +238,3 @@ main() {
pager_exec main pager_exec main
exit $? exit $?

View File

@ -16,20 +16,21 @@ source "${LIB}/print.sh"
FORMATTERS=("prettier" "rustfmt" "shfmt" "clangformat") FORMATTERS=("prettier" "rustfmt" "shfmt" "clangformat")
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
formatter_prettier_supports() { formatter_prettier_supports() {
case "$1" in case "$1" in
.js|.jsx|\ .js | .jsx | \
.ts|.tsx|\ .ts | .tsx | \
.css|.scss|.sass|\ .css | .scss | .sass | \
.html|\ .html | \
.json|\ .json | \
.md|\ .md | \
.yml) .yml)
return 0;; return 0
;;
esac esac
return 1 return 1
} }
@ -38,16 +39,17 @@ formatter_prettier_process() {
return $? return $?
} }
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
formatter_clangformat_supports() { formatter_clangformat_supports() {
case "$1" in case "$1" in
.c|.cpp|.cxx|\ .c | .cpp | .cxx | \
.h|.hpp|\ .h | .hpp | \
.m) .m)
return 0;; return 0
;;
esac esac
return 1 return 1
} }
@ -56,7 +58,7 @@ formatter_clangformat_process() {
return $? return $?
} }
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
formatter_rustfmt_supports() { formatter_rustfmt_supports() {
[[ "$1" = ".rs" ]] [[ "$1" = ".rs" ]]
@ -68,7 +70,7 @@ formatter_rustfmt_process() {
return $? return $?
} }
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
formatter_shfmt_supports() { formatter_shfmt_supports() {
[[ "$1" = ".sh" ]] [[ "$1" = ".sh" ]]
@ -80,8 +82,6 @@ formatter_shfmt_process() {
return $? return $?
} }
# ----------------------------------------------------------------------------- # -----------------------------------------------------------------------------
# Functions: # Functions:
# ----------------------------------------------------------------------------- # -----------------------------------------------------------------------------
@ -93,21 +93,21 @@ 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"
} }
@ -121,7 +121,7 @@ map_extension_to_formatter() {
return 0 return 0
fi fi
done done
echo "none" echo "none"
return 0 return 0
} }
@ -146,7 +146,7 @@ process_file() {
local ext="$2" local ext="$2"
local fext="$ext" local fext="$ext"
local lang="${ext:1}" local lang="${ext:1}"
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")"
@ -169,14 +169,14 @@ process_file() {
local data_formatted local data_formatted
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 [[ $? -ne 0 ]]; 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 [[ $? -ne 0 ]]; 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"
@ -184,7 +184,7 @@ process_file() {
fi fi
fi fi
print_file --language="$lang" - <<< "$data_formatted" print_file --language="$lang" - <<<"$data_formatted"
return $? return $?
} }
@ -199,20 +199,26 @@ FILES=()
while shiftopt; do while shiftopt; do
case "$OPT" in case "$OPT" in
# Language Options # Language options
-l) shiftval; OPT_LANGUAGE="${OPT_VAL}" ;; -l)
-l*) OPT_LANGUAGE="${OPT:2}" ;; shiftval
--language) shiftval; OPT_LANGUAGE="$OPT_VAL" ;; OPT_LANGUAGE="${OPT_VAL}"
;;
-l*) OPT_LANGUAGE="${OPT:2}" ;;
--language)
shiftval
OPT_LANGUAGE="$OPT_VAL"
;;
# Bat Options # bat options
-*) { -*) {
BAT_ARGS+=("$OPT=$OPT_VAL") BAT_ARGS+=("$OPT=$OPT_VAL")
};; } ;;
# Files # Files
*) { *) {
FILES+=("$OPT") FILES+=("$OPT")
};; } ;;
esac esac
done done
@ -231,4 +237,3 @@ done
# Exit. # Exit.
exit $EXIT exit $EXIT

View File

@ -25,8 +25,11 @@ export SNAPSHOT_DIR="${HERE}/test/snapshot"
OPT_ARGV=() OPT_ARGV=()
while shiftopt; do while shiftopt; do
case "$OPT" in case "$OPT" in
--compiled) TEST_ENV_BIN_DIR="${HERE}/bin"; TEST_ENV_BIN_SUFFIX="" ;; --compiled)
*) OPT_ARGV+=("$OPT") ;; TEST_ENV_BIN_DIR="${HERE}/bin"
TEST_ENV_BIN_SUFFIX=""
;;
*) OPT_ARGV+=("$OPT") ;;
esac esac
done done

View File

@ -6,4 +6,4 @@ while read -d ':' -r dir; do
if [[ -f "${dir}/bat" ]]; then if [[ -f "${dir}/bat" ]]; then
exec "${dir}/bat" "$@" --theme="Monokai Extended" exec "${dir}/bat" "$@" --theme="Monokai Extended"
fi fi
done <<< "$PATH" done <<<"$PATH"

View File

@ -1,5 +1,5 @@
#!/usr/bin/env bash #!/usr/bin/env bash
case "$1" in case "$1" in
cols) echo 80 ;; cols) echo 80 ;;
lines) echo 22 ;; lines) echo 22 ;;
esac esac