#!/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