Hello everybody,

I recently customized the passmenu script arrange the password entries
in dmenu in order of their access counts. These statistics are stored in
~/.local/share/passmenu/stats.

Now I want to share my patch against the current version of passmenu
with you and would like to hear some comments from you.

I'm afraid my bash-scripting capabilities are not the best - so if you
have any improvement suggestions, please let me know!

The patch uses the bash's associative array feature, which was
introduced in bash 4 so this would only work with bash version 4 or
later installed.

Best regards,

Paul
>From 2e48d539f20442f7b66016ebb796fc4f1f51fc46 Mon Sep 17 00:00:00 2001
From: schaeferpp <[email protected]>
Date: Fri, 30 Jun 2017 11:25:55 +0200
Subject: [PATCH] Passmenu: Sort the passwords based on their access count

Store the access count of every password in the store at
~/.local/share/passmenu/stats and use these data to reorder the entries
in the dmenu.
---
 contrib/dmenu/passmenu | 45 +++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 45 insertions(+)

diff --git a/contrib/dmenu/passmenu b/contrib/dmenu/passmenu
index 7a9c517..b2dfc1b 100755
--- a/contrib/dmenu/passmenu
+++ b/contrib/dmenu/passmenu
@@ -13,9 +13,49 @@ password_files=( "$prefix"/**/*.gpg )
 password_files=( "${password_files[@]#"$prefix"/}" )
 password_files=( "${password_files[@]%.gpg}" )
 
+declare -A ranked_files
+
+function print_ranked() {
+    for file in "${!ranked_files[@]}"; do
+        echo "${ranked_files[$file]}|$file"
+    done | sort -rn
+}
+
+if [ -e ~/.local/share/passmenu/stats ]; then
+    for f in "${password_files[@]}"; do
+        ranked_files[$f]=0
+    done
+
+    while read line; do
+        stat=$(echo "$line" | cut -d'|' -f 1)
+        filename=$(echo "$line" | cut -d'|' -f 2)
+        # If there is a valid line in the file
+        # and the file exists in password_files, update the rank
+        if [[ -n "$filename" ]]; then
+            if [[ -n "${ranked_files[$filename]}" ]]; then
+                ranked_files["$filename"]="$stat"
+            else
+                echo "'$filename'" not found
+            fi
+        fi
+    done < ~/.local/share/passmenu/stats
+
+    password_files=()
+    while read line; do
+        filename=$(echo "$line" | cut -d'|' -f 2)
+        password_files+=("$filename")
+    done < <(print_ranked)
+    #echo ${password_files[@]}
+else
+    mkdir -p ~/.local/share/passmenu
+    touch ~/.local/share/passmenu/stats
+fi
+
+
 password=$(printf '%s\n' "${password_files[@]}" | dmenu "$@")
 
 [[ -n $password ]] || exit
+ranked_files[$password]=$((${ranked_files[$password]}+1))
 
 if [[ $typeit -eq 0 ]]; then
 	pass show -c "$password" 2>/dev/null
@@ -23,3 +63,8 @@ else
 	pass show "$password" | { read -r pass; printf %s "$pass"; } |
 		xdotool type --clearmodifiers --file -
 fi
+
+# Store updated statistics to file
+if [ -e ~/.local/share/passmenu/stats ]; then
+    print_ranked > ~/.local/share/passmenu/stats
+fi
-- 
2.13.2

_______________________________________________
Password-Store mailing list
[email protected]
https://lists.zx2c4.com/mailman/listinfo/password-store

Reply via email to