diff options
author | ダカマ <dakama@kakeranoumi.xyz> | 2025-06-02 13:08:46 +0200 |
---|---|---|
committer | ダカマ <dakama@kakeranoumi.xyz> | 2025-06-02 13:08:46 +0200 |
commit | 14a892e627a375334a02381351f139d94ad7ecf3 (patch) | |
tree | e7c1ee5040f3076f57401b3b9e9eb567e2ecfb1a /nsxiv_utils |
Diffstat (limited to 'nsxiv_utils')
-rwxr-xr-x | nsxiv_utils/nsxiv-env | 6 | ||||
-rwxr-xr-x | nsxiv_utils/nsxiv-pipe | 24 | ||||
-rwxr-xr-x | nsxiv_utils/nsxiv-rifle | 51 | ||||
-rwxr-xr-x | nsxiv_utils/nsxiv-url | 32 |
4 files changed, 113 insertions, 0 deletions
diff --git a/nsxiv_utils/nsxiv-env b/nsxiv_utils/nsxiv-env new file mode 100755 index 0000000..10eac20 --- /dev/null +++ b/nsxiv_utils/nsxiv-env @@ -0,0 +1,6 @@ +#!/usr/bin/env sh + +NSXIV_OPTS=${NSXIV_OPTS:-"-a -q"} +# set default args here ^ ^ inside the quotes + +exec nsxiv $NSXIV_OPTS "$@" diff --git a/nsxiv_utils/nsxiv-pipe b/nsxiv_utils/nsxiv-pipe new file mode 100755 index 0000000..31a5216 --- /dev/null +++ b/nsxiv_utils/nsxiv-pipe @@ -0,0 +1,24 @@ +#!/bin/sh + +tmpfile="${TMPDIR:-/tmp}/nsxiv_pipe_$$" +trap 'rm -f -- $tmpfile' EXIT + +if [ "$#" -eq 0 ]; then + if [ -t 0 ]; then + echo "nsxiv-pipe: No arguments provided" >&2; exit 1 + else + # Consume stdin and put it in the temporal file + cat > "$tmpfile" + fi +fi + +for arg in "$@"; do + # if it's a pipe then drain it to $tmpfile + [ -p "$arg" ] && cat "$arg" > "$tmpfile" +done + +if [ -s "$tmpfile" ]; then + nsxiv -q "$@" "$tmpfile" # -q to silence warnings +else + nsxiv "$@" # fallback +fi diff --git a/nsxiv_utils/nsxiv-rifle b/nsxiv_utils/nsxiv-rifle new file mode 100755 index 0000000..ac50568 --- /dev/null +++ b/nsxiv_utils/nsxiv-rifle @@ -0,0 +1,51 @@ +#!/bin/sh + +TMPDIR="${TMPDIR:-/tmp}" +tmp="$TMPDIR/nsxiv_rifle_$$" + +is_img_extension() { + grep -iE '\.(jpe?g|png|gif|svg|jxl|webp|tiff|heif|avif|ico|bmp|pam|pbm|ppm|tga|qoi|ff)$' +} + +listfiles() { + find -L "$1" -maxdepth 1 -type f -print | + is_img_extension | sort -V --ignore-case | tee "$tmp" +} + +open_img() { + file="$1"; shift; + # only go through listfiles() if the file has a valid img extension + if echo "$file" | is_img_extension >/dev/null 2>&1; then + trap 'rm -f $tmp' EXIT + count="$(listfiles "///${file%/*}" | grep -nF "$file")" + fi + if [ -n "$count" ]; then + nsxiv -i -a -g 1200x900 -n "${count%%:*}" "$@" -- < "$tmp" + else + # fallback incase file didn't have a valid extension, or we couldn't + # find it inside the list + nsxiv -a -g 1200x900 "$@" -- "$file" + fi +} + +uri2path() { + python3 - "$@" <<'___HEREDOC' +from urllib.parse import unquote, urlparse +from sys import argv +for arg in argv[1:]: + print(unquote(urlparse(arg).path)) +___HEREDOC +} + +[ "$1" = '--' ] && shift +case "$1" in + "") echo "Usage: ${0##*/} PICTURES" >&2; exit 1 ;; + /*) open_img "$1" ;; + "~"/*) open_img "$HOME/${1#"~"/}" ;; + file:///*) open_img "$(uri2path "$1")" ;; + trash:///*) + trash_dir="${XDG_DATA_HOME:-$HOME/.local/share}/Trash/files" + open_img "${trash_dir}$(uri2path "$1")" -N "nsxiv_trash" + ;; + *) open_img "$PWD/$1" ;; +esac diff --git a/nsxiv_utils/nsxiv-url b/nsxiv_utils/nsxiv-url new file mode 100755 index 0000000..575902a --- /dev/null +++ b/nsxiv_utils/nsxiv-url @@ -0,0 +1,32 @@ +#!/usr/bin/env sh + +cache_dir="${TMPDIR:-/tmp}/nsxiv" + +die() { + [ -n "$1" ] && printf '%s\n' "$*" >&2; + exit 1 +} + +cleanup() { + rm -f -- "$cache_dir"/* +} + +get_image() ( + cd "$cache_dir" && curl -sSLO "$1" +) + +### main ### + +[ -z "$1" ] && die "No arguments given" +trap cleanup EXIT +[ -d "$cache_dir" ] || mkdir -p -- "$cache_dir" || die +while [ -n "$1" ]; do + case "$1" in + *://*.*) get_image "$1" ;; + *) echo "Invalid url: $1" >&2 ;; + esac + shift +done + +[ "$(find "$cache_dir" -type f -print | wc -l)" -ne 0 ] && + nsxiv -p "$cache_dir" |