2015-05-27 01:11:31 +00:00
|
|
|
import curses
|
2015-05-31 21:54:43 +00:00
|
|
|
import time
|
|
|
|
import sys
|
2024-04-27 21:00:01 +00:00
|
|
|
from pathlib import Path as _Path
|
|
|
|
from playsound import playsound as _play
|
|
|
|
from threading import Thread
|
|
|
|
from time import sleep
|
|
|
|
|
2015-05-27 01:11:31 +00:00
|
|
|
|
2015-05-27 01:28:59 +00:00
|
|
|
LETTER_PAUSE = 5
|
2015-05-27 01:11:31 +00:00
|
|
|
|
2024-04-28 15:02:47 +00:00
|
|
|
INPUT_PAUSE = 500 # ms
|
2015-06-14 21:05:56 +00:00
|
|
|
|
2015-06-14 22:31:01 +00:00
|
|
|
TYPE_DELAY = 40
|
2015-06-14 21:05:56 +00:00
|
|
|
|
2024-04-28 15:02:47 +00:00
|
|
|
HIDDEN_MASK = "*"
|
2015-06-14 21:05:56 +00:00
|
|
|
|
|
|
|
NEWLINE = 10
|
|
|
|
|
2024-04-28 01:26:44 +00:00
|
|
|
SPACE = 32
|
|
|
|
|
2024-04-27 21:00:01 +00:00
|
|
|
DELETE = 330
|
|
|
|
|
|
|
|
BACKSPACE = 263
|
|
|
|
|
|
|
|
_playing = False
|
|
|
|
|
|
|
|
_soundQueue = []
|
2015-06-14 21:05:56 +00:00
|
|
|
|
2024-04-27 21:00:01 +00:00
|
|
|
_queueRunning = False
|
|
|
|
|
2024-04-28 15:02:47 +00:00
|
|
|
|
|
|
|
def slowWrite(window, text, pause=LETTER_PAUSE, fake_user=False, silent=False):
|
2015-05-27 01:11:31 +00:00
|
|
|
"""
|
2024-04-27 21:31:26 +00:00
|
|
|
wrapper for curses.addstr() which writes the text slowly
|
2015-05-27 01:11:31 +00:00
|
|
|
"""
|
2024-04-28 01:14:57 +00:00
|
|
|
width = window.getmaxyx()[1]
|
|
|
|
texts = {0: text}
|
|
|
|
if len(text) > width:
|
|
|
|
while any(len(texts[ind]) > width for ind in texts.keys()):
|
2024-04-28 15:02:47 +00:00
|
|
|
i = 0
|
2024-04-28 01:14:57 +00:00
|
|
|
for ind in texts.keys():
|
2024-04-28 15:02:47 +00:00
|
|
|
if len(texts[ind]) > width:
|
|
|
|
break
|
|
|
|
i += 1
|
|
|
|
texts[i + 1] = (
|
|
|
|
f"{texts[i].split()[-1].strip()}{' '+texts[i+1].strip() if texts.get(i+1) else ''}\n"
|
|
|
|
)
|
|
|
|
texts[i] = " ".join(texts[i].split()[:-1]).strip() + "\n"
|
2024-04-28 01:14:57 +00:00
|
|
|
for txt in texts.values():
|
2024-04-28 01:28:54 +00:00
|
|
|
if not fake_user and not silent:
|
|
|
|
addSound("beep")
|
2024-04-28 01:14:57 +00:00
|
|
|
for i in range(len(txt)):
|
|
|
|
window.addstr(txt[i])
|
|
|
|
window.refresh()
|
|
|
|
curses.napms(pause)
|
2024-04-27 22:01:54 +00:00
|
|
|
if fake_user and not silent:
|
2024-04-27 21:00:01 +00:00
|
|
|
_playSound("keyenter", True)
|
2015-06-14 21:05:56 +00:00
|
|
|
|
2024-04-28 15:02:47 +00:00
|
|
|
|
|
|
|
def upperInput(window, hidden=False, can_newline=True):
|
2015-06-14 21:05:56 +00:00
|
|
|
"""
|
|
|
|
Reads user input until enter key is pressed. Echoes the input in upper case
|
|
|
|
|
|
|
|
hidden - if true the output will be masked
|
|
|
|
can_newline - if true the input is followed by a newline and the screen is
|
|
|
|
scrolled if necessary
|
|
|
|
"""
|
|
|
|
inchar = 0
|
2024-04-28 15:02:47 +00:00
|
|
|
instr = ""
|
2015-06-14 21:05:56 +00:00
|
|
|
while inchar != NEWLINE:
|
|
|
|
inchar = window.getch()
|
|
|
|
# convert lower case to upper
|
|
|
|
if inchar > 96 and inchar < 123:
|
|
|
|
inchar -= 32
|
|
|
|
# deal with backspace
|
2024-04-27 21:00:01 +00:00
|
|
|
if inchar in [DELETE, BACKSPACE]:
|
2015-06-14 21:05:56 +00:00
|
|
|
if len(instr) > 0:
|
2024-04-28 15:02:47 +00:00
|
|
|
# addSound("keyenter")
|
2015-06-14 21:05:56 +00:00
|
|
|
instr = instr[:-1]
|
|
|
|
cur = window.getyx()
|
|
|
|
window.move(cur[0], cur[1] - 1)
|
|
|
|
window.clrtobot()
|
|
|
|
else:
|
|
|
|
continue
|
|
|
|
elif inchar > 255:
|
|
|
|
continue
|
|
|
|
# output the character
|
|
|
|
elif inchar != NEWLINE:
|
2024-04-28 15:02:47 +00:00
|
|
|
# addSound("keyenter")
|
2015-06-14 21:05:56 +00:00
|
|
|
instr += chr(inchar)
|
|
|
|
if hidden:
|
|
|
|
window.addch(HIDDEN_MASK)
|
|
|
|
else:
|
|
|
|
window.addch(inchar)
|
|
|
|
elif can_newline:
|
2024-04-28 15:02:47 +00:00
|
|
|
# addSound("keyenter")
|
2015-06-14 21:05:56 +00:00
|
|
|
window.addch(NEWLINE)
|
|
|
|
return instr
|
2015-06-14 22:31:01 +00:00
|
|
|
|
2024-04-28 15:02:47 +00:00
|
|
|
|
|
|
|
def centeredWrite(window, text, pause=LETTER_PAUSE, silent=False):
|
2015-06-14 22:31:01 +00:00
|
|
|
"""
|
|
|
|
Writes to the current line but centers the text
|
|
|
|
"""
|
|
|
|
width = window.getmaxyx()[1]
|
2021-05-19 18:45:06 +00:00
|
|
|
window.move(window.getyx()[0], int(width / 2 - len(text) / 2))
|
2024-04-28 15:02:47 +00:00
|
|
|
slowWrite(window, text, pause, silent=silent)
|
2024-04-27 21:00:01 +00:00
|
|
|
|
|
|
|
|
|
|
|
def _soundCheck():
|
|
|
|
"""
|
|
|
|
Internal use - Checks if the user explicity disabled sound or not
|
|
|
|
"""
|
2024-04-28 15:02:47 +00:00
|
|
|
return "--no-sound" not in sys.argv
|
2024-04-27 21:00:01 +00:00
|
|
|
|
|
|
|
|
2024-04-28 15:02:47 +00:00
|
|
|
def _playSound(file, block=False):
|
2024-04-27 21:00:01 +00:00
|
|
|
"""
|
|
|
|
Internal use - plays a specific sound from the current directory
|
|
|
|
"""
|
|
|
|
if _soundCheck():
|
2024-04-28 15:02:47 +00:00
|
|
|
_play(str(_Path.cwd() / f"audio/{file}.wav"), block)
|
2024-04-27 21:00:01 +00:00
|
|
|
|
|
|
|
|
|
|
|
def _playQueue():
|
|
|
|
"""
|
|
|
|
Internal use - play sounds from a queue
|
|
|
|
"""
|
|
|
|
global _queueRunning
|
|
|
|
if _queueRunning:
|
|
|
|
return
|
|
|
|
_queueRunning = True
|
2024-04-27 21:35:30 +00:00
|
|
|
while _soundCheck():
|
2024-04-27 21:00:01 +00:00
|
|
|
if len(_soundQueue) > 0:
|
|
|
|
_playSound(_soundQueue[0], True)
|
|
|
|
_soundQueue.pop(0)
|
|
|
|
else:
|
|
|
|
sleep(0.02)
|
|
|
|
|
|
|
|
|
2024-04-27 21:43:53 +00:00
|
|
|
def queueIsEmpty():
|
2024-04-27 21:00:01 +00:00
|
|
|
"""
|
|
|
|
Check if the sound queue is empty
|
|
|
|
"""
|
|
|
|
return len(_soundQueue) == 0
|
|
|
|
|
|
|
|
|
|
|
|
def soundTest():
|
|
|
|
addSound("beep")
|
|
|
|
addSound("beep")
|
|
|
|
addSound("keyenter")
|
|
|
|
addSound("keyenter")
|
|
|
|
addSound("wrongpass")
|
|
|
|
addSound("wrongpass")
|
|
|
|
addSound("correctpass")
|
|
|
|
addSound("correctpass")
|
|
|
|
addSound("poweron")
|
|
|
|
addSound("poweron")
|
|
|
|
addSound("poweroff")
|
|
|
|
addSound("poweroff")
|
2024-04-27 21:43:53 +00:00
|
|
|
while not queueIsEmpty():
|
2024-04-27 21:00:01 +00:00
|
|
|
sleep(0.2)
|
|
|
|
|
|
|
|
|
|
|
|
def addSound(file):
|
|
|
|
"""
|
|
|
|
Add sounds to the queue
|
|
|
|
"""
|
2024-04-27 21:37:01 +00:00
|
|
|
if _soundCheck():
|
2024-04-27 21:35:30 +00:00
|
|
|
_soundQueue.extend([file])
|
2024-04-27 21:00:01 +00:00
|
|
|
|
|
|
|
|
|
|
|
_queueMgr = Thread(target=_playQueue)
|
|
|
|
_queueMgr.daemon = True
|
|
|
|
_queueMgr.start()
|