#!/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="$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
while getopts 'hisp' 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\t\tShows This Message\n'
printf -- '-i\t\tOpen the directory using sxiv (Browse the images in selected directory).\n'
printf -- '-s\t\tSet the marked image as the wallpaper upon closing dcd, needs -i\n'
printf -- '-p\t\tPersist, ie use dcd as a full blown directory browser.\n\n'
printf -- 'Written by Skiqqy\n'
exit 0
;;
i)
term=false
;;
s)
setwp=true
;;
p)
persist=true
;;
*)
exit 1
;;
esac
done
shift $((OPTIND-1)) # Remove parsed options
dir=${1:-$HOME/Pictures}
dir=$(realpath "$dir")
cd "$dir" || exit 1
if "$persist"
then
while gui; do : ; done # Main event loop.
else
gui
fi
}
main "$@"