Another copy of my dotfiles. Because I don't completely trust GitHub.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

80 lines
2.3 KiB

4 years ago
4 years ago
  1. #!/bin/sh
  2. #description: dmenu for killall
  3. #usage: dmenu-killall is best suited for launching from a shortcut
  4. #example: dmenu-killall
  5. #a gui menu appears asking for which app to kill
  6. progname="$(expr "${0}" : '.*/\([^/]*\)')"
  7. #variables are impractical to save complex cmds because of shell expantion
  8. #therefore functions are required: http://mywiki.wooledge.org/BashFAQ/050
  9. DMENU() { dmenu -i -p 'Kill'; }
  10. #looks better on xft powered dmenu:
  11. #https://bugs.launchpad.net/ubuntu/+source/suckless-tools/+bug/1093745
  12. _usage() {
  13. printf "%s\\n" "Usage: ${progname} [PATTERN]"
  14. printf "%s\\n" "Dmenu window selector for i3-wm."
  15. printf "%s\\n"
  16. printf "%s\\n" " -h, --help show this message and exit"
  17. }
  18. _die() {
  19. [ -n "${1}" ] && _die_msg="${1}" || exit 1
  20. printf "%b%b\\n" "${_die_msg}" ", press <Enter> to exit" | DMENU
  21. exit 1
  22. }
  23. _notify() {
  24. [ -z "${1}" ] && return 1
  25. kill -9 $(pgrep notify-osd) >/dev/null 2>&1
  26. if ! DISPLAY=${DISPLAY:-:0} notify-send -t 1000 "${1}" "${2}"; then
  27. if command -v "gxmessage" 2>/dev/null; then
  28. font="Monaco 9"
  29. DISPLAY=${DISPLAY:-:0} gxmessage "${font:+-fn "$font"}" "${1}" "ok"
  30. elif command -v "xmessage" 2>/dev/null; then
  31. font="fixed"
  32. DISPLAY=${DISPLAY:-:0} xmessage "${font:+-fn "$font"}" "${1}" "ok"
  33. fi
  34. fi
  35. }
  36. _get_process_names() {
  37. printf "%s\\n" "$(command ps xo command= | sed \
  38. -e "s: .*::; s:.*/::; s/:$//;" \
  39. -e "s:^\[.*\]$::" -e "/^$/d" \
  40. -e "s:^<defunct>$::")" | sort | uniq
  41. }
  42. if [ ! -t 0 ]; then
  43. #add input comming from pipe or file to $@
  44. set -- "${@}" $(cat)
  45. fi
  46. for arg in "${@}"; do #parse options
  47. case "${arg}" in
  48. -h|--help) _usage && exit ;;
  49. esac
  50. done
  51. if [ -z "${1}" ]; then
  52. if ! command -v "dmenu" >/dev/null 2>&1; then
  53. printf "%s\\n" "${progname}: install 'dmenu' to run this program" >&2
  54. exit 1
  55. fi
  56. process_name="$(_get_process_names | DMENU)"
  57. else
  58. process_name="$(_get_process_names | grep -i "${@}" 2>/dev/null | head -1 )"
  59. fi
  60. if [ -z "${process_name}" ]; then
  61. _die
  62. else
  63. error_msg="$(kill -9 $(pgrep -x "${process_name}") 2>&1 1>/dev/null)"
  64. if [ X"${?}" != X"0" ]; then
  65. _notify "Error" "${error_msg}"
  66. exit 1
  67. fi
  68. fi
  69. #vim:ft=sh