2018-12-21 16:25:15 -05:00
|
|
|
|
#!/usr/bin/bash
|
2020-03-09 09:08:31 -04:00
|
|
|
|
#
|
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
|
2018-12-21 16:25:15 -05:00
|
|
|
|
# . go left to the correct position for cli and gui paste
|
|
|
|
|
# . go up for block snippet for gui paste
|
2017-03-23 17:10:48 -04:00
|
|
|
|
# . ##noparse header in snippet to not parse
|
2019-10-28 07:11:09 -04:00
|
|
|
|
# . ##tmpfile header in snippet to replace $tmpfile by the temp filename used in the script
|
|
|
|
|
# . ##richsnippet header in snippet to use paste buffer in rich mode (To copy html content in gui)
|
2018-05-17 07:36:07 -04:00
|
|
|
|
# . execute command begining by $
|
2019-02-01 01:39:51 -05:00
|
|
|
|
# . execute bash script in $snippets_directory/scripts
|
2020-02-17 02:35:05 -05:00
|
|
|
|
# . copy script content when selection is selected by CTRL+Return
|
|
|
|
|
# . icons ! Icon name is set from first dir name. If you have the following snippets, the terminal icon will be displayed in rofi
|
|
|
|
|
# terminal/
|
|
|
|
|
# ├── other
|
|
|
|
|
# │ └── date
|
|
|
|
|
# └── script
|
|
|
|
|
# └── test
|
2018-05-17 07:36:07 -04:00
|
|
|
|
#
|
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)
|
|
|
|
|
#
|
2020-09-01 10:30:02 -04:00
|
|
|
|
# You will also need "rofi", "xsel", "xclip" and "xdotool". Get them from your linux
|
2016-11-01 16:27:13 -04:00
|
|
|
|
# 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.
|
|
|
|
|
#
|
2020-02-17 02:35:05 -05:00
|
|
|
|
# TIP: The name of the subdirectory will be passed to rofi as an icon name
|
|
|
|
|
#
|
2016-11-01 16:27:13 -04:00
|
|
|
|
# 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")
|
|
|
|
|
#
|
2020-02-17 02:35:05 -05:00
|
|
|
|
set -o errexit -o pipefail -o nounset
|
2016-11-01 16:27:13 -04:00
|
|
|
|
|
2021-06-18 05:46:14 -04:00
|
|
|
|
readonly snippets_directory=${XDG_CONFIG_HOME:-$HOME/.config}/snippy
|
|
|
|
|
readonly rofi_args=(-no-lazy-grab -dmenu -i -sort -sorting-method fzf -async-pre-read 20 -theme-str 'element-icon { size: 2.35ch;}' -kb-accept-custom "" -kb-custom-1 "Ctrl+Return")
|
2021-06-18 05:59:47 -04:00
|
|
|
|
readonly fzf_args=(--select-1 --reverse --inline-info --multi --preview '( bat --style auto --color always --language bash {} 2> /dev/null || highlight --force -O ansi -l {} 2> /dev/null ) | head -200' -1)
|
2018-12-21 16:25:15 -05:00
|
|
|
|
|
2020-02-17 02:35:05 -05:00
|
|
|
|
# Placeholders
|
2020-09-01 10:30:02 -04:00
|
|
|
|
readonly placeholder_cursor="{cursor}"
|
|
|
|
|
readonly placeholder_clipboard="{clipboard}"
|
|
|
|
|
readonly placeholder_clipboard_urlencode="{clipboard_urlencode}"
|
2016-11-01 16:27:13 -04:00
|
|
|
|
|
2021-06-18 06:03:12 -04:00
|
|
|
|
# shellcheck disable=2155
|
|
|
|
|
readonly tmpfile=$(mktemp)
|
2018-12-22 07:22:22 -05:00
|
|
|
|
trap 'rm -f $tmpfile' EXIT HUP INT TRAP TERM
|
2016-11-01 16:27:13 -04:00
|
|
|
|
|
2020-06-19 02:36:39 -04:00
|
|
|
|
# colors
|
|
|
|
|
readonly normal="\e[0m"
|
|
|
|
|
readonly bold="\e[1m"
|
|
|
|
|
readonly underline="\e[4m"
|
|
|
|
|
|
2020-02-17 02:35:05 -05:00
|
|
|
|
script_content=false
|
2020-06-19 02:36:39 -04:00
|
|
|
|
action=gui
|
|
|
|
|
snippet=
|
2020-02-17 02:35:05 -05:00
|
|
|
|
|
2016-11-01 16:27:13 -04:00
|
|
|
|
# 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 ")
|
2018-12-21 16:25:15 -05:00
|
|
|
|
bashdown() {
|
2018-05-23 10:01:41 -04:00
|
|
|
|
while IFS= read -r line; do
|
2019-05-17 09:46:00 -04:00
|
|
|
|
line="$(eval "printf -- \"$( printf "%s" "$line" | sed 's/"/\\"/g' )\"")";
|
2016-11-01 16:27:13 -04:00
|
|
|
|
echo -e "$line"
|
2018-04-22 01:50:52 -04:00
|
|
|
|
done
|
2016-11-01 16:27:13 -04:00
|
|
|
|
}
|
|
|
|
|
|
2019-05-17 09:46:00 -04:00
|
|
|
|
# Simplified version of bashdown, use echo and bash var search and replace
|
|
|
|
|
# Better handling of symbol char
|
2019-10-28 07:06:05 -04:00
|
|
|
|
bashdown_simple() {
|
2020-05-08 12:45:41 -04:00
|
|
|
|
while IFS= read -r line || [[ -n "$line" ]]; do
|
2019-05-17 09:46:00 -04:00
|
|
|
|
line="$(eval "echo \"${line//\"/\\\"}\"")"
|
|
|
|
|
echo "$line"
|
|
|
|
|
done
|
|
|
|
|
}
|
|
|
|
|
|
2018-12-21 16:25:15 -05:00
|
|
|
|
# Detect if focused app is a terminal or a gui
|
|
|
|
|
is_gui() {
|
2020-02-09 01:13:14 -05:00
|
|
|
|
class="$(xprop -id "$(xdotool getwindowfocus)" WM_CLASS | cut -d'"' -f2 | tr '[:upper:]' '[:lower:]')"
|
|
|
|
|
# Return false if the class if a term
|
|
|
|
|
if [[ "$class" =~ term|tilda|kitty|alacritty ]]; then
|
|
|
|
|
return 1
|
|
|
|
|
fi
|
2018-02-01 14:49:44 -05:00
|
|
|
|
return 0
|
|
|
|
|
}
|
|
|
|
|
|
2020-02-09 01:13:14 -05:00
|
|
|
|
# Detect vim
|
|
|
|
|
is_vim() {
|
|
|
|
|
name="$(xprop -id "$(xdotool getwindowfocus)" WM_NAME | cut -d'"' -f2)"
|
|
|
|
|
# vim with `set title` set the term title with:
|
|
|
|
|
# document - VIM
|
|
|
|
|
if [[ "${name:(-3)}" == VIM ]]; then
|
|
|
|
|
return 0
|
|
|
|
|
fi
|
|
|
|
|
return 1
|
|
|
|
|
}
|
|
|
|
|
|
2018-12-21 16:25:15 -05:00
|
|
|
|
# Find the index of a string in a string
|
|
|
|
|
strindex() {
|
|
|
|
|
x="${1%%$2*}"
|
|
|
|
|
[[ "$x" = "$1" ]] && echo -1 || echo "${#x}"
|
|
|
|
|
}
|
|
|
|
|
|
2019-10-28 07:06:57 -04:00
|
|
|
|
is_rich_snippet() {
|
|
|
|
|
file="$1"
|
|
|
|
|
grep -q "##richsnippet" "$file"
|
|
|
|
|
}
|
|
|
|
|
|
2018-12-21 16:25:15 -05:00
|
|
|
|
# Move the cursor up or left
|
|
|
|
|
move_cursor() {
|
|
|
|
|
local key=$1
|
|
|
|
|
local count=$2
|
|
|
|
|
local keys="End "
|
|
|
|
|
if [[ $count -gt 0 ]]; then
|
|
|
|
|
until [ "$count" -eq 0 ]; do
|
|
|
|
|
keys+="${key} "
|
2020-03-13 08:04:25 -04:00
|
|
|
|
((count-=1)) || true
|
2018-12-21 16:25:15 -05:00
|
|
|
|
done
|
|
|
|
|
# shellcheck disable=SC2086
|
|
|
|
|
xdotool key --delay 0.1 $keys
|
|
|
|
|
fi
|
|
|
|
|
}
|
|
|
|
|
|
2020-03-09 09:09:50 -04:00
|
|
|
|
init() {
|
2020-06-19 02:36:39 -04:00
|
|
|
|
# Check basic dependency
|
|
|
|
|
local all_needed_programs_installed=true
|
2020-09-01 10:30:02 -04:00
|
|
|
|
local needed_programs=( rofi fzf xsel xclip jq )
|
2020-06-19 02:36:39 -04:00
|
|
|
|
for program in "${needed_programs[@]}"; do
|
|
|
|
|
if ! command -v "$program" >/dev/null 2>&1; then
|
|
|
|
|
all_needed_programs_installed=false
|
|
|
|
|
echo -e "${bold}$program${normal} missing"
|
|
|
|
|
fi
|
|
|
|
|
done
|
|
|
|
|
|
|
|
|
|
if [ "$all_needed_programs_installed" = false ] ; then
|
|
|
|
|
echo -e "\nPlease install the previous dependancies"
|
|
|
|
|
exit 1
|
|
|
|
|
fi
|
|
|
|
|
|
|
|
|
|
# Check snippet directory
|
2020-03-09 09:09:50 -04:00
|
|
|
|
if [[ ! -d "$snippets_directory" ]]; then
|
|
|
|
|
mkdir -p "$snippets_directory"
|
|
|
|
|
echo "$snippets_directory created"
|
|
|
|
|
echo "hi it is \$(date)" > "$snippets_directory/test"
|
|
|
|
|
fi
|
|
|
|
|
}
|
|
|
|
|
|
2020-06-19 02:36:39 -04:00
|
|
|
|
usage() {
|
|
|
|
|
echo "Usage:"
|
|
|
|
|
echo -e "\t${bold}$0${normal} [OPTION] ACTION"
|
|
|
|
|
echo -e "\tSnippy snippets manager"
|
2018-12-21 16:25:15 -05:00
|
|
|
|
|
2020-06-19 02:36:39 -04:00
|
|
|
|
echo "Options"
|
|
|
|
|
echo -e "\t-h, --help Show help"
|
|
|
|
|
|
|
|
|
|
echo "Actions"
|
|
|
|
|
echo -ne "\t${bold}gui${normal}"
|
|
|
|
|
echo -e "\t Browse snippet and paste it in the focused window ${underline}(default)${normal}"
|
|
|
|
|
|
|
|
|
|
echo -ne "\t${bold}cli${normal}"
|
|
|
|
|
echo -e "\t list snippet in cli mode, only copy snippet in the paste buffer"
|
|
|
|
|
|
|
|
|
|
echo -ne "\t${bold}edit${normal}"
|
|
|
|
|
echo -e "\t Browse snippet and edit it"
|
|
|
|
|
|
|
|
|
|
echo -ne "\t${bold}add${normal}"
|
|
|
|
|
echo -e "\t Add a new snippet"
|
|
|
|
|
|
|
|
|
|
echo -ne "\t${bold}list${normal}"
|
|
|
|
|
echo -e "\t list snippet"
|
|
|
|
|
|
|
|
|
|
echo -ne "\t${bold}cat${normal}"
|
|
|
|
|
echo -e "\t list category"
|
|
|
|
|
|
|
|
|
|
echo -ne "\t${bold}completion${normal}"
|
|
|
|
|
echo -e "\t bash completion"
|
|
|
|
|
|
|
|
|
|
exit
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
parse_options() {
|
|
|
|
|
|
|
|
|
|
while (( "$#" )); do
|
|
|
|
|
case "$1" in
|
|
|
|
|
-h|--help)
|
|
|
|
|
usage
|
|
|
|
|
exit
|
|
|
|
|
;;
|
|
|
|
|
--) # end argument parsing
|
|
|
|
|
shift
|
|
|
|
|
break
|
|
|
|
|
;;
|
|
|
|
|
*)
|
|
|
|
|
action="$1"
|
|
|
|
|
shift
|
|
|
|
|
return
|
|
|
|
|
;;
|
|
|
|
|
esac
|
|
|
|
|
done
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
error() {
|
|
|
|
|
local message="$1"
|
|
|
|
|
echo "$message" >&2
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
cli() {
|
|
|
|
|
snippet=$( list | fzf "${fzf_args[@]}")
|
|
|
|
|
}
|
2018-12-21 16:25:15 -05:00
|
|
|
|
|
2020-06-19 02:36:39 -04:00
|
|
|
|
list() {
|
|
|
|
|
local type="${1:-f}"
|
|
|
|
|
|
|
|
|
|
find -L . -type "$type" \
|
|
|
|
|
| grep -vE '^\.$|\.git|\.gitconfig|\.gitkeep|\.gitignore' \
|
2021-06-18 05:47:06 -04:00
|
|
|
|
| sed -e 's!\.\/!!' \
|
|
|
|
|
| sort
|
2020-06-19 02:36:39 -04:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
add() {
|
|
|
|
|
local snippet="$*"
|
|
|
|
|
if [ -e "${snippet}" ]; then
|
|
|
|
|
error "snippet ${snippet} already exists"
|
|
|
|
|
return 1
|
|
|
|
|
fi
|
|
|
|
|
|
|
|
|
|
if [ -z "$EDITOR" ]; then
|
|
|
|
|
EDITOR=vim
|
|
|
|
|
fi
|
|
|
|
|
|
|
|
|
|
$EDITOR "$snippet"
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
edit() {
|
|
|
|
|
[ -z "${snippet}" ] && return 1
|
|
|
|
|
|
|
|
|
|
if [ -z "$EDITOR" ]; then
|
|
|
|
|
EDITOR=vim
|
|
|
|
|
fi
|
|
|
|
|
|
|
|
|
|
$EDITOR "$snippet"
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
gui() {
|
2016-11-01 16:27:13 -04:00
|
|
|
|
# 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
|
2020-02-17 02:35:05 -05:00
|
|
|
|
set +o errexit
|
|
|
|
|
|
2020-06-19 02:36:39 -04:00
|
|
|
|
snippet=$( list | sed -re 's_^([^/]*)/(.*)_&\x0icon\x1f\1_' | rofi "${rofi_args[@]}" -p '❯ ')
|
2020-02-17 02:35:05 -05:00
|
|
|
|
|
|
|
|
|
if [ $? -eq 10 ]; then
|
|
|
|
|
script_content=true
|
|
|
|
|
fi
|
|
|
|
|
|
|
|
|
|
set -o errexit
|
2020-06-19 02:36:39 -04:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
run() {
|
|
|
|
|
local paste="${1:-true}"
|
|
|
|
|
local current_clipboard cursor_line cursor_line_position cursor_line cursor_position cursor_line_lenght
|
2018-12-21 16:25:15 -05:00
|
|
|
|
|
2016-11-01 16:27:13 -04:00
|
|
|
|
# just return if nothing was selected
|
2020-06-19 02:36:39 -04:00
|
|
|
|
[ -z "${snippet}" ] && return 1
|
2016-11-01 16:27:13 -04:00
|
|
|
|
|
2018-12-21 16:25:15 -05:00
|
|
|
|
if [ -f "${snippets_directory}/${snippet}" ]; then
|
2019-02-14 08:01:04 -05:00
|
|
|
|
|
2016-11-01 16:27:13 -04:00
|
|
|
|
# Put the contents of the selected file into the paste buffer.
|
2019-02-14 08:01:04 -05:00
|
|
|
|
# If file is empty, the content is the basename of the file
|
|
|
|
|
if [ ! -s "${snippets_directory}/${snippet}" ]; then
|
|
|
|
|
content="$(basename "${snippet}")"
|
|
|
|
|
|
2020-02-17 02:35:05 -05:00
|
|
|
|
# Custom selection, copy the script content without parsing
|
|
|
|
|
elif [ "$script_content" = true ]; then
|
|
|
|
|
content="$( cat "${snippets_directory}/${snippet}" )"
|
|
|
|
|
|
2017-03-23 17:10:48 -04:00
|
|
|
|
# don't parse file with the ##noparse header
|
2019-02-14 08:01:04 -05:00
|
|
|
|
elif grep -qE "^##noparse" "${snippets_directory}/${snippet}"; then
|
2018-12-21 16:25:15 -05:00
|
|
|
|
content="$( tail -n +2 "${snippets_directory}/${snippet}" )"
|
2019-02-14 08:01:04 -05:00
|
|
|
|
|
2019-10-28 07:07:24 -04:00
|
|
|
|
# replace tmpfile for snippets with ##tmpfile header
|
|
|
|
|
elif grep -qE "^##tmpfile" "${snippets_directory}/${snippet}"; then
|
|
|
|
|
content="$( bashdown_simple <<< "$(tail -n +2 "${snippets_directory}/${snippet}" | sed "s%\$tmpfile%$tmpfile%g" )" )"
|
|
|
|
|
|
2019-02-14 08:01:04 -05:00
|
|
|
|
# execute bash script in scripts dir
|
2019-02-01 01:39:51 -05:00
|
|
|
|
elif [[ $(dirname "${snippet}") == 'scripts' ]] && grep -qE "^#!/bin/bash" "${snippets_directory}/${snippet}"; then
|
|
|
|
|
content="$( bash "${snippets_directory}/${snippet}" )"
|
2019-02-14 08:01:04 -05:00
|
|
|
|
|
|
|
|
|
# default action
|
2016-12-09 04:11:52 -05:00
|
|
|
|
else
|
2019-10-28 07:06:05 -04:00
|
|
|
|
content="$( bashdown_simple < "${snippets_directory}/${snippet}" )"
|
2016-12-09 04:11:52 -05:00
|
|
|
|
fi
|
2016-11-02 12:58:26 -04:00
|
|
|
|
|
2019-10-28 07:07:53 -04:00
|
|
|
|
if [ -n "$content" ]; then
|
|
|
|
|
printf "%s" "$content" > "$tmpfile"
|
|
|
|
|
fi
|
2018-12-22 07:22:43 -05:00
|
|
|
|
|
2018-12-21 16:25:15 -05:00
|
|
|
|
else [[ ${snippet} =~ ^$ ]]
|
|
|
|
|
${snippet##*$} 2>/dev/null > "$tmpfile" # execute as bashcommand
|
|
|
|
|
fi
|
2016-11-05 03:35:50 -04:00
|
|
|
|
|
2019-10-28 07:07:53 -04:00
|
|
|
|
# if tmpfile is empty at this step, nothing to do.
|
|
|
|
|
if [ ! -s "$tmpfile" ]; then
|
|
|
|
|
return
|
|
|
|
|
fi
|
|
|
|
|
|
2018-12-27 06:28:22 -05:00
|
|
|
|
# save current clipboard
|
2020-04-13 09:05:07 -04:00
|
|
|
|
current_clipboard=$(xsel --clipboard)
|
|
|
|
|
# clear clipboard
|
|
|
|
|
xsel --clipboard --clear
|
2018-12-27 06:28:22 -05:00
|
|
|
|
# replace {clipboard} by the clipboard content
|
|
|
|
|
# use awk to handle correctly multiline clipboard
|
2020-09-01 10:30:02 -04:00
|
|
|
|
if grep -q "$placeholder_clipboard" "$tmpfile"; then
|
|
|
|
|
awk -i inplace \
|
2018-12-27 06:28:22 -05:00
|
|
|
|
-v clipboard="$current_clipboard" \
|
2020-09-01 10:30:02 -04:00
|
|
|
|
-v placeholder="$placeholder_clipboard" \
|
|
|
|
|
'{ gsub(placeholder, clipboard); print }' "$tmpfile"
|
|
|
|
|
|
|
|
|
|
# remove last EOL
|
|
|
|
|
perl -pi -e 'chomp if eof' "$tmpfile"
|
|
|
|
|
fi
|
|
|
|
|
|
|
|
|
|
if grep -q "$placeholder_clipboard_urlencode" "$tmpfile"; then
|
|
|
|
|
awk -i inplace \
|
|
|
|
|
-v clipboard="$( echo "$current_clipboard" | jq -sRr @uri)" \
|
|
|
|
|
-v placeholder="$placeholder_clipboard_urlencode" \
|
|
|
|
|
'{ gsub(placeholder, clipboard); print }' "$tmpfile"
|
2018-12-27 06:28:22 -05:00
|
|
|
|
|
|
|
|
|
# remove last EOL
|
|
|
|
|
perl -pi -e 'chomp if eof' "$tmpfile"
|
|
|
|
|
fi
|
|
|
|
|
|
2018-12-21 16:25:15 -05:00
|
|
|
|
# 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_position=0
|
2016-11-02 12:58:26 -04:00
|
|
|
|
|
2016-11-05 03:35:50 -04:00
|
|
|
|
# Check if there is a {cursor} placeholder
|
2020-09-01 10:30:02 -04:00
|
|
|
|
if grep -qF $placeholder_cursor "$tmpfile"; then
|
2018-12-21 16:25:15 -05:00
|
|
|
|
# retrieve the line number of the cursor placeholder
|
2020-09-01 10:30:02 -04:00
|
|
|
|
cursor_line_position=$(grep -n "$placeholder_cursor" "$tmpfile" | cut -d: -f1)
|
2016-11-05 03:35:50 -04:00
|
|
|
|
# retrieve the line
|
2020-09-01 10:30:02 -04:00
|
|
|
|
cursor_line=$(grep $placeholder_cursor "$tmpfile")
|
2016-11-05 03:35:50 -04:00
|
|
|
|
# calculate snippet total lines
|
2018-12-21 16:25:15 -05:00
|
|
|
|
file_lines=$(wc -l < "$tmpfile")
|
|
|
|
|
# determine the number of line to go up
|
|
|
|
|
cursor_line_position=$(( file_lines - cursor_line_position + 1 ))
|
|
|
|
|
# Extract cursor position
|
2020-09-01 10:30:02 -04:00
|
|
|
|
cursor_position=$(strindex "$cursor_line" $placeholder_cursor)
|
2018-12-21 16:25:15 -05:00
|
|
|
|
# total cursor line lenght
|
|
|
|
|
cursor_line_lenght=${#cursor_line}
|
|
|
|
|
# Compute the final cursor position ( 8 is the lenght of the placeholder {cursor} )
|
|
|
|
|
cursor_position=$(( cursor_line_lenght - cursor_position - 8 ))
|
2016-11-05 03:35:50 -04:00
|
|
|
|
# remove the placeholder
|
2020-09-01 10:30:02 -04:00
|
|
|
|
sed -i -e "s/$placeholder_cursor//g" "$tmpfile"
|
2016-11-04 08:56:07 -04:00
|
|
|
|
fi
|
|
|
|
|
|
2018-12-21 16:25:15 -05:00
|
|
|
|
# Copy snippet in clipboard
|
2019-10-28 07:06:57 -04:00
|
|
|
|
if is_rich_snippet "${snippets_directory}/${snippet}"; then
|
2020-04-13 09:05:07 -04:00
|
|
|
|
xclip -target text/html -selection clipboard -in -loops 1 < "$tmpfile"
|
2019-10-28 07:06:57 -04:00
|
|
|
|
else
|
2020-04-13 09:05:07 -04:00
|
|
|
|
xsel --clipboard --input < "$tmpfile"
|
2019-10-28 07:06:57 -04:00
|
|
|
|
fi
|
2018-12-21 16:25:15 -05:00
|
|
|
|
|
2020-06-19 02:36:39 -04:00
|
|
|
|
if [ "$paste" = true ] ; then
|
|
|
|
|
# Paste into the current application.
|
|
|
|
|
if is_gui; then
|
|
|
|
|
# We need a little pause to handle the time to focus on the window
|
2021-06-18 05:49:01 -04:00
|
|
|
|
sleep 0.15
|
2020-07-03 06:34:06 -04:00
|
|
|
|
# Paste
|
2021-06-18 05:49:01 -04:00
|
|
|
|
xdotool key ctrl+v sleep 0.15
|
2020-02-09 01:13:14 -05:00
|
|
|
|
move_cursor "Up" $cursor_line_position
|
2020-06-19 02:36:39 -04:00
|
|
|
|
else
|
2020-07-03 06:34:06 -04:00
|
|
|
|
xdotool key ctrl+shift+v
|
2020-06-19 02:36:39 -04:00
|
|
|
|
if is_vim; then
|
|
|
|
|
move_cursor "Up" $cursor_line_position
|
|
|
|
|
fi
|
2020-02-09 01:13:14 -05:00
|
|
|
|
fi
|
2020-06-19 02:36:39 -04:00
|
|
|
|
|
|
|
|
|
move_cursor "Left" $cursor_position
|
|
|
|
|
|
|
|
|
|
# Restore current clipboard
|
|
|
|
|
echo -ne "$current_clipboard" | xsel --clipboard --input
|
2016-11-01 16:27:13 -04:00
|
|
|
|
fi
|
2020-06-19 02:36:39 -04:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
main() {
|
|
|
|
|
parse_options "$@"
|
2016-11-04 08:56:07 -04:00
|
|
|
|
|
2020-06-19 02:36:39 -04:00
|
|
|
|
cd "${snippets_directory}" || exit
|
2018-12-21 16:25:15 -05:00
|
|
|
|
|
2020-06-19 02:36:39 -04:00
|
|
|
|
case "$action" in
|
|
|
|
|
'gui' )
|
|
|
|
|
gui
|
|
|
|
|
run
|
|
|
|
|
;;
|
|
|
|
|
'cli' )
|
|
|
|
|
cli
|
|
|
|
|
run false
|
|
|
|
|
;;
|
|
|
|
|
'list' )
|
|
|
|
|
list
|
|
|
|
|
;;
|
|
|
|
|
'cat' )
|
|
|
|
|
list d
|
|
|
|
|
;;
|
|
|
|
|
'add' )
|
|
|
|
|
shift
|
|
|
|
|
add "$@"
|
|
|
|
|
;;
|
|
|
|
|
'edit' )
|
|
|
|
|
cli
|
|
|
|
|
edit
|
|
|
|
|
;;
|
|
|
|
|
*)
|
|
|
|
|
error "Action $action does not exists"
|
|
|
|
|
exit 1
|
|
|
|
|
;;
|
|
|
|
|
esac
|
2016-11-01 16:27:13 -04:00
|
|
|
|
}
|
|
|
|
|
|
2020-06-19 02:36:39 -04:00
|
|
|
|
init && main "$@"
|