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()