From 6a82d574f59b3acebf855bbca74811ffb3854c0a Mon Sep 17 00:00:00 2001 From: John Mertz Date: Tue, 24 Sep 2024 18:15:46 -0600 Subject: [PATCH] Automatically paste at cursor, then restore previous clipboard --- tofi/tofi-snippets.sh | 22 ++++++++++++++++++++-- 1 file changed, 20 insertions(+), 2 deletions(-) diff --git a/tofi/tofi-snippets.sh b/tofi/tofi-snippets.sh index 68abac6..8193c7a 100755 --- a/tofi/tofi-snippets.sh +++ b/tofi/tofi-snippets.sh @@ -1,9 +1,27 @@ #!/usr/bin/env bash -# Copy snippet to primary clipboard for middle-click pasting +# Paste snippet to mouse location, then restore clipboard # Maintain list of snippets in private repository cd ${HOME}/.private-scripts/snippets HEIGHT=$(swaymsg -t get_outputs | tr '\n' ' ' | sed -e 's/ */ /g' | sed -e 's/\(.*"focused": [a-z]*\),/\1\n/' | less | grep '"focused": true' | sed -e 's/.*"rect": {[^}]*"height": \([0-9]*\).*/\1/') -cat $(find ./ -type f | sed -E 's/\.\///' | ${HOME}/.local/bin/tofi --prompt-text 'Snippets: ' --height $HEIGHT --config ${HOME}/.dotfiles/tofi/sidebar.toml) | wl-copy -p +# Back-up anything already in the clipboard +OLD="$(wl-paste -p)" + +# Get input from Tofi +INPUT="$(find ./ -type f | sed -E 's/\.\///' | ${HOME}/.local/bin/tofi --prompt-text 'Snippets: ' --height $HEIGHT --config ${HOME}/.dotfiles/tofi/sidebar.toml)" + +# Ignore if returned file does not exist +if [ -e "$INPUT" ]; then + + # Copy file contents to clipboard and allow only one paste + wl-copy -p "$(cat $INPUT)" + + # Paste at focus cursor + YDOTOOL_SOCKET=/tmp/.ydotool_socket ydotool click 0xC2 + + # Restored backed-up clipboard + echo "$OLD" | wl-copy -p + +fi