From 860638715a58fb3284fff8bf09b1faec5178c572 Mon Sep 17 00:00:00 2001 From: Ethan P Date: Tue, 6 Apr 2021 15:45:30 -0700 Subject: [PATCH] prettybat: Allow reading from pipe (#51) --- src/prettybat.sh | 13 ++++++++++--- test/suite/prettybat.sh | 13 +++++++++++++ 2 files changed, 23 insertions(+), 3 deletions(-) diff --git a/src/prettybat.sh b/src/prettybat.sh index 49d44cd..de8f061 100755 --- a/src/prettybat.sh +++ b/src/prettybat.sh @@ -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") diff --git a/test/suite/prettybat.sh b/test/suite/prettybat.sh index c0800b2..6d9933c 100644 --- a/test/suite/prettybat.sh +++ b/test/suite/prettybat.sh @@ -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)" +}