2016-11-01 16:27:13 -04:00
|
|
|
|
#!/usr/bin/env bash
|
|
|
|
|
# video demo at: http://www.youtube.com/watch?v=90xoathBYfk
|
|
|
|
|
|
2016-11-05 03:41:50 -04:00
|
|
|
|
# augmented by barbuk: https://github.com/BarbUk/dotfiles/blob/master/bin/snippy
|
|
|
|
|
# . restore current clipboard
|
|
|
|
|
# . {clipboard} placeholder to use current clipboard in snippet
|
2017-03-23 17:10:48 -04:00
|
|
|
|
# . {cursor} placeholder to place the cursor
|
|
|
|
|
# . ##noparse header in snippet to not parse
|
2018-05-17 07:36:07 -04:00
|
|
|
|
# . execute command begining by $
|
|
|
|
|
#
|
2016-11-01 16:27:13 -04:00
|
|
|
|
# augmented by "opennomad": https://gist.github.com/opennomad/15c4d624dce99066a82d
|
|
|
|
|
# originally written by "mhwombat": https://bbs.archlinux.org/viewtopic.php?id=71938&p=2
|
|
|
|
|
# Based on "snippy" by "sessy"
|
|
|
|
|
# (https://bbs.archlinux.org/viewtopic.php?id=71938)
|
|
|
|
|
#
|
|
|
|
|
# You will also need "dmenu", "zenity", "xsel" and "xdotool". Get them from your linux
|
|
|
|
|
# distro in the usual way.
|
|
|
|
|
#
|
|
|
|
|
# To use:
|
|
|
|
|
# 1. Create the directory ~/.snippy
|
|
|
|
|
#
|
|
|
|
|
# 2. Create a file in that directory for each snippet that you want.
|
|
|
|
|
# The filename will be used as a menu item, so you might want to
|
|
|
|
|
# omit the file extension when you name the file.
|
|
|
|
|
#
|
|
|
|
|
# TIP: If you have a lot of snippets, you can organise them into
|
|
|
|
|
# subdirectories under ~/.snippy.
|
|
|
|
|
#
|
|
|
|
|
# TIP: The contents of the file will be pasted asis, so if you
|
|
|
|
|
# don't want a newline at the end when the text is pasted, don't
|
|
|
|
|
# put one in the file.
|
|
|
|
|
#
|
|
|
|
|
# 3. Bind a convenient key combination to this script.
|
|
|
|
|
#
|
|
|
|
|
# TIP: If you're using XMonad, add something like this to xmonad.hs
|
|
|
|
|
# ((mod4Mask, xK_s), spawn "/path/to/snippy")
|
|
|
|
|
#
|
|
|
|
|
# 4. Set variables in ~/.snippy.conf. for example
|
|
|
|
|
# MENU_ENGINE="dmenu"
|
|
|
|
|
# DMENU_ARGS=' -p snippy -fn arial -sf green'
|
|
|
|
|
# ZENITY_ARGS='--list --hide-header --column=Snippet --text=snippy'
|
|
|
|
|
# GUIPASTE="xdotool key ctrl+v"
|
|
|
|
|
# CLIPASTE="xdotool key ctrl+shift+v"
|
|
|
|
|
|
|
|
|
|
DIR=${HOME}/.snippy
|
2017-11-07 14:41:07 -05:00
|
|
|
|
MENU_ENGINE="rofi"
|
2018-06-21 02:01:00 -04:00
|
|
|
|
DMENU_ARGS='-dmenu -i -sort'
|
2016-11-01 16:27:13 -04:00
|
|
|
|
# if nothing happens, try "xdotool click 2", "xdotool key ctrl+v" or "xdotool key ctrl+shift+v"
|
|
|
|
|
GUIPASTE="xdotool key ctrl+v"
|
|
|
|
|
CLIPASTE="xdotool key ctrl+shift+v"
|
|
|
|
|
|
|
|
|
|
TMPFILE="/tmp/.snippy.tmp"; :>$TMPFILE
|
|
|
|
|
|
|
|
|
|
MENU_ARGS=${DMENU_ARGS}
|
|
|
|
|
|
|
|
|
|
# smarty like template engine which executes inline bash in (bashdown) strings (replaces variables with values e.g.)
|
|
|
|
|
# @link http://github.com/coderofsalvation/bashdown
|
|
|
|
|
# @dependancies: sed cat
|
|
|
|
|
# @example: echo 'hi $NAME it is $(date)' | bashdown
|
|
|
|
|
# fetches a document and interprets bashsyntax in string (bashdown) templates
|
|
|
|
|
# @param string - string with bash(down) syntax (usually surrounded by ' quotes instead of ")
|
|
|
|
|
bashdown(){
|
2018-05-23 10:01:41 -04:00
|
|
|
|
while IFS= read -r line; do
|
2016-11-01 16:27:13 -04:00
|
|
|
|
line="$(eval "printf -- \"$( printf "%s" "$line" | sed 's/"/\\"/g')\"")";
|
|
|
|
|
echo -e "$line"
|
2018-04-22 01:50:52 -04:00
|
|
|
|
done
|
2016-11-01 16:27:13 -04:00
|
|
|
|
}
|
|
|
|
|
|
2018-02-01 14:49:44 -05:00
|
|
|
|
is_gui(){
|
|
|
|
|
name="$(xprop -id "$(xdotool getwindowfocus)" WM_CLASS | cut -d'"' -f2 | tr '[:upper:]' '[:lower:]')"
|
2018-09-28 10:03:11 -04:00
|
|
|
|
[[ "$name" =~ term|tilda|kitty|alacritty ]] && return 1
|
2018-02-01 14:49:44 -05:00
|
|
|
|
return 0
|
|
|
|
|
}
|
|
|
|
|
|
2016-11-01 16:27:13 -04:00
|
|
|
|
run(){
|
|
|
|
|
cd "${DIR}" || exit
|
|
|
|
|
# Use the filenames in the snippy directory as menu entries.
|
|
|
|
|
# Get the menu selection from the user.
|
2017-06-10 17:41:31 -04:00
|
|
|
|
# shellcheck disable=SC2086
|
2017-11-07 14:41:07 -05:00
|
|
|
|
FILE=$(find -L . -type f | grep -v '^\.$' | sed 's!\.\/!!' | grep -vE '\.git/|\.gitconfig|\.gitkeep' | ${MENU_ENGINE} ${MENU_ARGS} -p '❯ ')
|
2016-11-01 16:27:13 -04:00
|
|
|
|
# just return if nothing was selected
|
|
|
|
|
[[ -z "${FILE}" ]] && return 1
|
|
|
|
|
|
|
|
|
|
if [ -f "${DIR}/${FILE}" ]; then
|
|
|
|
|
# Put the contents of the selected file into the paste buffer.
|
2017-03-23 17:10:48 -04:00
|
|
|
|
# don't parse file with the ##noparse header
|
|
|
|
|
if grep -qE "^##noparse" "${FILE}"; then
|
|
|
|
|
content="$(tail -n +2 "${DIR}/${FILE}")"
|
2016-12-09 04:11:52 -05:00
|
|
|
|
else
|
|
|
|
|
content="$(bashdown < "${DIR}/${FILE}")"
|
|
|
|
|
fi
|
2017-01-28 17:43:23 -05:00
|
|
|
|
printf "%s" "$content" > $TMPFILE
|
2018-05-17 07:36:07 -04:00
|
|
|
|
else [[ ${FILE} =~ ^$ ]]
|
|
|
|
|
${FILE##*$} 2>/dev/null > $TMPFILE # execute as bashcommand
|
2016-11-01 16:27:13 -04:00
|
|
|
|
fi
|
2016-11-02 12:58:26 -04:00
|
|
|
|
|
2016-11-04 08:56:07 -04:00
|
|
|
|
local current_clip cursor
|
2016-11-02 12:58:26 -04:00
|
|
|
|
current_clip=$(xsel -b)
|
2017-07-28 02:43:26 -04:00
|
|
|
|
# define cursor at 0, we don't need to go up if there is no {cursor}
|
2016-11-04 08:56:07 -04:00
|
|
|
|
cursor=0
|
2016-11-05 03:35:50 -04:00
|
|
|
|
|
2016-11-02 12:58:26 -04:00
|
|
|
|
# replace {clipboard} by the cliboard comtent
|
2017-07-28 02:43:26 -04:00
|
|
|
|
sed -i -e "s~{clipboard}~${current_clip}~g" "$TMPFILE"
|
2016-11-02 12:58:26 -04:00
|
|
|
|
|
2016-11-05 03:35:50 -04:00
|
|
|
|
# Check if there is a {cursor} placeholder
|
2016-11-04 08:56:07 -04:00
|
|
|
|
if grep -qF '{cursor}' "$TMPFILE";then
|
|
|
|
|
local file_lines
|
2016-11-05 03:35:50 -04:00
|
|
|
|
# retrieve the line
|
2016-11-04 08:56:07 -04:00
|
|
|
|
cursor=$(grep -n '{cursor}' "$TMPFILE" | cut -d: -f1)
|
2016-11-05 03:35:50 -04:00
|
|
|
|
# calculate snippet total lines
|
2016-11-04 08:56:07 -04:00
|
|
|
|
file_lines=$(wc -l < "$TMPFILE")
|
2016-11-05 03:35:50 -04:00
|
|
|
|
# determine the number of line to go
|
2016-11-04 08:56:07 -04:00
|
|
|
|
cursor=$((file_lines - cursor))
|
2016-11-05 03:35:50 -04:00
|
|
|
|
# remove the placeholder
|
2016-11-04 08:56:07 -04:00
|
|
|
|
sed -i -e "s/{cursor}//g" "$TMPFILE"
|
|
|
|
|
fi
|
|
|
|
|
|
2016-11-02 12:58:26 -04:00
|
|
|
|
xsel -b --input < "$TMPFILE"
|
2016-11-01 16:27:13 -04:00
|
|
|
|
# Paste into the current application.
|
2018-02-01 14:49:44 -05:00
|
|
|
|
if is_gui; then
|
2016-11-01 16:27:13 -04:00
|
|
|
|
${GUIPASTE}
|
2016-11-05 03:35:50 -04:00
|
|
|
|
|
|
|
|
|
if [[ $cursor -gt 0 ]]; then
|
|
|
|
|
local keyup
|
|
|
|
|
until [ $cursor -eq 0 ]; do
|
|
|
|
|
keyup+="Up "
|
2018-02-01 14:49:44 -05:00
|
|
|
|
((cursor-=1))
|
2017-06-10 17:41:31 -04:00
|
|
|
|
# shellcheck disable=SC2086
|
2016-12-12 11:25:41 -05:00
|
|
|
|
xdotool key --delay -0.1 $keyup
|
2016-11-04 08:56:07 -04:00
|
|
|
|
done
|
2016-11-05 03:35:50 -04:00
|
|
|
|
fi
|
2016-11-01 16:27:13 -04:00
|
|
|
|
else
|
|
|
|
|
${CLIPASTE}
|
|
|
|
|
fi
|
2016-11-04 08:56:07 -04:00
|
|
|
|
|
2016-11-02 12:58:26 -04:00
|
|
|
|
echo -ne "$current_clip" | xsel -b --input
|
2016-11-01 16:27:13 -04:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
run
|