MCPcopy Create free account
hub / github.com/HelpFreedom/youthub / KeyWatcher

Class KeyWatcher

tui_player.py:131–165  ·  view source on GitHub ↗

Raw-mode stdin reader running in a thread. Sets `event` on quit keys.

Source from the content-addressed store, hash-verified

129
130
131class KeyWatcher:
132 """Raw-mode stdin reader running in a thread. Sets `event` on quit keys."""
133
134 def __init__(self):
135 self.event = threading.Event()
136 self.pause = threading.Event()
137 self._old_termios = None
138 self._thread = None
139
140 def start(self):
141 if not sys.stdin.isatty():
142 return
143 self._old_termios = termios.tcgetattr(sys.stdin.fileno())
144 tty.setcbreak(sys.stdin.fileno())
145 self._thread = threading.Thread(target=self._loop, daemon=True)
146 self._thread.start()
147
148 def _loop(self):
149 while not self.event.is_set():
150 r, _, _ = select.select([sys.stdin], [], [], 0.1)
151 if not r:
152 continue
153 ch = sys.stdin.read(1)
154 if ch in ("q", "Q", "\x03", "\x04"):
155 self.event.set()
156 return
157 if ch == " ":
158 if self.pause.is_set():
159 self.pause.clear()
160 else:
161 self.pause.set()
162
163 def stop(self):
164 if self._old_termios is not None:
165 termios.tcsetattr(sys.stdin.fileno(), termios.TCSADRAIN, self._old_termios)
166
167
168def parse_args():

Callers 1

mainFunction · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected