My Scripts.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
bin/dcd

110 lines
2.0 KiB

#!/usr/bin/env bash
# Written by skiqqy, my minimal directory browsing script.
# Use dmenu to browse directories and open st or sxiv
# Pipe the input into $@ whilst evaling $@ it if pipe is not empty
ifne () {
read -r line || return 1
(echo -e "..\n$line"; cat) | eval "$@" # Eval is evil
}
# Basicaly cd using dmenu, whilst setting global dir to new directory
goto()
{
local pos
local goto
pos=${1:-.}
goto=.
if goto=$(find "$pos" -maxdepth 1 -type d -printf '%f\n' | tail -n +2 |
ifne dmenu -p "$dir")
then
[ -z "$goto" ] && return 1
cd "$pos/$goto" || return 1
dir="$(realpath "$pos/$goto")"
else
return 1
fi
}
gui()
{
# Hitting escape anytime will open that directory in st/sxiv.
while goto "$dir"
do
:
done
if $term
then
st
else
local img
img=$(sxiv -to "$dir" | head -1)
if "$setwp" && [ -n "$img" ]
then
setwp "$img"
fi
fi
}
main()
{
term=true
persist=false
setwp=false
default=$HOME
while getopts 'hispf:' opt
do
case "$opt" in
h)
printf -- 'dcd ~ Simple Directory Browser\n'
printf -- '\nUsage: dcd [OPTIONS] [START DIR]\n\n'
printf -- 'Options\n'
printf -- '-h \tShows This Message\n'
printf -- '-i \tOpen the directory using sxiv (Browse the images in selected directory).\n'
printf -- '-s \tSet the marked image as the wallpaper upon closing dcd, needs -i\n'
printf -- '-p \tPersist, ie use dcd as a full blown directory browser.\n'
printf -- '-f TARGET\tFind TARGET\n\n'
printf -- 'Written by Skiqqy\n'
exit 0
;;
i)
term=false
;;
s)
setwp=true
;;
p)
persist=true
;;
f)
default=$(find "$HOME" 2> /dev/null | grep -E "$OPTARG\$" | grep -v "$HOME/.cache" | head -1)
if [ -z "$default" ]
then
printf 'Could not find %s' "$OPTARG"
exit 1
fi
;;
*)
exit 1
;;
esac
done
shift $((OPTIND-1)) # Remove parsed options
dir=${1:-$default}
dir=$(realpath "$dir")
cd "$dir" || exit 1
if "$persist"
then
while gui; do : ; done # Main event loop.
else
gui
fi
}
main "$@"