diff options
author | Sebastian Gniazdowski <sgniazdowski@gmail.com> | 2015-09-20 14:39:46 +0200 |
---|---|---|
committer | Sebastian Gniazdowski <sgniazdowski@gmail.com> | 2015-11-01 16:10:55 +0100 |
commit | 4c292ea2b023a331dcf1af5644487f2272d24a4d (patch) | |
tree | 7141e282b7aaf9e5fba7ea3102c25ef0e5478d40 /plugins/zsh-navigation-tools/n-kill | |
parent | 9c08641d7c2aae0c82fa5ad91f94c67b70115ba5 (diff) | |
download | zsh-4c292ea2b023a331dcf1af5644487f2272d24a4d.tar.gz zsh-4c292ea2b023a331dcf1af5644487f2272d24a4d.tar.bz2 zsh-4c292ea2b023a331dcf1af5644487f2272d24a4d.zip |
Initial commit of Zsh Navigation Tools
Diffstat (limited to 'plugins/zsh-navigation-tools/n-kill')
-rw-r--r-- | plugins/zsh-navigation-tools/n-kill | 96 |
1 files changed, 96 insertions, 0 deletions
diff --git a/plugins/zsh-navigation-tools/n-kill b/plugins/zsh-navigation-tools/n-kill new file mode 100644 index 000000000..e52082282 --- /dev/null +++ b/plugins/zsh-navigation-tools/n-kill @@ -0,0 +1,96 @@ +# Copy this file into /usr/share/zsh/site-functions/ +# and add 'autoload n-kill` to .zshrc +# +# This function allows to choose a process and a signal to send to it +# +# Uses n-list + +emulate -L zsh + +setopt extendedglob +zmodload zsh/curses + +local IFS=" +" + +[ -f ~/.config/znt/n-list.conf ] && . ~/.config/znt/n-list.conf +[ -f ~/.config/znt/n-kill.conf ] && . ~/.config/znt/n-kill.conf + +typeset -A signals +signals=( + 1 "1 - HUP" + 2 "2 - INT" + 3 "3 - QUIT" + 6 "6 - ABRT" + 9 "9 - KILL" + 14 "14 - ALRM" + 15 "15 - TERM" + 17 "17 - STOP" + 19 "19 - CONT" +) + +local list +local selected +local signal +local -a signal_names +local title + +NLIST_REMEMBER_STATE=0 + +typeset -a NLIST_NONSELECTABLE_ELEMENTS +NLIST_NONSELECTABLE_ELEMENTS=( 1 ) + +type ps 2>/dev/null 1>&2 || { echo >&2 "Error: \`ps' not found"; return 1 } + +case "$(uname)" in + CYGWIN*) list=( `command ps -Wa` ) ;; + *) list=( `command ps -o pid,uid,command -A` ) ;; +esac + +# Ask of PID +title=$'\x1b[00;31m'"${list[1]}"$'\x1b[00;00m\0' +shift list +list=( "$title" "${(@M)list:#(#i)*$1*}" ) + +local NLIST_GREP_STRING="$1" + +if [ "$#list" -eq 1 ]; then + echo "No matching processes" + return 1 +fi + +n-list "$list[@]" + +# Got answer? (could be Ctrl-C or 'q') +if [ "$REPLY" -gt 0 ]; then + selected="$reply[REPLY]" + selected="${selected## #}" + pid="${selected%% *}" + + # Now ask of signal + signal_names=( ${(vin)signals} ) + typeset -a NLIST_HOP_INDEXES + NLIST_HOP_INDEXES=( 3 6 8 ) + unset NLIST_COLORING_PATTERN + n-list $'\x1b[00;31mSelect signal:\x1b[00;00m' "$signal_names[@]" + + if [ "$REPLY" -gt 0 ]; then + selected="$reply[REPLY]" + signal="${(k)signals[(r)$selected]}" + + # ZLE? + if [ "${(t)CURSOR}" = "integer-local-special" ]; then + zle redisplay + zle kill-whole-line + zle -U "kill -$signal $pid" + else + print -zr "kill -$signal $pid" + fi + else + [ "${(t)CURSOR}" = "integer-local-special" ] && zle redisplay + fi +else + [ "${(t)CURSOR}" = "integer-local-special" ] && zle redisplay +fi + +# vim: set filetype=zsh: |