8000 If/else to case in preview-tui by luukvbaal · Pull Request #1009 · jarun/nnn · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

If/else to case in preview-tui #1009

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
May 14, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 30 additions & 18 deletions plugins/preview-tui
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ startpreview() {
--env "PREVIEW_MODE=1" --env "PAGER=$PAGER" --env "TMPDIR=$TMPDIR" \
--env "USE_SCOPE=$USE_SCOPE" --env "SPLIT=$SPLIT" --env "TERMINAL=$TERMINAL"\
--env "USE_PISTOL=$USE_PISTOL" --env "BAT_STYLE=${BAT_STYLE:-numbers}" \
--env "PAGERPID=$PAGERPID" --env "GIFPID=$GIFPID" --env "FIFO_UEBERZUG=$FIFO_UEBERZUG" \
--env "PAGERPID=$PAGERPID" --env "IMGPID=$IMGPID" --env "FIFO_UEBERZUG=$FIFO_UEBERZUG" \
--env "CURSEL=$CURSEL" --location "${SPLIT}split" "$0" "$1" >/dev/null
elif [ -n "$2" ]; then
QUICKLOOK=1 QLPATH="$2" PREVIEW_MODE=1 "$0" "$1" >/dev/null &
Expand Down Expand Up @@ -180,6 +180,31 @@ print_bin_info() {
fi
}

handle_mime() {
case "$2" in
image/*) image_preview "$cols" "$lines" "$1" ;;
application/zip) fifo_pager unzip -l "$1" ;;
text/troff)
if exists man; then
fifo_pager man -Pcat -l "$1"
else
fifo_pager pager "$1"
fi ;;
*) handle_ext "$1" "$3" "$4" ;;
esac
}

handle_ext() {
case "$2" in
gz|bz2) fifo_pager tar -tvf "$1" ;;
*) if [ "$3" = "bin" ]; then
fifo_pager print_bin_info "$1"
else
fifo_pager pager "$1"
fi ;;
esac
}

preview_file () {
clear
# Trying to use pistol if it's available.
Expand Down Expand Up @@ -223,29 +248,16 @@ preview_file () {
fifo_pager ls -F --directories-first --color=always
fi
elif [ "${encoding#*)}" = "binary" ]; then
if [ "${mimetype%%/*}" = "image" ]; then
image_preview "$cols" "$lines" "$1"
elif [ "$mimetype" = "application/zip" ]; then
fifo_pager unzip -l "$1"
elif [ "$ext" = "gz" ] || [ "$ext" = "bz2" ]; then
fifo_pager tar -tvf "$1"
else
fifo_pager print_bin_info "$1"
fi
elif [ "$mimetype" = "text/troff" ]; then
if exists man; then
fifo_pager man -Pcat -l "$1"
else
fifo_pager pager "$1"
fi
handle_mime "$1" "$mimetype" "$ext" "bin"
else
fifo_pager pager "$1"
handle_mime "$1" "$mimetype" "$ext"
fi
}

image_preview() {
if [ "$TERMINAL" = "kitty" ]; then
# Kitty terminal users can use the native image preview method.
# Kitty terminal users can use the native image preview method although one might consider
# installing ueberzug and moving the ueberzug clause to the top since it performs alot better.
kitty +kitten icat --silent --place "$1"x"$2"@0x0 --transfer-mode=stream --stdin=no "$3" &
printf "%s" "$!" > "$IMGPID"
elif [ -n "$QLPATH" ] && stat "$3" >/dev/null 2>&1; then
Expand Down
129 changes: 63 additions & 66 deletions plugins/preview-tui-ext
< ED48 td class="blob-code blob-code-deletion js-file-line"> generate_preview "$cols" "$lines" "$1" "gif"
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ startpreview() {
--env "PREVIEW_MODE=1" --env "PAGER=$PAGER" --env "TMPDIR=$TMPDIR" \
--env "USE_SCOPE=$USE_SCOPE" --env "SPLIT=$SPLIT" --env "TERMINAL=$TERMINAL"\
--env "USE_PISTOL=$USE_PISTOL" --env "BAT_STYLE=${BAT_STYLE:-numbers}" \
--env "PAGERPID=$PAGERPID" --env "GIFPID=$GIFPID" --env "FIFO_UEBERZUG=$FIFO_UEBERZUG" \
--env "PAGERPID=$PAGERPID" --env "IMGPID=$IMGPID" --env "FIFO_UEBERZUG=$FIFO_UEBERZUG" \
--env "ICONLOOKUP=$ICONLOOKUP" --env "NNN_PREVIEWDIR=$NNN_PREVIEWDIR" \
--env "NNN_PREVIEWWIDTH=$NNN_PREVIEWWIDTH" --env "NNN_PREVIEWHEIGHT=$NNN_PREVIEWHEIGHT" \
--env "CURSEL=$CURSEL" --location "${SPLIT}split" "$0" "$1" >/dev/null
Expand Down Expand Up @@ -200,6 +200,63 @@ print_bin_info() {
fi
}

handle_mime() {
case "$2" in
image/jpg) image_preview "$cols" "$lines" "$1" ;;
image/gif) generate_preview "$cols" "$lines" "$1" "gif" ;;
image/*) generate_preview "$cols" "$lines" "$1" "image" ;;
video/*) generate_preview "$cols" "$lines" "$1" "video" ;;
audio/*) generate_preview "$cols" "$lines" "$1" "audio" ;;
application/font*|application/*opentype) generate_preview "$cols" "$lines" "$1" "font" ;;
*/*office*|*/*document*) generate_preview "$cols" "$lines" "$1" "office" ;;
application/zip) fifo_pager unzip -l "$1" ;;
text/troff)
if exists man; then
fifo_pager man -Pcat -l "$1"
else
fifo_pager pager "$1"
fi ;;
*) handle_ext "$1" "$3" "$4" ;;
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If no mimetype matches, check extensions

esac
}

handle_ext() {
case "$2" in
epub) generate_preview "$cols" "$lines" "$1" "epub" ;;
pdf) generate_preview "$cols" "$lines" "$1" "pdf" ;;
gz|bz2) fifo_pager tar -tvf "$1" ;;
md) if exists glow; then
fifo_pager glow -s dark "$1"
elif exists lowdown; then
fifo_pager lowdown -Tterm "$1"
else
fifo_pager pager "$1"
fi ;;
htm|html|xhtml)
if exists w3m; then
fifo_pager w3m "$1"
elif exists lynx; then
fifo_pager lynx "$1"
elif exists elinks; then
fifo_pager elinks "$1"
else
fifo_pager pager "$1"
fi ;;
7z|a|ace|alz|arc|arj|bz|cab|cpio|deb|jar|lha|lz|lzh|lzma|lzo\
|rar|rpm|rz|t7z|tar|tbz|tbz2|tgz|tlz|txz|tZ|tzo|war|xpi|xz|Z)
if exists atool; then
fifo_pager atool -l "$1"
elif exists bsdtar; then
fifo_pager bsdtar -tvf "$1"
fi ;;
*) if [ "$3" = "bin" ]; then
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

print_bin_info() or pager depending on whether or not we came from if [ "${encoding#*)}" = "binary" ]

fifo_pager print_bin_info "$1"
else
fifo_pager pager "$1"
fi ;;
esac
}

preview_file() {
clear
# Trying to use pistol if it's available.
Expand All @@ -226,9 +283,7 @@ preview_file() {
encoding="$(file -bL --mime-encoding -- "$1")"
mimetype="$(file -bL --mime-type -- "$1")"
ext="${1##*.}"
if [ -n "$ext" ]; then
ext="$(printf "%s" "${ext}" | tr '[:upper:]' '[:lower:]')"
fi
[ -n "$ext" ] && ext="$(printf "%s" "${ext}" | tr '[:upper:]' '[:lower:]')"
lines=$(tput lines)
cols=$(tput cols)

Expand All @@ -248,68 +303,9 @@ preview_file() {
fifo_pager ls -F --group-directories-first --color=always
fi
elif [ "${encoding#*)}" = "binary" ]; then
if [ "${mimetype%%/*}" = "image" ]; then
if [ "${mimetype#*/}" = "gif" ]; then
elif [ "${mimetype#*/}" != "jpeg" ]; then
generate_preview "$cols" "$lines" "$1" "image"
else
image_preview "$cols" "$lines" "$1"
fi
elif [ "${mimetype%%/*}" = "audio" ]; then
generate_preview "$cols" "$lines" "$1" "audio"
elif [ "${mimetype%%/*}" = "video" ]; then
generate_preview "$cols" "$lines" "$1" "video"
elif [ "$ext" = "pdf" ]; then
generate_preview "$cols" "$lines" "$1" "pdf"
elif [ "$ext" = "epub" ]; then
generate_preview "$cols" "$lines" "$1" "epub"
elif [ "${mimetype#*opentype}" != "$mimetype" ] || [ "${mimetype#*font}" != "$mimetype" ]; then
generate_preview "$cols" "$lines" "$1" "font"
elif [ "${mimetype#*office}" != "$mimetype" ] || [ "${mimetype#*document}" != "$mimetype" ]; then
generate_preview "$cols" "$lines" "$1" "office"
elif [ "$mimetype" = "application/zip" ]; then
fifo_pager unzip -l "$1"
elif [ "$ext" = "gz" ] || [ "$ext" = "bz2" ]; then
fifo_pager tar -tvf "$1"
else
case "$ext" in
7z|a|ace|alz|arc|arj|bz|cab|cpio|deb|jar|lha|lz|lzh|lzma|lzo\
|rar|rpm|rz|t7z|tar|tbz|tbz2|tgz|tlz|txz|tZ|tzo|war|xpi|xz|Z)
if exists atool; then
fifo_pager atool -l "$1"
elif exists bsdtar; then
fifo_pager bsdtar -tvf "$1"
fi ;;
*) fifo_pager print_bin_info "$1" ;;
esac
fi
elif [ "$ext" = "md" ]; then
if exists glow; then
fifo_pager glow -s dark "$1"
elif exists lowdown; then
fifo_pager lowdown -Tterm "$1"
else
fifo_pager pager "$1"
fi
elif [ "$ext" = "htm" ] || [ "$ext" = "html" ] || [ "$ext" = "xhtml" ]; then
if exists w3m; then
fifo_pager w3m "$1"
elif exists lynx; then
fifo_pager lynx "$1"
elif exists elinks; then
fifo_pager elinks "$1"
else
fifo_pager pager "$1"
fi
elif [ "$mimetype" = "text/troff" ]; then
if exists man; then
fifo_pager man -Pcat -l "$1"
else
fifo_pager pager "$1"
fi
handle_mime "$1" "$mimetype" "$ext" "bin"
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pass "bin" as argument to handle_mime() to maintain the if [ "${encoding#*)}" = "binary" ] condition.

else
fifo_pager pager "$1"
handle_mime "$1" "$mimetype" "$ext"
fi
}

Expand Down Expand Up @@ -357,7 +353,8 @@ generate_preview() {
image_preview() {
clear
if [ "$TERMINAL" = "kitty" ]; then
# Kitty terminal users can use the native image preview method.
# Kitty terminal users can use the native image preview method although one might consider
# installing ueberzug and moving the ueberzug clause to the top since it performs alot better.
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add note about kitty vs ueberzug performance consideration

kitty +kitten icat --silent --place "$1"x"$2"@0x0 --transfer-mode=stream --stdin=no "$3" &
printf "%s" "$!" > "$IMGPID"
elif exists ueberzug; then
Expand Down
0