Handle clipboard replace only for snippet with a clipboard placeholder

Current clipboard is saved after bashdown template.
Bashdown command can now work with the clipboard
This commit is contained in:
BarbUk 2018-12-27 15:28:22 +04:00
parent 4cb85271cc
commit f0a5ab1273

28
snippy
View File

@ -15,7 +15,7 @@
# Based on "snippy" by "sessy" # Based on "snippy" by "sessy"
# (https://bbs.archlinux.org/viewtopic.php?id=71938) # (https://bbs.archlinux.org/viewtopic.php?id=71938)
# #
# You will also need "dmenu", "zenity", "xsel" and "xdotool". Get them from your linux # You will also need "dmenu", "zenity", "xsel", "sponge" and "xdotool". Get them from your linux
# distro in the usual way. # distro in the usual way.
# #
# To use: # To use:
@ -104,7 +104,6 @@ run() {
cd "${snippets_directory}" || exit cd "${snippets_directory}" || exit
local current_clipboard cursor_line cursor_line_position cursor_line cursor_position cursor_line_lenght local current_clipboard cursor_line cursor_line_position cursor_line cursor_position cursor_line_lenght
current_clipboard=$(xsel -b)
# Use the filenames in the snippy directory as menu entries. # Use the filenames in the snippy directory as menu entries.
# Get the menu selection from the user. # Get the menu selection from the user.
@ -127,20 +126,27 @@ run() {
content="$( bashdown < "${snippets_directory}/${snippet}" )" content="$( bashdown < "${snippets_directory}/${snippet}" )"
fi fi
# replace {clipboard} by the clipboard content printf "%s" "$content" > "$tmpfile"
# use awk to handle correctly multiline clipboard
printf "%s" "$content" | awk \
-v clipboard="$current_clipboard" \
-v placeholder="$clipboard_placeholder" \
'{ gsub(placeholder, clipboard); print }' > "$tmpfile"
# remove last EOL
perl -pi -e 'chomp if eof' "$tmpfile"
else [[ ${snippet} =~ ^$ ]] else [[ ${snippet} =~ ^$ ]]
${snippet##*$} 2>/dev/null > "$tmpfile" # execute as bashcommand ${snippet##*$} 2>/dev/null > "$tmpfile" # execute as bashcommand
fi fi
# save current clipboard
current_clipboard=$(xsel -b)
# replace {clipboard} by the clipboard content
# use awk to handle correctly multiline clipboard
if grep -q "$clipboard_placeholder" "$tmpfile"; then
awk \
-v clipboard="$current_clipboard" \
-v placeholder="$clipboard_placeholder" \
'{ gsub(placeholder, clipboard); print }' "$tmpfile" | sponge "$tmpfile"
# remove last EOL
perl -pi -e 'chomp if eof' "$tmpfile"
fi
# define cursor position and line at 0, we don't need to go up or left if there is no {cursor} placeholder # define cursor position and line at 0, we don't need to go up or left if there is no {cursor} placeholder
cursor_line_position=0 cursor_line_position=0
cursor_position=0 cursor_position=0