n-kill
2201 bytes
1# Copy this file into /usr/share/zsh/site-functions/
2# and add 'autoload n-kill` to .zshrc
3#
4# This function allows to choose a process and a signal to send to it
5#
6# Uses n-list
7
8emulate -L zsh
9
10setopt extendedglob
11zmodload zsh/curses
12
13local IFS="
14"
15
16[ -f ~/.config/znt/n-list.conf ] && builtin source ~/.config/znt/n-list.conf
17[ -f ~/.config/znt/n-kill.conf ] && builtin source ~/.config/znt/n-kill.conf
18
19typeset -A signals
20signals=(
21 1 "1 - HUP"
22 2 "2 - INT"
23 3 "3 - QUIT"
24 6 "6 - ABRT"
25 9 "9 - KILL"
26 14 "14 - ALRM"
27 15 "15 - TERM"
28 17 "17 - STOP"
29 19 "19 - CONT"
30)
31
32local list
33local selected
34local signal
35local -a signal_names
36local title
37
38NLIST_REMEMBER_STATE=0
39
40typeset -a NLIST_NONSELECTABLE_ELEMENTS
41NLIST_NONSELECTABLE_ELEMENTS=( 1 )
42
43type ps 2>/dev/null 1>&2 || { echo >&2 "Error: \`ps' not found"; return 1 }
44
45case "$(uname)" in
46 CYGWIN*) list=( `command ps -Wa` ) ;;
47 *) list=( `command ps -o pid,uid,command -A` ) ;;
48esac
49
50# Ask of PID
51title=$'\x1b[00;31m'"${list[1]}"$'\x1b[00;00m\0'
52shift list
53list=( "$title" "${(@M)list:#(#i)*$1*}" )
54
55local NLIST_GREP_STRING="$1"
56
57if [ "$#list" -eq 1 ]; then
58 echo "No matching processes"
59 return 1
60fi
61
62n-list "$list[@]"
63
64# Got answer? (could be Ctrl-C or 'q')
65if [ "$REPLY" -gt 0 ]; then
66 selected="$reply[REPLY]"
67 selected="${selected## #}"
68 pid="${selected%% *}"
69
70 # Now ask of signal
71 signal_names=( ${(vin)signals} )
72 typeset -a NLIST_HOP_INDEXES
73 NLIST_HOP_INDEXES=( 3 6 8 )
74 unset NLIST_COLORING_PATTERN
75 n-list $'\x1b[00;31mSelect signal:\x1b[00;00m' "$signal_names[@]"
76
77 if [ "$REPLY" -gt 0 ]; then
78 selected="$reply[REPLY]"
79 signal="${(k)signals[(r)$selected]}"
80
81 # ZLE?
82 if [ "${(t)CURSOR}" = "integer-local-special" ]; then
83 zle redisplay
84 zle kill-whole-line
85 zle -U "kill -$signal $pid"
86 else
87 print -zr "kill -$signal $pid"
88 fi
89 else
90 [ "${(t)CURSOR}" = "integer-local-special" ] && zle redisplay
91 fi
92else
93 [ "${(t)CURSOR}" = "integer-local-special" ] && zle redisplay
94fi
95
96# vim: set filetype=zsh: