olan-events 0.1.0__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- olan_events-0.1.0/PKG-INFO +5 -0
- olan_events-0.1.0/events/__init__.py +1 -0
- olan_events-0.1.0/events/events.py +79 -0
- olan_events-0.1.0/olan_events.egg-info/PKG-INFO +5 -0
- olan_events-0.1.0/olan_events.egg-info/SOURCES.txt +7 -0
- olan_events-0.1.0/olan_events.egg-info/dependency_links.txt +1 -0
- olan_events-0.1.0/olan_events.egg-info/top_level.txt +1 -0
- olan_events-0.1.0/pyproject.toml +9 -0
- olan_events-0.1.0/setup.cfg +4 -0
|
@@ -0,0 +1 @@
|
|
|
1
|
+
from .events import *
|
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
import sys
|
|
2
|
+
import tty
|
|
3
|
+
import termios
|
|
4
|
+
from dataclasses import dataclass
|
|
5
|
+
|
|
6
|
+
# Event types
|
|
7
|
+
TypeKey = "key"
|
|
8
|
+
NewLine = "newline"
|
|
9
|
+
EOF = "eof"
|
|
10
|
+
KI = "keyboard_interrupt"
|
|
11
|
+
|
|
12
|
+
_callback = None
|
|
13
|
+
|
|
14
|
+
|
|
15
|
+
@dataclass
|
|
16
|
+
class Event:
|
|
17
|
+
type: str
|
|
18
|
+
input: str | None = None
|
|
19
|
+
|
|
20
|
+
|
|
21
|
+
def set(func):
|
|
22
|
+
global _callback
|
|
23
|
+
_callback = func
|
|
24
|
+
return func
|
|
25
|
+
|
|
26
|
+
|
|
27
|
+
def getev():
|
|
28
|
+
fd = sys.stdin.fileno()
|
|
29
|
+
old = termios.tcgetattr(fd)
|
|
30
|
+
|
|
31
|
+
try:
|
|
32
|
+
tty.setraw(fd)
|
|
33
|
+
|
|
34
|
+
while True:
|
|
35
|
+
ch = sys.stdin.read(1)
|
|
36
|
+
|
|
37
|
+
# Ctrl+C
|
|
38
|
+
if ch == "\x03":
|
|
39
|
+
return Event(KI)
|
|
40
|
+
|
|
41
|
+
# Ctrl+D
|
|
42
|
+
if ch == "\x04":
|
|
43
|
+
return Event(EOF)
|
|
44
|
+
|
|
45
|
+
# Enter
|
|
46
|
+
if ch in ("\r", "\n"):
|
|
47
|
+
return Event(NewLine)
|
|
48
|
+
|
|
49
|
+
# stdin closed
|
|
50
|
+
if ch == "":
|
|
51
|
+
return Event(EOF)
|
|
52
|
+
|
|
53
|
+
# Normal key
|
|
54
|
+
return Event(TypeKey, ch)
|
|
55
|
+
|
|
56
|
+
finally:
|
|
57
|
+
termios.tcsetattr(fd, termios.TCSADRAIN, old)
|
|
58
|
+
|
|
59
|
+
|
|
60
|
+
def run():
|
|
61
|
+
if _callback is None:
|
|
62
|
+
raise RuntimeError("No event handler registered. Use @events.set.")
|
|
63
|
+
|
|
64
|
+
while True:
|
|
65
|
+
event = getev()
|
|
66
|
+
_callback(event)
|
|
67
|
+
|
|
68
|
+
def post(name):
|
|
69
|
+
return name
|
|
70
|
+
|
|
71
|
+
def do(event, input=None):
|
|
72
|
+
"""
|
|
73
|
+
Dispatch an event immediately.
|
|
74
|
+
"""
|
|
75
|
+
if _callback is None:
|
|
76
|
+
raise RuntimeError("No callback registered. Use @events.set.")
|
|
77
|
+
|
|
78
|
+
_callback(Event(event, input))
|
|
79
|
+
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
events
|