]> src.twobees.de Git - dotfiles.git/blob - stow/oh-my-zsh/.oh-my-zsh/plugins/zsh-navigation-tools/n-kill
0d10565e4f9b1ce45d7474a845842c0e04dbc51c
[dotfiles.git] / stow / oh-my-zsh / .oh-my-zsh / plugins / zsh-navigation-tools / n-kill
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
8 emulate -L zsh
9
10 setopt extendedglob
11 zmodload zsh/curses
12
13 local 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
19 typeset -A signals
20 signals=(
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
32 local list
33 local selected
34 local signal
35 local -a signal_names
36 local title
37
38 NLIST_REMEMBER_STATE=0
39
40 typeset -a NLIST_NONSELECTABLE_ELEMENTS
41 NLIST_NONSELECTABLE_ELEMENTS=( 1 )
42
43 type ps 2>/dev/null 1>&2 || { echo >&2 "Error: \`ps' not found"; return 1 }
44
45 case "$(uname)" in
46     CYGWIN*) list=( `command ps -Wa` )  ;;
47     *) list=( `command ps -o pid,uid,command -A` ) ;;
48 esac
49
50 # Ask of PID
51 title=$'\x1b[00;31m'"${list[1]}"$'\x1b[00;00m\0'
52 shift list
53 list=( "$title" "${(@M)list:#(#i)*$1*}" )
54
55 local NLIST_GREP_STRING="$1"
56
57 if [ "$#list" -eq 1 ]; then
58     echo "No matching processes"
59     return 1
60 fi
61
62 n-list "$list[@]"
63
64 # Got answer? (could be Ctrl-C or 'q')
65 if [ "$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
92 else
93     [ "${(t)CURSOR}" = "integer-local-special" ] && zle redisplay
94 fi
95
96 # vim: set filetype=zsh: