Skip to content

Commit

Permalink
feat: implement multicharacter input
Browse files Browse the repository at this point in the history
  • Loading branch information
mecaneer23 committed Apr 26, 2024
1 parent 7ed119f commit 6e7da2c
Showing 1 changed file with 48 additions and 0 deletions.
48 changes: 48 additions & 0 deletions tools/new.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
#!/usr/bin/env python3

from sys import stdin
import tty
import termios
from time import time as now
from queue import Queue, Empty as queue_empty
import threading

_SHORT_TIME_SECONDS = 0.01


def _fill_queue(queue: Queue[int]):
while True:
queue.put(ord(stdin.read(1)))


def _main() -> None:
chars: list[int] = []
queue: Queue[int] = Queue()

threading.Thread(target=_fill_queue, args=(queue,), daemon=True).start()

char = queue.get()
if char != 27:
print(f"key: {char}", end="\n\r")
return
esc = now()
chars.append(27)
while now() - esc < _SHORT_TIME_SECONDS:
try:
char = queue.get(timeout=_SHORT_TIME_SECONDS)
except queue_empty:
break
if char not in chars:
chars.append(char)
print(chars, end="\n\r")
chars.clear()


if __name__ == "__main__":
old_settings = termios.tcgetattr(stdin)
try:
tty.setraw(stdin.fileno())
_main()
finally:
termios.tcsetattr(stdin, termios.TCSADRAIN, old_settings)
print("byeeeee")

0 comments on commit 6e7da2c

Please sign in to comment.