From ac72805dd1b4f85fc4e440c29a60bc30559118a2 Mon Sep 17 00:00:00 2001 From: Gogs Date: Fri, 3 Oct 2014 21:30:16 +0800 Subject: [PATCH] add warp plugin --- plugins/warp/warp.plugin.zsh | 63 ++++++++++++++++++++++++++++++++++++ 1 file changed, 63 insertions(+) create mode 100644 plugins/warp/warp.plugin.zsh diff --git a/plugins/warp/warp.plugin.zsh b/plugins/warp/warp.plugin.zsh new file mode 100644 index 00000000..23c80d8f --- /dev/null +++ b/plugins/warp/warp.plugin.zsh @@ -0,0 +1,63 @@ +#!/bin/bash +#https://github.com/jpalardy/warp +warp() { + # ensure SOURCE file exists + local SOURCE="$HOME/.warp" + if [ ! -f "$SOURCE" ]; then + echo "$SOURCE does not exist..." + return 1 + fi + + # cleanup TARGET file + local TARGET=$HOME/.picked + if [ -e "$TARGET" ]; then + rm "$TARGET" + fi + + # make a selection, with vim + cat "$SOURCE" | vim -c "setlocal noreadonly" \ + -c "setlocal cursorline" \ + -c "setlocal number" \ + -c "nnoremap V:w! ~/.picked:qa!" \ + -c "vnoremap :w! ~/.picked:qa!" \ + -R - + # nothing was selected, abort + if [ ! -e "$TARGET" ]; then + return + fi + + # transform seletion in ssh command + local SSH=${SSH:-ssh} + local COMMAND="" + if [ $(cat "$TARGET" | wc -l) -gt 1 ]; then + # determine which clusterssh to use based on os + unamestr=`uname` + if [[ "$unamestr" == 'Linux' ]]; then + SSH=${MULTISSH:-cssh} + elif [[ "$unamestr" == 'Darwin' ]]; then + SSH=${MULTISSH:-csshX} + fi + COMMAND="$(awk -v cmd=$SSH 'BEGIN {printf cmd} {printf " " $1} END { print "" }' "$TARGET")" + else + COMMAND="$(awk -v cmd=$SSH '{ n = split($1, parts, ":"); if (n == 2) { print cmd, "-p", parts[2], parts[1] } else { print cmd, $1 } }' "$TARGET")" + fi + + # add the command to the bash history as if we had typed it, will only work if sourced + # Determine which history command to use based on shell + if [ -n "$BASH_VERSION" ]; then + eval "history -s $COMMAND" + elif [ -n "$ZSH_VERSION" ]; then + eval "print -s $COMMAND" + fi + + # run the command + eval $COMMAND +} + +# allow warp to be sourced without running +if [[ $_ == $0 ]]; then + warp +else + # Hide warp from history if using zsh and setopt histignorespace + alias warp=" warp" +fi