clip (1364B)
1 #!/bin/sh 2 3 # A primitive clipboard manager. Saves the current selection and clipboard 4 # to a temporary file and shows all saved selections in dmenu for the user 5 # to select one. Duplicate selections are avoided. 6 7 # Bugs (wontfix): the program fails if it can't create the temporary 8 # directory with the designated name. 9 10 # Usage: clip [-m menu] 11 12 # Requires: xsel, dmenu (or similar) 13 14 menu="dmenu -i -l 25" 15 16 usage() { 17 echo "Usage: clip [-m MENU]" 18 } 19 20 while getopts "m:" opt; do 21 case "$opt" in 22 m) 23 menu="$OPTARG" 24 ;; 25 *) 26 usage 27 exit 1 28 ;; 29 esac 30 done 31 shift "$((OPTIND - 1))" 32 33 dir="/tmp/clipdir" 34 mkdir -p "$dir" 35 36 file1="$(mktemp -p "$dir" "$(date +%s)".XXXXX)" 37 file2="$(mktemp -p "$dir" "$(date +%s)"b.XXXX)" 38 echo "created $file1" 39 echo "created $file2" 40 xsel > "$file1" 41 xsel -b > "$file2" 42 43 # Avoid duplicates 44 for f in "$dir"/*; do 45 [ "$f" != "$file1" ] && if diff "$f" "$file1"; then rm "$file1"; fi 46 [ "$f" != "$file2" ] && if diff "$f" "$file2"; then rm "$file2"; fi 47 done 48 49 list="$(mktemp)" 50 ls "$dir" > "$list"; 51 52 lines="$(mktemp)" 53 for f in "$dir"/*; do 54 nlines="$(wc -l "$f" | awk '{print $1}')" 55 fclean="$(echo "$f" | sed "s|$dir\/||")" 56 printf '%s (%s) | %s\n' \ 57 "$fclean" "$((1+nlines))" "$(head -n 1 "$f")" >> "$lines" 58 done 59 60 selected=$(sort -r "$lines" | $menu | awk '{print $1}') 61 if [ -n "$selected" ]; then 62 xsel -ib < "$dir/$selected" 63 fi