2019-04-20 16:51:20 +00:00
|
|
|
#!/bin/bash
|
|
|
|
set -euo pipefail
|
|
|
|
|
|
|
|
sniplet_dir="${HOME}/.config/sniplet"
|
|
|
|
tmpdir=$(mktemp -d)
|
|
|
|
|
|
|
|
function cleanup() {
|
|
|
|
rm -rf ${tmpdir}
|
|
|
|
}
|
|
|
|
trap cleanup EXIT
|
|
|
|
|
|
|
|
# In case the directory does not exist create it
|
|
|
|
[ -d "${sniplet_dir}" ] || {
|
|
|
|
mkdir "${sniplet_dir}"
|
|
|
|
}
|
|
|
|
|
|
|
|
# Find sniplet files
|
2021-09-12 10:55:15 +00:00
|
|
|
names=$(find "${sniplet_dir}" -type f | sed "s|${sniplet_dir}/||" | sort)
|
2019-04-20 16:51:20 +00:00
|
|
|
|
|
|
|
# Let the user select one of them
|
|
|
|
name=$(
|
|
|
|
zenity \
|
|
|
|
--list \
|
|
|
|
--title="Sniplet" \
|
|
|
|
--width=275 \
|
|
|
|
--height=400 \
|
|
|
|
--column=Sniplet \
|
|
|
|
${names}
|
|
|
|
)
|
|
|
|
sniplet="${sniplet_dir}/${name}"
|
|
|
|
|
|
|
|
# Check presence of the sniplet
|
|
|
|
[ -f "${sniplet}" ] || {
|
|
|
|
zenity --error --text="Sniplet has gone away"
|
|
|
|
exit 1
|
|
|
|
}
|
|
|
|
|
|
|
|
# Store clipboard away
|
2021-09-12 10:55:15 +00:00
|
|
|
xclip -selection clipboard -o >"${tmpdir}/clipboard"
|
|
|
|
xclip -selection primary -o >"${tmpdir}/primary"
|
2019-04-20 16:51:20 +00:00
|
|
|
|
|
|
|
# Render the sniplet using korvike for templating
|
2021-09-12 10:55:15 +00:00
|
|
|
korvike -i "${sniplet}" -o "${tmpdir}/sniplet"
|
2019-04-20 16:51:20 +00:00
|
|
|
|
|
|
|
# Set clipboard with sniplet
|
2021-09-12 10:55:15 +00:00
|
|
|
xclip -selection clipboard -i <"${tmpdir}/sniplet"
|
|
|
|
xclip -selection primary -i <"${tmpdir}/sniplet"
|
2019-04-20 16:51:20 +00:00
|
|
|
|
|
|
|
# Do the paste magic
|
|
|
|
sleep 0.3
|
|
|
|
xdotool key shift+Insert
|
|
|
|
sleep 0.5
|
|
|
|
|
|
|
|
# Restore clipboard
|
2021-09-12 10:55:15 +00:00
|
|
|
xclip -selection clipboard -i <"${tmpdir}/clipboard"
|
|
|
|
xclip -selection primary -i <"${tmpdir}/primary"
|