~skiqqy/bin

ref: 1a1a726478e94be941594a7c54e0bf9ab207801a bin/dcd -rwxr-xr-x 1.3 KiB
1a1a7264Stephen Cochrane Added dcd 1 year, 6 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
#!/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 "$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' | sed 's|\./||g' |
		tail -n +2 | ifne dmenu -p "$dir")
	then
		[ -z "$goto" ] && return 1
		cd "$pos/$goto" || return 1
		dir="$pos/$goto"
	else
		return 1
	fi
}

main()
{
	term=true
	while getopts hi 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\n'
				printf -- 'Written by Skiqqy\n'
				exit 0
				;;
			i)
				term=false
				;;
			*)
				exit 1
				;;
		esac
	done
	shift $((OPTIND-1)) # Remove parsed options

	dir=${1:-$HOME/Pictures}
	dir=$(realpath "$dir")
	cd "$dir" || exit 1

	# Hitting escape anytime will open that directory in st/sxiv.
	while goto "$dir"
	do
		echo "$dir"
	done

	if $term
	then
		st
	else
		sxiv -t "$dir"
	fi
}

main "$@"