01433503c2a474c049fa56d792ebfd9274e683cc

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

Message

fix(rbw): reset clipboard in `rbwpw` only in the last invocation (#13185)

The previous behavior would reset the clipboard after 20 seconds always, even
if the `rbwpw` function was called again in between. This commit fixes that
behavior.

Co-authored-by: Marc Cornellà <marc@mcornella.com>

Diff

 1diff --git a/plugins/rbw/rbw.plugin.zsh b/plugins/rbw/rbw.plugin.zsh
 2index b6cecf8b4ec759ef8475a791c3457d7ad7e1d25c..0b55e6e5f6adfa95ef6a25ec416aa8535563c835 100644
 3--- a/plugins/rbw/rbw.plugin.zsh
 4+++ b/plugins/rbw/rbw.plugin.zsh
 5@@ -29,9 +29,24 @@ function rbwpw {
 6     echo "$service not found"
 7     return 1
 8   fi
 9+
10+  # Generate a random identifier for this call to rbwpw
11+  # so we can check if the clipboard content has changed
12+  local _random="$RANDOM" _cache="$ZSH_CACHE_DIR/.rbwpw"
13+  echo -n "$_random" > "$_cache"
14+
15+  # Use clipcopy to copy the password to the clipboard
16   echo -n $pw | clipcopy
17   echo "password for $service copied!"
18-  {sleep 20 && clipcopy </dev/null 2>/dev/null} &|
19+
20+  # Clear the clipboard after 20 seconds, but only if the clipboard hasn't
21+  # changed (if rbwpw hasn't been called again)
22+  {
23+    sleep 20 \
24+    && [[ "$(<"$_cache")" == "$_random" ]] \
25+    && clipcopy </dev/null 2>/dev/null \
26+    && command rm -f "$_cache" &>/dev/null
27+  } &|
28 }
29 
30 function _rbwpw {