~skiqqy/bin

bin/dcd -rwxr-xr-x 1.8 KiB
c0954778Stephen Cochrane Prog on deploy 2 days ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
#!/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 "$@"