From 1734cedbd976d53a051faecbfe659d17df449277 Mon Sep 17 00:00:00 2001 From: Ethan P Date: Wed, 24 Mar 2021 20:00:23 -0700 Subject: [PATCH] batpipe: Add zip viewer --- doc/batpipe.md | 1 + src/batpipe.sh | 26 ++++++++++++++++++++++++-- 2 files changed, 25 insertions(+), 2 deletions(-) diff --git a/doc/batpipe.md b/doc/batpipe.md index ae68711..c7534ae 100644 --- a/doc/batpipe.md +++ b/doc/batpipe.md @@ -25,6 +25,7 @@ Like [lesspipe](https://github.com/wofr06/lesspipe), `batpipe` is designed to wo | -------------------- | --------------------------- | | Directories | `ls` | | `*.tar`, `*.tar.gz` | `tar` | +| `*.zip`, `*.jar` | `unzip` | diff --git a/src/batpipe.sh b/src/batpipe.sh index e8cd2f4..489b2bf 100755 --- a/src/batpipe.sh +++ b/src/batpipe.sh @@ -109,7 +109,7 @@ fi # Viewers: # ----------------------------------------------------------------------------- -BATPIPE_VIEWERS=("ls" "tar") +BATPIPE_VIEWERS=("ls" "tar" "unzip") # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - @@ -148,6 +148,29 @@ viewer_tar_process() { fi } +# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + +viewer_unzip_supports() { + command -v "unzip" &> /dev/null || return 1 + + case "$2" in + *.zip) return 0 ;; + esac + + return 1 +} + +viewer_unzip_process() { + if [[ -n "$2" ]]; then + unzip -p "$1" "$2" | bat_if_not_bat --file-name="$1/$2" + else + batpipe_header "Viewing contents of archive: %{PATH}%s" "$1" + batpipe_subheader "To view files within the archive, add the file path after the archive." + unzip -l "$1" + return $? + fi +} + # ----------------------------------------------------------------------------- # Functions: # ----------------------------------------------------------------------------- @@ -308,5 +331,4 @@ for viewer in "${BATPIPE_VIEWERS[@]}"; do done # No supported viewer. Just pass it through. -cat exit 1