From f3666c5fe79bb4e0aff05739f52ee21e25ced49a Mon Sep 17 00:00:00 2001 From: BarbUk Date: Wed, 2 Nov 2016 00:27:13 +0400 Subject: [PATCH] merge scripts and bin --- snippy | 100 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 100 insertions(+) create mode 100755 snippy diff --git a/snippy b/snippy new file mode 100755 index 0000000..0130132 --- /dev/null +++ b/snippy @@ -0,0 +1,100 @@ +#!/usr/bin/env bash +# video demo at: http://www.youtube.com/watch?v=90xoathBYfk + +# 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 +MENU_ENGINE="dmenu" +. "$HOME/.dmenurc" +DMENU_ARGS="$DMENU_CONF -l 10 -p Snippets:" +# 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(){ + while read -r line; do + line="$(eval "printf -- \"$( printf "%s" "$line" | sed 's/"/\\"/g')\"")"; + echo -e "$line" + done < <(cat "${DIR}/${FILE}") +} + +run(){ + cd "${DIR}" || exit + # Use the filenames in the snippy directory as menu entries. + # Get the menu selection from the user. + FILE=$(find -L . -type f | grep -v '^\.$' | sed 's!\.\/!!' | ${MENU_ENGINE} ${MENU_ARGS}) + # 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. + content="$(bashdown < "${DIR}/${FILE}")" + if [[ "${#content}" == 0 ]]; then + printf "%s" "${FILE}" > $TMPFILE + else + printf "%s" "$content" > $TMPFILE + fi + else + ${FILE} &> $TMPFILE # execute as bashcommand + fi + local old + old=$(xsel -b) + xsel -b --input < $TMPFILE + # Paste into the current application. + if is_window; then + ${GUIPASTE} + else + ${CLIPASTE} + fi + echo -ne "$old" | xsel -b --input +} + +is_window(){ + name="$(xprop -id "$(xdotool getwindowfocus)" WM_CLASS | cut -d'"' -f2 | tr '[:upper:]' '[:lower:]')" + [[ "$name" =~ term|tilda ]] && return 1 + return 0 +} + +run