aboutsummaryrefslogtreecommitdiff
path: root/clip
blob: 4f1ae2571746095923a74fd04c51cb0bac9c0a33 (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
#!/bin/sh

# A primitive clipboard manager. Saves the current selection and clipboard
# to a temporary file and shows all saved selections in dmenu for the user
# to select one. Duplicate selections are avoided.

# Bugs (wontfix): the program fails if it can't create the temporary
# directory with the designated name.

# Requires: xsel, dmenu

# TODO: improve formatting in dmenu

menu="dmenu"
menuopts="-l 25"

dir="/tmp/clipdir"
mkdir -p "$dir"

file1="$(mktemp -p $dir $(date +%s).XXXXX)"
file2="$(mktemp -p $dir $(date +%s)b.XXXX)"
xsel    > "$file1"
xsel -b > "$file2"

# Avoid duplicates
for f in $dir/*; do
	[ "$f" != "$file1" ] && if diff "$f" "$file1"; then rm "$file1"; fi
	[ "$f" != "$file2" ] && if diff "$f" "$file2"; then rm "$file2"; fi
done

list="$(mktemp)"
ls $dir > $list;

lines="$(mktemp)"
for f in $dir/*; do
	nlines=$(expr 1 + "$(wc -l $f | awk '{print $1}')")
	fclean=$(echo $f | sed "s|$dir\/||")
	printf "$fclean ($nlines) |    $(head -n 1 $f)\n" >> "$lines"
done

selected=$(sort -r $lines | $menu $menuopts | awk '{print $1}')
if [ -n "$selected" ]; then
	xsel -ib < "$dir/$selected"
fi

Generated with cgit - Back to sebastiano.tronto.net