#!/bin/bash workdir="${XDG_DATA_HOME:-$HOME/.local/share/clipboard/history}" maxsize="${CLIPBOARD_MAX_SIZE:-30M}" histcount="${CLIPBOARD_HISTORY_COUNT:-300}" red=$(tput setaf 1) reset=$(tput sgr0) delim="\034" umask 077 mkdir -p "$workdir" cd "$workdir" || exit daemon(){ echo "values may be subject to change" echo " workdir: $workdir" echo " maxsize: $maxsize" echo " histcount: $histcount" echo " current: $(find "${workdir}" | wc | awk '{print $2}')" wl-paste -w "$0" on-copy } on-copy(){ if [ -f "$workdir/.lock" ] ; then echo "script is currently locked." exit 0 fi file="$(date +%s)" cat | sed -e 's/^[ \t]*//' >"$file" ext="$(file --mime-type "$file" | cut -d' ' -f2 | cut -d'/' -f2)" mv "$file" "$file.$ext" fdupes -idN . find . -type f -size 0 -delete find . -type f -size "+$maxsize" -delete find . -type f -print0 \ | sort -zn \ | head -z --lines="-$histcount" \ | xargs -r -0 rm } list(){ find . -type f -printf '%P\0' \ | sort -znr \ | while IFS= read -r -d '' file ; do if [[ "$file" =~ \.(plain|x-python)$ ]]; then printf "%s${delim}" "$file" "${red}${reset} $(sed ':a;N;$!ba;s/\n/+/g' < "$file")" elif [[ "$file" =~ \.octet-stream$ ]]; then printf "%s${delim}" "$file" "${red}﫳${reset} $(sed ':a;N;$!ba;s/\n/+/g' < "$file")" else printf "%s${delim}" "$file" "${red}${reset} $file" fi printf '\0' done } parse-file(){ echo "$1" | awk '{print $1}' FS="$delim" } on-select(){ read -r -d '' result if [ -z "$result" ]; then exit 0 fi wl-paste local file file="$(parse-file "${result}")" wl-copy < "$file" swaymsg exec "ydotool type $(wl-paste | sed -e "s/'/'\\\\''/g; 1s/^/'/; \$s/\$/'/")" } menu(){ list | fzf --read0 +s \ --info=hidden \ --reverse \ -d "${delim}" --nth ..2 --with-nth 2 \ --bind="del:execute-silent($0 delete {})+reload($0 list),f1:execute($0 qr {})" \ --prompt='clip:: ' \ --pointer='➜' \ --color="gutter:0,prompt:4" \ --no-multi \ --preview "$0 preview {}" \ --preview-window=down:3:wrap \ --ansi \ | on-select } usage(){ cat <