apps 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. #!/bin/bash
  2. # Originally based on code by Dieter Plaetinck.
  3. # Pretty much re-written by Mina Nagy (mnzaki)
  4. dmenu_cmd="dmenu $DMENU_OPTIONS"
  5. terminal="urxvt -e"
  6. max_recent=199 # Number of recent commands to track
  7. cache_dir="${XDG_CACHE_HOME:-$HOME/.cache}/dmenu-recent"
  8. recent_cache="$cache_dir/recent"
  9. rest_cache="$cache_dir/all"
  10. known_types=" background terminal terminal_hold "
  11. config_dir="${XDG_CONFIG_HOME:-$HOME/.config}/dmenu-recent"
  12. mkdir -p "$cache_dir"
  13. mkdir -p "$config_dir"
  14. touch "$recent_cache"
  15. IFS=:
  16. if stest -dqr -n "$rest_cache" $PATH 2>/dev/null; then
  17. stest -flx $PATH | sort -u | grep -vf "$recent_cache" > "$rest_cache"
  18. fi
  19. IFS=" "
  20. cmd=$(cat "$recent_cache" "$rest_cache" | $dmenu_cmd -p Execute: "$@") || exit
  21. if ! grep -qx "$cmd" "$recent_cache" &> /dev/null; then
  22. grep -vx "$cmd" "$rest_cache" > "$rest_cache.$$"
  23. mv "$rest_cache.$$" "$rest_cache"
  24. fi
  25. echo "$cmd" > "$recent_cache.$$"
  26. grep -vx "$cmd" "$recent_cache" | head -n "$max_recent" >> "$recent_cache.$$"
  27. mv "$recent_cache.$$" "$recent_cache"
  28. # Figure out how to run the command based on the command name, disregarding
  29. # arguments, if any.
  30. word0=${cmd%% *}
  31. match="^$word0$"
  32. get_type () {
  33. while type=$(echo $known_types | xargs -n1 | $dmenu_cmd -p Type:); do
  34. [[ $known_types =~ " $type " ]] || continue
  35. echo "$word0" >> "$config_dir/$type"
  36. break
  37. done
  38. echo $type
  39. }
  40. if ! type=$(grep -lx "$match" -R "$config_dir"); then
  41. type=$(get_type)
  42. else
  43. type=${type##*/}
  44. if ! [[ $known_types =~ " $type " ]]; then
  45. rm "$config_dir/$type"
  46. type=$(get_type)
  47. fi
  48. fi
  49. [[ "$type" = "background" ]] && exec $cmd
  50. [[ "$type" = "terminal" ]] && exec $terminal "$cmd"
  51. [[ "$type" = "terminal_hold" ]] &&
  52. exec $terminal sh -c "$cmd && echo Press Enter to kill me... && read line"