Created
January 21, 2024 17:33
-
-
Save dtomvan/116576294d8bf3adcd72af534da9705d to your computer and use it in GitHub Desktop.
Wrapper for `sxiv -`
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env bash | |
# When used as drop-in script, sxiv can now also read an image from stdin | |
# through `sxiv -`. Would've solved xyb3rt/sxiv#250. | |
# To use: write to /usr/local/bin/sxiv and chmod a+x. | |
# Note: this doesn't guarantee the input args are correct, it just separates | |
# files from options. | |
TEMP=$(getopt -o "A::abce::fG::g::hin::N::opqrS::s::tvZz::" -n sxiv -q $@) | |
if [ $? -ne 0 ]; then | |
# If things go south, let's use the actual sxiv to handle any problems with | |
# the input args. | |
exec /usr/bin/sxiv $@ | |
fi | |
eval set -- "$TEMP" | |
unset TEMP | |
opts=() | |
while [ "$1" != "--" ]; do | |
opts+=("$1") | |
shift | |
done | |
shift | |
files="$@" | |
# When we encounter a dash, we store stdin as a temporary file in order for sxiv | |
# to read it. There is only one stdin, so we allow for only one input file. | |
# | |
# We preserve the order of the files through an indexed for loop. | |
for i in ${!files[@]}; do | |
if [ "${files[$i]}" == "-" ]; then | |
tempfile="$(mktemp)" | |
cp /dev/stdin "$tempfile" | |
files[$i]="$tempfile" | |
break | |
fi | |
done | |
/usr/bin/sxiv $opts $files | |
if [ -f "$tempfile" ]; then | |
rm "$tempfile" | |
fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment