c41f2e7a0947139db79ae234b994467ff921f4cb

Author
Carlo Sala <carlosalag@protonmail.com>
Committer
GitHub <noreply@github.com>
Date

Message

feat(rbw): add `rbwpw` function to copy passwords to the clipboard (#10639)

Diff

 1diff --git a/plugins/rbw/README.md b/plugins/rbw/README.md
 2index 87d073d24c25fe2d457dd6c8db6ab68b54d76720..cae36ceadfb4cfd23f3867e98029ece42046002a 100644
 3--- a/plugins/rbw/README.md
 4+++ b/plugins/rbw/README.md
 5@@ -9,4 +9,14 @@ To use it, add `rbw` to the plugins array in your zshrc file:
 6 plugins=(... rbw)
 7 ```
 8 
 9+## `rbwpw`
10+
11+The `rbwpw` function is a wrapper around `rbw`. It copies the password in the
12+clipboard for the service you ask for and clears the clipboard 20s later.
13+The usage is as follows:
14+
15+```zsh
16+rbwpw <service>
17+```
18+
19 This plugin does not add any aliases.
20diff --git a/plugins/rbw/rbw.plugin.zsh b/plugins/rbw/rbw.plugin.zsh
21index 56683ad067dfd507d6589449fce8c9de61c39083..523e35affe8385cd4e4a37ff8779da5d2bc9552d 100644
22--- a/plugins/rbw/rbw.plugin.zsh
23+++ b/plugins/rbw/rbw.plugin.zsh
24@@ -17,3 +17,33 @@ if [[ ! -f "$ZSH_CACHE_DIR/completions/_rbw" ]]; then
25 fi
26 
27 rbw gen-completions zsh >| "$ZSH_CACHE_DIR/completions/_rbw" &|
28+
29+# rbwpw function copies the password of a service to the clipboard
30+# and clears it after 20 seconds
31+function rbwpw {
32+  if [[ $# -ne 1 ]]; then
33+    echo "usage: rbwpw <service>"
34+    return 1
35+  fi
36+  local service=$1
37+  if ! rbw unlock; then
38+    echo "rbw is locked"
39+    return 1
40+  fi
41+  local pw=$(rbw get $service 2>/dev/null)
42+  if [[ -z $pw ]]; then
43+    echo "$service not found"
44+    return 1
45+  fi
46+  echo -n $pw | clipcopy
47+  echo "password for $service copied!"
48+  {sleep 20 && clipcopy </dev/null 2>/dev/null} &|
49+}
50+
51+function _rbwpw {
52+  local -a services
53+  services=("${(@f)$(rbw ls 2>/dev/null)}")
54+  [[ -n "$services" ]] && compadd -a -- services
55+}
56+
57+compdef _rbwpw rbwpw