n-kill 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  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. emulate -L zsh
  8. setopt extendedglob
  9. zmodload zsh/curses
  10. local IFS="
  11. "
  12. [ -f ~/.config/znt/n-list.conf ] && builtin source ~/.config/znt/n-list.conf
  13. [ -f ~/.config/znt/n-kill.conf ] && builtin source ~/.config/znt/n-kill.conf
  14. typeset -A signals
  15. signals=(
  16. 1 "1 - HUP"
  17. 2 "2 - INT"
  18. 3 "3 - QUIT"
  19. 6 "6 - ABRT"
  20. 9 "9 - KILL"
  21. 14 "14 - ALRM"
  22. 15 "15 - TERM"
  23. 17 "17 - STOP"
  24. 19 "19 - CONT"
  25. )
  26. local list
  27. local selected
  28. local signal
  29. local -a signal_names
  30. local title
  31. NLIST_REMEMBER_STATE=0
  32. typeset -a NLIST_NONSELECTABLE_ELEMENTS
  33. NLIST_NONSELECTABLE_ELEMENTS=( 1 )
  34. type ps 2>/dev/null 1>&2 || { echo >&2 "Error: \`ps' not found"; return 1 }
  35. case "$OSTYPE" in
  36. cygwin*) list=( `command ps -Wa` ) ;;
  37. *) list=( `command ps -o pid,uid,command -A` ) ;;
  38. esac
  39. # Ask of PID
  40. title=$'\x1b[00;31m'"${list[1]}"$'\x1b[00;00m\0'
  41. shift list
  42. list=( "$title" "${(@M)list:#(#i)*$1*}" )
  43. local NLIST_GREP_STRING="$1"
  44. if [ "$#list" -eq 1 ]; then
  45. echo "No matching processes"
  46. return 1
  47. fi
  48. n-list "$list[@]"
  49. # Got answer? (could be Ctrl-C or 'q')
  50. if [ "$REPLY" -gt 0 ]; then
  51. selected="$reply[REPLY]"
  52. selected="${selected## #}"
  53. pid="${selected%% *}"
  54. # Now ask of signal
  55. signal_names=( ${(vin)signals} )
  56. typeset -a NLIST_HOP_INDEXES
  57. NLIST_HOP_INDEXES=( 3 6 8 )
  58. unset NLIST_COLORING_PATTERN
  59. n-list $'\x1b[00;31mSelect signal:\x1b[00;00m' "$signal_names[@]"
  60. if [ "$REPLY" -gt 0 ]; then
  61. selected="$reply[REPLY]"
  62. signal="${(k)signals[(r)$selected]}"
  63. # ZLE?
  64. if [ "${(t)CURSOR}" = "integer-local-special" ]; then
  65. zle redisplay
  66. zle kill-whole-line
  67. zle -U "kill -$signal $pid"
  68. else
  69. print -zr "kill -$signal $pid"
  70. fi
  71. else
  72. [ "${(t)CURSOR}" = "integer-local-special" ] && zle redisplay
  73. fi
  74. else
  75. [ "${(t)CURSOR}" = "integer-local-special" ] && zle redisplay
  76. fi
  77. # vim: set filetype=zsh: