2014-02-05 10:12:34 +01:00
|
|
|
#!/bin/bash
|
|
|
|
|
2015-08-22 20:40:59 +02:00
|
|
|
shopt -s nullglob
|
|
|
|
|
2015-09-06 14:59:07 +02:00
|
|
|
# We use this to make sure the cache files are sorted bytewise
|
2017-01-06 15:02:47 +01:00
|
|
|
export LC_COLLATE=C
|
2015-04-12 19:51:46 +02:00
|
|
|
|
2017-01-06 13:03:13 +01:00
|
|
|
cache_file=/tmp/clipmenu.$USER/line_cache
|
2014-02-05 10:12:34 +01:00
|
|
|
|
2015-09-06 14:59:07 +02:00
|
|
|
# It's okay to hardcode `-l 8` here as a sensible default without checking
|
|
|
|
# whether `-l` is also in "$@", because the way that dmenu works allows a later
|
|
|
|
# argument to override an earlier one. That is, if the user passes in `-l`, our
|
|
|
|
# one will be ignored.
|
2017-01-06 13:52:34 +01:00
|
|
|
chosen_line=$(sed 's/^[^|]\+|//' "$cache_file" | tac | uniq | dmenu -l 8 "$@")
|
2014-02-05 10:16:42 +01:00
|
|
|
|
|
|
|
[[ $chosen_line ]] || exit 1
|
|
|
|
|
2017-01-06 15:02:47 +01:00
|
|
|
# Naive, but performant path, only follow expensive path if it doesn't work
|
|
|
|
out_line=$(grep -F "|$chosen_line" "$cache_file")
|
|
|
|
out_length=$(wc -l <<< "$out_line")
|
|
|
|
|
|
|
|
if (( out_length == 1 )); then
|
|
|
|
# Cheap path succeded
|
|
|
|
file=${out_line%%|*}
|
|
|
|
elif (( out_length > 1 )); then
|
|
|
|
# Cheap path failed
|
|
|
|
while IFS='|' read -r full_file first_line; do
|
|
|
|
if [[ $first_line == "$chosen_line" ]]; then
|
|
|
|
file=$full_file
|
|
|
|
break
|
|
|
|
fi
|
|
|
|
done <<< "$out_line"
|
|
|
|
else
|
|
|
|
# We didn't find this in cache
|
|
|
|
printf 'FATAL: %s not in cache, run clipmenu-fsck\n' "$chosen_line" >&2
|
|
|
|
exit 2
|
|
|
|
fi
|
|
|
|
|
|
|
|
|
2014-02-05 10:48:03 +01:00
|
|
|
for selection in clipboard primary; do
|
2015-02-08 18:36:06 +01:00
|
|
|
if type -p xsel >/dev/null 2>&1; then
|
2017-01-06 15:02:47 +01:00
|
|
|
xsel -i --"$selection" < "$file"
|
2015-02-08 18:36:06 +01:00
|
|
|
else
|
2017-01-06 15:02:47 +01:00
|
|
|
xclip -sel "$selection" < "$file"
|
2015-02-08 18:36:06 +01:00
|
|
|
fi
|
2014-02-05 10:48:03 +01:00
|
|
|
done
|