~mil/sxmo-utils

ref: f0ca7048123f2985ccb4babc0338761a62d5295c sxmo-utils/scripts/appscripts/sxmo_files.sh -rwxr-xr-x 1.4 KiB
f0ca7048Stacy Harper sxmo file only should open the selected file 2 months 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
#!/usr/bin/env sh

# include common definitions
# shellcheck source=scripts/core/sxmo_common.sh
. "$(dirname "$0")/sxmo_common.sh"

DIR="$1"
[ -z "$DIR" ] && DIR="$HOME"
cd "$DIR" || exit 1

SORT=
REVERSE=

sort_loop() {
	CHOICES="$([ -z "$SORT" ] && echo "date" || echo "name")\n$([ -z "$REVERSE" ] && echo "desc" || echo "asc")"

	PICKED="$(
		printf %b "$CHOICES" |
		sxmo_dmenu.sh -p "Sort" -i
	)"

	echo "$PICKED" | grep -q "date" && SORT="--sort=t"
	echo "$PICKED" | grep -q "name" && SORT=
	echo "$PICKED" | grep -q "desc" && REVERSE="-r"
	echo "$PICKED" | grep -q "asc" && REVERSE=
}


while true; do
	# shellcheck disable=SC2086
	FILES="$(ls -1p $SORT $REVERSE)"
	CHOICES="$(printf %b 'Reload\nOpen in terminal\nClose Menu\nSort By\n../\n*\n'"$FILES")"
	DIR="$(basename "$(pwd)")"
	TRUNCATED="$(printf %.7s "$DIR")"
	if [ "$DIR" != "$TRUNCATED" ]; then
		DIR="$TRUNCATED..."
	fi


	PICKED="$(
		echo "$CHOICES" |
		sxmo_dmenu.sh -p "$DIR" -i
	)" || exit

	echo "$PICKED" | grep "Sort By" && sort_loop
	echo "$PICKED" | grep "Open in terminal" && cd "$(pwd)" && sxmo_terminal.sh
	echo "$PICKED" | grep "Close Menu" && exit 0
	echo "$PICKED" | grep "Reload" && continue
	[ -d "$PICKED" ] && cd "$PICKED" && continue
	echo "$PICKED" | grep -E '^[*]$' && sxmo_open.sh -a ./*
	if [ -f "$PICKED" ] || [ "$PICKED" =  "Open in terminal" ]; then
		sxmo_open.sh -a "$PICKED"
	fi
done