aboutsummaryrefslogtreecommitdiff
path: root/clip
blob: dbd3f333d9841bec8e1dd41771bd74165ef2b7ce (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
#!/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.

# Usage: clip [-m menu]

# Requires: xsel, dmenu (or similar)

menu="dmenu -i -l 25"

while getopts "m:" opt; do
	case "$opt" in
		m)
			menu="$OPTARG"
			;;
	esac
done
shift `expr $OPTIND - 1`

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 | awk '{print $1}')
if [ -n "$selected" ]; then
	xsel -ib < "$dir/$selected"
fi

Generated with cgit - Back to sebastiano.tronto.net