From aa47068fd0f947cde9d8d013e34bc5b36abe0cf7 Mon Sep 17 00:00:00 2001 From: Marty Oehme Date: Sat, 6 Aug 2022 21:54:55 +0200 Subject: [PATCH] scripts: Add nsxiv-rifle script Opens basically any path, local or remote in nsxiv. Can take multiple paths to open all of them. Additionally, alias whichever version of the image viewer is available on the system as `iv`, using `nsxiv-rifle`, `nsxiv`, `sxiv` in order of preference. --- scripts/.local/bin/nsxiv-rifle | 75 ++++++++++++++++++++++++++++++++++ sh/.config/sh/alias | 11 +++++ 2 files changed, 86 insertions(+) create mode 100755 scripts/.local/bin/nsxiv-rifle diff --git a/scripts/.local/bin/nsxiv-rifle b/scripts/.local/bin/nsxiv-rifle new file mode 100755 index 0000000..60b0e3d --- /dev/null +++ b/scripts/.local/bin/nsxiv-rifle @@ -0,0 +1,75 @@ +#!/usr/bin/env sh +# +# Advanced nsxiv url opening script. +# +# Can open remote urls of individual images, galleries, and local files at the same time. +# Uses gallery-dl for remote gallery opening - so if gallery-dl can open it, so can nsxiv. +# +# Usage: simply add multiple urls/paths as arguments. +# nsxiv-rifle "https://linkto.my/image.png" my/local/relative-path.jpg /home/absolute.gif + +cache_dir="$(mktemp -d)" + +die() { + [ -n "$1" ] && printf '%s\n' "$*" >&2 + exit 1 +} + +cleanup() { + rm -f -- "$cache_dir"/* +} + +from_local() { + fpath="$(realpath "$1")" + ln -s "$fpath" "$cache_dir" +} + +# invokes single image downloading or gallery downloading depending on content type +from_remote() { + link="$(dereference "$1")" + if curl -sLI "$link" | grep -i '^Content-Type:' | grep -q image; then + get_single_image "$link" + else + get_gallery "$link" + fi +} + +get_gallery() { + command -v gallery-dl 1>/dev/null 2>&1 || return + gallery-dl -q -D "$cache_dir" "$1" +} + +get_single_image() { + pwd="$PWD" + cd "$cache_dir" && curl -sSLO "$1" + cd "$pwd" || exit 1 +} + +# unaliases urls which will not be downloaded correctly otherwise +dereference() { + # teddit + if echo "$1" | grep -qe "https\?://w\?w\?w\?\.\?teddit\.net.\+"; then + echo "$1" | sed -e 's|^.*pics/w:null_\(.\+\)$|https://i.redd.it/\1|' + else + echo "$1" + fi +} + +### main ### +command -v nsxiv 1>/dev/null 2>&1 || die "Requires nsxiv installed" + +[ -z "$1" ] && nsxiv +trap cleanup EXIT +[ -d "$cache_dir" ] || mkdir -p -- "$cache_dir" || die +im_number=1 +while [ -n "$1" ]; do + case "$1" in + *://*.*) from_remote "$1" ;; + *) from_local "$1" ;; + esac + im_number=$((im_number + 1)) + shift +done + +[ "$(find "$cache_dir" -type f -print | wc -l)" -ne 0 ] && + nsxiv -p "$cache_dir" diff --git a/sh/.config/sh/alias b/sh/.config/sh/alias index a6a247c..fd8fe99 100644 --- a/sh/.config/sh/alias +++ b/sh/.config/sh/alias @@ -93,6 +93,17 @@ if exist vifm; then alias vmm='vifm ${PWD}' fi +# nsxiv image viewer +if exist nsxiv; then + if exist nsxiv-rifle; then + alias iv=nsxiv-rifle + else + alias iv=nsxiv + fi +elif exist sxiv; then + alias iv=sxiv +fi + # python if exist ptipython; then alias py=ptipython