aboutsummaryrefslogtreecommitdiff
path: root/sel
blob: f8845e2b45b2310bab7cfa6316933bbf837e6dd2 (plain)
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
101
102
103
104
105
106
#!/bin/sh

# Allow bulk operations on a list of selected files.
# Usage: sel [command]
# If no command is specified, shows the list of selected files
# Commands:
#	add [files|dirs|all]: add files to selection
#	clear: clear selection
#	cp: copy to current dir, possbily after editing filenames
#	edit: open selection in editor
#	mv: move to current dir, possbily after editing filenames
#	open: open files using open-file
#	rm: remove selected files and clear selection

# Requires: dmenu (or similar), trashrm (optional), open-file (for open only)

# TODO: add possibility of specifying file.
# TODO 2: The usage of paste(1) is a bit of a hack, and for example it does
# not work if filenames contain tab characters. Fix this.

file="$XDG_DATA_HOME/selectedfiles"
menu=${MENU:-dmenu}
editor=${EDITOR:-vi}
rm=trashrm  # replace with rm -r if you don't use trashrm

clear() {
	cat /dev/null > "$file"
}

cphere() {
	file2=$(mktemp)
	sed 's/^.*\///' "$file" > "$file2"
	$editor "$file2"
	paste "$file" "$file2" | while read f; do
		fold=$(echo "$f" | sed 's/\t.*//')
		fnew=$(echo "$f" | sed 's/.*\t//')
		cp -R "$fold" ./"$fnew"
	done
}

remove() {
	while read f; do
		$rm "$f"
	done < "$file"
}

if [ -z "$1" ]; then
	cat "$file"
else
case "$1" in
	add)
		if [ -z "$2" ]; then
			dmenu-filepicker >> "$file"
		else
		case "$2" in
			files)
				ls | while read f; do
					[ -f "$f" ] && echo "$PWD/$f" >> "$file"
				done
				;;
			dirs)
				ls | while read f; do
					[ -d "$f" ] && echo "$PWD/$f" >> "$file"
				done
				;;
			all)
				ls | while read f; do
					echo "$PWD/$f" >> "$file"
				done
				;;
			*)
				echo "$2: not a valid option"
				;;
		esac
		fi
		sort -u -o "$file" "$file"
		;;
	clear)
		clear
		;;
	cp)
		cphere
		clear
		;;
	edit)
		$editor "$file"
		;;
	mv)
		cphere
		remove
		clear
		;;
	open)
		while read f; do
			open-file "$f"
		done < "$file"
		;;
	rm)
		remove
		clear
		;;
	*)
		echo "$1: not a valid sel command"
		;;
esac
fi

Generated with cgit - Back to sebastiano.tronto.net