-
Notifications
You must be signed in to change notification settings - Fork 5
/
rofi-pass
executable file
·151 lines (117 loc) · 3.89 KB
/
rofi-pass
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
#!/usr/bin/env python3
import subprocess
import time
from pathlib import Path
import pyperclip
PASSWORD_STORE_PATH = Path("~/.config/password-store").expanduser()
class Password:
def __init__(self, file):
self.file = file
self.parent_tuples = self.file.parent.parts[5:]
self.parent = "/".join(self.parent_tuples)
self.name = self.parent + self.file.stem
def get_display_name(self):
display_name = []
for parent in self.parent_tuples:
display_name.append(f"{parent}/")
parent = "".join(display_name)
return f"{parent}<b>{self.file.stem}</b>"
def __repr__(self) -> str:
if self.parent == "password-store":
return f"{self.file.stem}"
return f"{self.parent}/{self.file.stem}"
class Passwords(list):
def __init__(self):
list.__init__(self, self.read_files())
self.display_names = [password.get_display_name() for password in self]
def read_files(self):
files = PASSWORD_STORE_PATH.glob("**/*.gpg")
return sorted([Password(file) for file in files], key=lambda p: p.name)
class Rofi:
def select(self, prompt, options):
optionstr = "\n".join(str(o).replace("\n", " ") for o in options)
args = [
"rofi",
"-markup-rows",
"-matching",
"fuzzy",
"-dmenu",
"-p",
prompt,
"-format",
"s",
"-i",
"-no-fixed-num-lines",
"-markup-rows",
"-kb-row-down",
"Down",
"-kb-custom-1",
"Ctrl+n",
]
result = subprocess.run(
args,
input=optionstr,
stdout=subprocess.PIPE,
universal_newlines=True,
)
returncode = result.returncode
stdout = result.stdout.strip()
selected = stdout.strip()
try:
index = [opt.strip() for opt in options].index(selected)
except ValueError:
index = -1
if returncode == 0:
code = 0
if returncode == 1:
code = -1
if returncode > 9:
code = returncode - 9
else:
code = -1
return code, index, selected
def display_passwords(self):
options = Passwords()
_, index, _ = self.select("Select password", options.display_names)
if index < 0:
return
password = subprocess.run(
["pass", str(options[index])],
input=str(options[index]),
stdout=subprocess.PIPE,
universal_newlines=True,
).stdout.strip()
password_list = password.split("\n")
new_password_list = []
if len(password_list) == 1:
pyperclip.copy(password)
return
else:
passwords = password.split("\n")
for password in passwords:
if ":" not in password:
new_password_list.append("password")
continue
if ":" in password:
password = password.split(":")[0]
new_password_list.append(password)
# display new list of passwords
_, sub_index, _ = self.select("Select password", new_password_list)
if sub_index < 0:
return
selected_password = ""
if sub_index == 0:
selected_password = password_list[0]
else:
selected_password = "".join(password_list[sub_index].split(":")[1:])
selected_password = selected_password.strip()
pyperclip.copy(selected_password)
message_header = f"Password {options[index]} copied to clipboard"
subprocess.run(["notify-send", message_header])
time.sleep(5)
pyperclip.copy("")
def main():
r = Rofi()
r.display_passwords()
if __name__ == "__main__":
main()