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
|
winsizeX = 75
winsizeY = 20
global show_help
show_help = False
password = ""
help_menu = [
"Press m to toggle help",
"Navigation:",
" Arrow keys,",
" WASD,",
" HJKL",
"Press Enter or Space to generate a password",
"Press Shift-Q to quit"
]
import random
import curses
import math
from english_words import english_words_lower_alpha_set
numbers_list = '0123456789'
characters_list = '{}[]()!@#$%^&*,./<>?\\|`~-_=+\'\";:'
stdscr = curses.initscr()
curses.noecho()
curses.curs_set(0)
height, width = stdscr.getmaxyx()
distanceX = width - winsizeX
distanceY = height - winsizeY
mainwin = curses.newwin(0, 0, distanceY, distanceX)
mainwin.mvwin(math.floor(distanceY / 2), math.floor(distanceX / 2))
curses.start_color()
from config import config
options = [
# name value min max
["Words", 2, 2, 12],
["Numbers", 0, 0, 12],
["Special Characters", 0, 0, 12]
]
global curspos
curspos = 0 # cursor position
def Left():
if options[curspos][1] > options[curspos][2]:
options[curspos][1] -= 1
def Right():
if options[curspos][1] < options[curspos][3]:
options[curspos][1] += 1
def Up():
global curspos
if curspos > 0:
curspos -= 1
def Down():
global curspos
if curspos < len(options) - 1:
curspos += 1
def Generate():
global password
password = ""
new_list = []
for i in range(options[0][1]):
new_list.append(random.choice(list(english_words_lower_alpha_set)))
for i in range(options[1][1]):
new_list.append(random.choice(list(numbers_list)))
for i in range(options[2][1]):
new_list.append(random.choice(list(characters_list)))
random.shuffle(new_list)
for item in new_list:
password = password + item
def Render():
mainwin.clear()
mainwin.box()
mainwin.addstr(1, 1, help_menu[0], curses.color_pair(3))
if show_help:
for i in range(len(help_menu)):
if i == 0:
continue
mainwin.addstr(i + 1, 1, help_menu[i], curses.color_pair(3))
else:
for i in range(len(options)):
color = 1
if i == curspos:
mainwin.addstr(i + 2, winsizeX - 3 - len(str(options[i][1])), "<", curses.color_pair(2))
mainwin.addstr(i + 2, winsizeX - 2, ">", curses.color_pair(2))
color = 2
mainwin.addstr(i + 2, 1, options[i][0], curses.color_pair(color))
mainwin.addstr(i + 2, winsizeX - 2 - len(str(options[i][1])), str(options[i][1]), curses.color_pair(1))
mainwin.addstr(winsizeY - 2, 1, password)
while True:
Render()
key = mainwin.getch()
if key in config.key_quit:
break
if key in config.key_left:
Left()
elif key in config.key_right:
Right()
elif key in config.key_up:
Up()
elif key in config.key_down:
Down()
elif key in config.key_enter:
Generate()
elif key in config.key_help:
show_help = not show_help
curses.endwin()
|