rbw.plugin.zsh 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. if (( ! $+commands[rbw] )); then
  2. return
  3. fi
  4. # TODO: 2021-12-28: remove this bit of code as it exists in oh-my-zsh.sh
  5. # Add completions folder in $ZSH_CACHE_DIR
  6. command mkdir -p "$ZSH_CACHE_DIR/completions"
  7. (( ${fpath[(Ie)"$ZSH_CACHE_DIR/completions"]} )) || fpath=("$ZSH_CACHE_DIR/completions" $fpath)
  8. # If the completion file doesn't exist yet, we need to autoload it and
  9. # bind it to `rbw`. Otherwise, compinit will have already done that.
  10. if [[ ! -f "$ZSH_CACHE_DIR/completions/_rbw" ]]; then
  11. typeset -g -A _comps
  12. autoload -Uz _rbw
  13. _comps[rbw]=_rbw
  14. fi
  15. rbw gen-completions zsh >| "$ZSH_CACHE_DIR/completions/_rbw" &|
  16. # rbwpw function copies the password of a service to the clipboard
  17. # and clears it after 20 seconds
  18. function rbwpw {
  19. if [[ $# -ne 1 ]]; then
  20. echo "usage: rbwpw <service>"
  21. return 1
  22. fi
  23. local service=$1
  24. if ! rbw unlock; then
  25. echo "rbw is locked"
  26. return 1
  27. fi
  28. local pw=$(rbw get $service 2>/dev/null)
  29. if [[ -z $pw ]]; then
  30. echo "$service not found"
  31. return 1
  32. fi
  33. echo -n $pw | clipcopy
  34. echo "password for $service copied!"
  35. {sleep 20 && clipcopy </dev/null 2>/dev/null} &|
  36. }
  37. function _rbwpw {
  38. local -a services
  39. services=("${(@f)$(rbw ls 2>/dev/null)}")
  40. [[ -n "$services" ]] && compadd -a -- services
  41. }
  42. compdef _rbwpw rbwpw