aboutsummaryrefslogtreecommitdiff
path: root/clip
blob: 9f039f898063c424f2124147f4aa532ddd8f15d8 (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
#!/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"

usage() {
	echo "Usage: clip [-m MENU]"
}

while getopts "m:" opt; do
	case "$opt" in
		m)
			menu="$OPTARG"
			;;
		*)
			usage
			exit 1
			;;
	esac
done
shift "$((OPTIND - 1))"

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

file1="$(mktemp -p "$dir" "$(date +%s)".XXXXX)"
file2="$(mktemp -p "$dir" "$(date +%s)"b.XXXX)"
echo "created $file1"
echo "created $file2"
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="$(wc -l "$f" | awk '{print $1}')"
	fclean="$(echo "$f" | sed "s|$dir\/||")"
	printf '%s (%s) |    %s\n' \
		"$fclean" "$((1+nlines))" "$(head -n 1 "$f")" >> "$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