mirror of
https://github.com/eth-p/bat-extras.git
synced 2025-06-20 19:57:46 +02:00
Refactored prettybat to be more modular
This commit is contained in:
parent
6ae017829a
commit
16ea5cc401
185
src/prettybat.sh
185
src/prettybat.sh
@ -7,33 +7,141 @@
|
|||||||
# -----------------------------------------------------------------------------
|
# -----------------------------------------------------------------------------
|
||||||
LIB="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)/../lib"
|
LIB="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)/../lib"
|
||||||
source "${LIB}/opt.sh"
|
source "${LIB}/opt.sh"
|
||||||
|
source "${LIB}/str.sh"
|
||||||
|
source "${LIB}/print.sh"
|
||||||
# -----------------------------------------------------------------------------
|
# -----------------------------------------------------------------------------
|
||||||
|
|
||||||
|
# -----------------------------------------------------------------------------
|
||||||
|
# Formatters:
|
||||||
|
# -----------------------------------------------------------------------------
|
||||||
|
|
||||||
|
FORMATTERS=("prettier")
|
||||||
|
|
||||||
|
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
|
||||||
|
|
||||||
|
formatter_prettier_supports() {
|
||||||
|
case "$1" in
|
||||||
|
.js|.jsx|\
|
||||||
|
.ts|.tsx|\
|
||||||
|
.css|.scss|.sass|\
|
||||||
|
.html|\
|
||||||
|
.json|\
|
||||||
|
.md|\
|
||||||
|
.yml)
|
||||||
|
return 0;;
|
||||||
|
esac
|
||||||
|
|
||||||
|
return 1
|
||||||
|
}
|
||||||
|
|
||||||
|
formatter_prettier_process() {
|
||||||
|
prettier --stdin --stdin-filepath "$1" 2>/dev/null
|
||||||
|
return $?
|
||||||
|
}
|
||||||
|
|
||||||
|
# -----------------------------------------------------------------------------
|
||||||
|
# Functions:
|
||||||
|
# -----------------------------------------------------------------------------
|
||||||
|
|
||||||
|
# This function will map a bat `--language=...` argument into an appropriate
|
||||||
|
# file extension for the language provided. This must be hardcoded for
|
||||||
|
# performance reasons.
|
||||||
map_language_to_extension() {
|
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" ;;
|
||||||
esac
|
esac
|
||||||
|
|
||||||
echo "$ext"
|
echo "$ext"
|
||||||
}
|
}
|
||||||
|
|
||||||
|
# This function will map a file extension to a formatter.
|
||||||
|
# Formatters are defined higher up in the file.
|
||||||
|
map_extension_to_formatter() {
|
||||||
|
local formatter
|
||||||
|
for formatter in "${FORMATTERS[@]}"; do
|
||||||
|
if "formatter_${formatter}_supports" "$1"; then
|
||||||
|
echo "$formatter"
|
||||||
|
return 0
|
||||||
|
fi
|
||||||
|
done
|
||||||
|
|
||||||
|
echo "none"
|
||||||
|
return 0
|
||||||
|
}
|
||||||
|
|
||||||
|
extname() {
|
||||||
|
local file="$1"
|
||||||
|
echo ".${file##*.}"
|
||||||
|
}
|
||||||
|
|
||||||
|
print_file() {
|
||||||
|
if [[ "${#BAT_ARGS[@]}" -eq 0 ]]; then
|
||||||
|
bat "$@"
|
||||||
|
return $?
|
||||||
|
else
|
||||||
|
bat "${BAT_ARGS[@]}" "$@"
|
||||||
|
return $?
|
||||||
|
fi
|
||||||
|
}
|
||||||
|
|
||||||
|
process_file() {
|
||||||
|
local file="$1"
|
||||||
|
local ext="$2"
|
||||||
|
local lang="${ext:1}"
|
||||||
|
|
||||||
|
if [[ -n "$OPT_LANGUAGE" ]]; then
|
||||||
|
lang="$OPT_LANGUAGE"
|
||||||
|
fi
|
||||||
|
|
||||||
|
local formatter="$(map_extension_to_formatter "$ext")"
|
||||||
|
if [[ "$formatter" = "none" ]]; then
|
||||||
|
print_file "$file"
|
||||||
|
return $?
|
||||||
|
fi
|
||||||
|
|
||||||
|
# Prettify, then print.
|
||||||
|
local status
|
||||||
|
local data_raw
|
||||||
|
local data_formatted
|
||||||
|
if [[ "$file" = "-" ]]; then
|
||||||
|
data_raw="$(cat -)"
|
||||||
|
data_formatted="$("formatter_${formatter}_process" "$file" 2>/dev/null <<< "$data_raw")"
|
||||||
|
if [[ $? -ne 0 ]]; then
|
||||||
|
printc "{YELLOW}[%s warning]{CLEAR}: 'STDIN': Unable to format with '%s'" "$0" "$formatter" 1>&2
|
||||||
|
print_file --language="$lang" - <<< "$data_raw"
|
||||||
|
return 1
|
||||||
|
fi
|
||||||
|
else
|
||||||
|
data_formatted="$("formatter_${formatter}_process" "$file" < "$file")"
|
||||||
|
if [[ $? -ne 0 ]]; then
|
||||||
|
printc "{YELLOW}[%s warning]{CLEAR}: '%s': Unable to format with '%s'" "$0" "$file" "$formatter" 1>&2
|
||||||
|
print_file --language="$lang" "$file"
|
||||||
|
return 1
|
||||||
|
fi
|
||||||
|
fi
|
||||||
|
|
||||||
|
print_file --language="$lang" - <<< "$data_formatted"
|
||||||
|
return $?
|
||||||
|
}
|
||||||
|
|
||||||
|
# -----------------------------------------------------------------------------
|
||||||
|
# Main:
|
||||||
# -----------------------------------------------------------------------------
|
# -----------------------------------------------------------------------------
|
||||||
PRETTIER_ARGS=()
|
|
||||||
BAT_ARGS=()
|
BAT_ARGS=()
|
||||||
OPT_LANGUAGE=()
|
OPT_LANGUAGE=
|
||||||
FILES=()
|
FILES=()
|
||||||
|
|
||||||
# Parse arguments.
|
# Parse arguments.
|
||||||
@ -62,52 +170,11 @@ if [[ "${#FILES[@]}" -eq 0 ]]; then
|
|||||||
FILES="-"
|
FILES="-"
|
||||||
fi
|
fi
|
||||||
|
|
||||||
# Add arguments.
|
|
||||||
[[ -n "$OPT_LANGUAGE" ]] && BAT_ARGS+=("--language=$OPT_LANGUAGE") || true
|
|
||||||
|
|
||||||
# Handle input files.
|
# Handle input files.
|
||||||
prettify() {
|
FAIL=0
|
||||||
local status
|
|
||||||
local formatted
|
|
||||||
|
|
||||||
if [[ "$1" = "-" ]]; then
|
|
||||||
local data
|
|
||||||
data="$(cat -)"
|
|
||||||
formatted="$(prettier --stdin --stdin-filepath "stdin.$(map_language_to_extension "$OPT_LANGUAGE")" 2>/dev/null <<< "$data")"
|
|
||||||
if [[ $? -ne 0 ]]; then
|
|
||||||
echo "$data"
|
|
||||||
return 0
|
|
||||||
fi
|
|
||||||
else
|
|
||||||
formatted="$(prettier -- "$1" 2>/dev/null)"
|
|
||||||
status=$?
|
|
||||||
fi
|
|
||||||
|
|
||||||
echo "$formatted"
|
|
||||||
return $status
|
|
||||||
}
|
|
||||||
|
|
||||||
batify() {
|
|
||||||
if [[ "${#BAT_ARGS[@]}" -eq 0 ]]; then
|
|
||||||
bat "$@"
|
|
||||||
else
|
|
||||||
bat "${BAT_ARGS[@]}" "$@"
|
|
||||||
fi
|
|
||||||
}
|
|
||||||
|
|
||||||
EXIT=0
|
|
||||||
for file in "${FILES[@]}"; do
|
for file in "${FILES[@]}"; do
|
||||||
file_pretty="$(prettify "$file")"
|
if ! process_file "$file" "$(tolower "$(extname "$file")")"; then
|
||||||
if [[ $? -eq 0 ]]; then
|
FAIL=1
|
||||||
batify --language="${file##*.}" - <<< "$file_pretty"
|
|
||||||
exitcode=$?
|
|
||||||
else
|
|
||||||
batify "$file"
|
|
||||||
exitcode=$?
|
|
||||||
fi
|
|
||||||
|
|
||||||
if [[ $exitcode -ne 0 ]]; then
|
|
||||||
EXIT=$exitcode
|
|
||||||
fi
|
fi
|
||||||
done
|
done
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user