diff --git a/sway/config b/sway/config index ceaefe8..fbd6d94 100644 --- a/sway/config +++ b/sway/config @@ -12,6 +12,7 @@ set $tty-popup $scripts/tty-popup set $menu bash $scripts/menu set $pass_menu $tty-popup pass $scripts/fzf-pass +set $clip_menu $tty-popup pass $scripts/clipboard menu set $printscreen bash $scripts/printscreen diff --git a/sway/config.d/keybinds.config b/sway/config.d/keybinds.config index 93440ba..d8b5bcf 100644 --- a/sway/config.d/keybinds.config +++ b/sway/config.d/keybinds.config @@ -12,6 +12,8 @@ bindsym $mod+backslash exec $pass_menu # reload the configuration file bindsym $mod+Shift+c reload +bindsym $mod++c exec $clip_menu + # exit sway (logs you out of your Wayland session) bindsym $mod+Shift+e exec swaynag -t \ warning -m 'You pressed the exit shortcut. Do you really want to exit sway? This will end your Wayland session.' \ diff --git a/sway/scripts/clipboard b/sway/scripts/clipboard index 511ca50..2c51d70 100755 --- a/sway/scripts/clipboard +++ b/sway/scripts/clipboard @@ -1,50 +1,126 @@ #!/bin/bash workdir="${XDG_DATA_HOME:-$HOME/.local/share/clipboard/history}" -mkdir -p "$workdir" -cd "$workdir" +maxsize="${CLIPBOARD_MAX_SIZE:-30M}" +histcount="${CLIPBOARD_HISTORY_COUNT:-300}" + +red=$(tput setaf 1) +reset=$(tput sgr0) +delim="\034" +#delim=":::::::" +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 -if [ "$1" = "daemon" ]; then - wl-paste -w $0 on-copy -elif [ "$1" = "lock" ]; then - touch .lock -elif [ "$1" = "unlock" ]; then - rm -f .lock -elif [ "$1" = "on-copy" ]; then - [ -f .lock ] && exit 0 file="$(date +%s)" - cat >"$file" + 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 +10000000c -delete - find . -type f -print0 | sort -zn | head -z -n -10000 | xargs -r -0 rm -elif [ "$1" = "dmenu" ]; then - find . -type f -printf '%P\0' | sort -znr | while IFS= read -r -d '' file; do + 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 - cat "$file" + 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' "$file" + printf "%s${delim}" "$file" "${red}${reset} $file" fi printf '\0' - done | dmenu -0 -p clipboard -r "$(realpath "$0") preview" | { + done +} + +parse-file(){ + echo "$1" | awk '{print $1}' FS="$delim" +} + +on-select(){ read -r -d '' result if [ -z "$result" ]; then exit 0 - elif [ -f "$result" ]; then - wl-copy <"$result" - else - printf '%s' "$result" | sed -e 's/[[:space:]]*$//' | sed -Ez 's/\s+$//' | wl-copy fi - } -elif [ "$1" = "preview" ]; then - if [ -f "$2" ]; then - kitty +kitten icat --silent --stdin=no "$2" - else - echo "$2" | bat -l bash --color always -pp + (cat "$(parse-file "${result}")" || exit 1) | wl-copy +} + +menu(){ + #--bind "del:execute-silent($0 delete '{}')+abort" \ + list | fzf --read0 +s \ + --info=hidden \ + --reverse \ + -d "${delim}" --nth ..2 --with-nth 2 \ + --bind="del:execute()" \ + --prompt='clip:: ' \ + --pointer='➜' \ + --color="gutter:0,prompt:4" \ + --no-multi \ + --preview "$0 preview '{}'" --preview-window=down:3:wrap --ansi \ + | on-select +} + +usage(){ + cat <&2 "Usage: $0 " - exit 1 -fi + + printf $(bat -l bash --color always -pp "$file") +} + +case "$1" in + daemon|on-copy|menu|list) + $1;; + lock) + touch "$workdir/.lock" ; echo "locked";; + unlock) + rm -f "$workdir/.lock" ; echo "unlocked";; + preview) + shift ; preview "$*";; + *) + usage;; +esac + +exit;