prettybat: Allow reading from pipe (#51)

This commit is contained in:
Ethan P 2021-04-06 15:45:30 -07:00
parent 6497bc04a0
commit 860638715a
No known key found for this signature in database
GPG Key ID: 6963FD04F6CF35EA
2 changed files with 23 additions and 3 deletions

View File

@ -181,8 +181,10 @@ process_file() {
local formatter
# Check that the file exists, and is a file.
check_exists "$file" || return 1
check_is_file "$file" || return 1
if [[ "$file" != "-" ]]; then
check_exists "$file" || return 1
check_is_file "$file" || return 1
fi
# Determine the formatter.
if [[ -n "$OPT_LANGUAGE" ]]; then
@ -190,7 +192,9 @@ process_file() {
fext="$(map_language_to_extension "$lang")"
fi
formatter="$(map_extension_to_formatter "$fext")"
if [[ "$ext" != "-" ]]; then
formatter="$(map_extension_to_formatter "$fext")"
fi
# Debug: Print the name and formatter.
if "$DEBUG_PRINT_FORMATTER"; then
@ -272,6 +276,9 @@ while shiftopt; do
# Debug options
--debug:formatter) DEBUG_PRINT_FORMATTER=true ;;
# Read from stdin
-) FILES+=("-") ;;
# bat options
-*) {
BAT_ARGS+=("$OPT=$OPT_VAL")

View File

@ -10,3 +10,16 @@ test:version() {
prettybat --version | awk 'FNR <= 1 { print $1 }'
prettybat --version | awk 'p{print} /^$/ { p=1 }'
}
test:read_from_pipe() {
description "Test 'prettybat -'"
assert_equal "ABC" "$(echo "ABC" | prettybat)"
assert_equal "ABC" "$(echo "ABC" | prettybat -)"
}
test:read_from_pipe_with_formatter() {
description "Test 'prettybat - -lsh'"
assert_equal "-: shfmt" "$(echo "" | prettybat - -lsh --debug:formatter)"
}