Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/env python3
- # Version 6
- # POSTED ONLINE: https://pastebin.com/g10jVftx
- # Windows: $ pip install pyperclip asciimatics
- # Linux: $ sudo pacman -S python-pyperclip
- # $ sudo pacman -S wl-clipboard <-- Pyperclip has issues with KDE + Wayland. Installing this seems to get it to work.
- # $ yay -S python-asciimatics
- # NOTE: For Linux you also need to install a copy/paste mechanism if you don't have one. KDE has /bin/klipper installed by default, otherwise try [$ sudo pacman -S xclip] or [$ sudo pacman -S xsel].
- # If you get error "Cannot find '.setClipboardContents' in object /klipper at org.kde.klipper",
- # its because klipper isn't running on startup. Try Start > klipper and see if the problem goes away.
- # To display klipper in taskbar right click the ^ > Configure System Tray... > Entries > Clipboard: [Show when relevant].
- import pyperclip
- from asciimatics.screen import Screen
- from asciimatics.event import KeyboardEvent
- # -----
- def draw_menu(screen, menuItems, selectedIndex, title=''):
- # Clear screen.
- clear(screen)
- # Draw title.
- if title:
- writeline(screen, title)
- # Draw menu items.
- for i in range(len(menuItems)):
- writeline(screen, f' {"●" if i == selectedIndex else "○"} {menuItems[i]}', Screen.COLOUR_GREEN if i == selectedIndex else Screen.COLOUR_CYAN)
- writeline(screen)
- # Draw usage information.
- writeline(screen, ' Make → Space', Screen.COLOUR_YELLOW)
- writeline(screen, ' Copy → Enter', Screen.COLOUR_YELLOW)
- writeline(screen, ' Rise → Up', Screen.COLOUR_YELLOW)
- writeline(screen, ' Fall → Down', Screen.COLOUR_YELLOW)
- writeline(screen, ' Smol → Left', Screen.COLOUR_YELLOW)
- writeline(screen, ' Swol → Right', Screen.COLOUR_YELLOW)
- writeline(screen, ' Quit → Escape', Screen.COLOUR_YELLOW)
- # Draw screen.
- screen.refresh()
- # -----
- def clear(screen):
- global current_line
- current_line = 0
- screen.clear()
- # -----
- current_line = 0
- def writeline(screen, text='', color=Screen.COLOUR_WHITE):
- global current_line
- lines = text.split('\n')
- for i, line in enumerate(lines):
- screen.print_at(line, 0, current_line, color)
- current_line += 1
- # -----
- from datetime import datetime
- def generate_timestamped_passwords(password_length, count=5):
- dt = datetime.now()
- SHORT_TIME_STAMP = dt.strftime('%y%m%d')
- SHORT_TIME_STAMP_LENGTH = len(SHORT_TIME_STAMP)
- reducedLength = password_length - SHORT_TIME_STAMP_LENGTH
- passwords = [SHORT_TIME_STAMP + generate_password(reducedLength) for i in range(count)]
- LARGE_TIME_STAMP = dt.strftime('%Y-%m-%d %H:%M:%S')
- timestamp = f' Created: {LARGE_TIME_STAMP}\n'
- timestamp += f' Length: {password_length}\n'
- return passwords, timestamp
- # -----
- import string
- import secrets
- def generate_password(password_length):
- # Comment out or add usable characters here.
- PASSWORD_CHARACTERS = [
- string.ascii_lowercase,
- string.ascii_uppercase,
- string.digits,
- string.punctuation,
- ]
- characters = "".join(PASSWORD_CHARACTERS)
- # SOURCE: https://docs.python.org/3/library/secrets.html
- password = ''.join(secrets.choice(characters) for i in range(password_length))
- return password
- # -----
- def wait_for_any_keypress(screen):
- writeline(screen, ' Press any key to continue . . . ')
- screen.refresh()
- # Wait for user keyboard input indefinitely.
- while True:
- screen.wait_for_input(5) # Sleep for this many seconds while waiting for input. This prevents 100% CPU usage from the loop.
- ev = screen.get_event()
- if isinstance(ev, KeyboardEvent):
- break
- # -----
- MIN_PASSWORD_LENGTH = 8
- def main(screen):
- password_length = 23
- menuItems, timestamp = generate_timestamped_passwords(password_length)
- selectedIndex = 0
- draw_menu(screen, menuItems, selectedIndex, timestamp) # Initial drawing.
- while True:
- # Get keyboard input.
- screen.wait_for_input(5) # Sleep for this many seconds while waiting for input. This prevents 100% CPU usage from the loop.
- ev = screen.get_event()
- if not isinstance(ev, KeyboardEvent):
- continue
- # If Escape key is pressed.
- elif ev.key_code == -1:
- break
- # If Space key is pressed.
- elif ev.key_code == ord(' '):
- menuItems, timestamp = generate_timestamped_passwords(password_length)
- # If Up key is pressed.
- elif ev.key_code == Screen.KEY_UP:
- # Loop around backwards.
- selectedIndex = (selectedIndex - 1 + len(menuItems)) % len(menuItems)
- # If Down key is pressed.
- elif ev.key_code == Screen.KEY_DOWN:
- # Loop around forwards.
- selectedIndex = (selectedIndex + 1) % len(menuItems)
- # If Left key is pressed.
- elif ev.key_code == Screen.KEY_LEFT:
- # Reduce length of passwords.
- password_length = max(MIN_PASSWORD_LENGTH, password_length - 1)
- menuItems, timestamp = generate_timestamped_passwords(password_length)
- # If Right key is pressed.
- elif ev.key_code == Screen.KEY_RIGHT:
- # Increase length of passwords.
- password_length = password_length + 1
- menuItems, timestamp = generate_timestamped_passwords(password_length)
- # If Enter key is pressed.
- # NOTE: On Windows the enter key appears to be \r, and on Linux its \n.
- elif ev.key_code == ord('\r') or ev.key_code == ord('\n'):
- pyperclip.copy(menuItems[selectedIndex])
- writeline(screen, f"\n Copied {menuItems[selectedIndex]} to clipboard.\n")
- wait_for_any_keypress(screen)
- # Else an unexpected key is pressed.
- # else:
- # try:
- # writeline(screen, "\n The pressed key '{}' {} is not associated with a menu function.\n".format(chr(ev.key_code), ev.key_code))
- # except ValueError:
- # writeline(screen, "\n The pressed key {} is not associated with a menu function.\n".format(ev.key_code))
- # wait_for_any_keypress(screen)
- draw_menu(screen, menuItems, selectedIndex, timestamp)
- if __name__ == '__main__':
- Screen.wrapper(main)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement