clipmenu-spmenu/clipmenu

42 lines
900 B
Bash
Executable file

#!/bin/bash
LC_COLLATE=C
# Some people copy/paste huge swathes of text that could slow down dmenu
line_length_limit=500
declare -A selections
ordered_selections=()
files=("/tmp/clipmenu.$USER/"*)
for (( i=${#files[@]}-1; i>=0; i-- )); do
file=${files[$i]}
first_line=$(sed -n '/./{p;q}' "$file" | cut -c1-"$line_length_limit")
lines=$(wc -l "$file")
lines=${lines%% *}
if (( lines > 1 )); then
first_line+=" ($lines lines)"
fi
ordered_selections+=("$first_line")
selections[$first_line]=$file
done
chosen_line=$(
printf '%s\n' "${ordered_selections[@]}" | awk '!x[$0]++' | dmenu "$@"
)
[[ $chosen_line ]] || exit 1
for selection in clipboard primary; do
if type -p xsel >/dev/null 2>&1; then
xsel -i --"$selection" < "${selections[$chosen_line]}"
else
xclip -sel "$selection" < "${selections[$chosen_line]}"
fi
done