-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement watcher mode with negative
-lookahead
values, see help fo…
…r usage
- Loading branch information
1 parent
3d3c445
commit c7eb61c
Showing
6 changed files
with
137 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
# coding=UTF-8 | ||
""" | ||
Author: trickerer (https://github.com/trickerer, https://github.com/trickerer01) | ||
""" | ||
######################################### | ||
# Original solution by Bharel: https://stackoverflow.com/a/70664652 | ||
# | ||
|
||
from asyncio import CancelledError, sleep | ||
from collections.abc import Callable | ||
from contextlib import nullcontext, contextmanager | ||
from platform import system | ||
|
||
__all__ = ('wait_for_key',) | ||
|
||
if system() == 'Windows': | ||
# noinspection PyCompatibility | ||
from msvcrt import getwch, kbhit | ||
|
||
set_terminal_raw = nullcontext | ||
input_ready = kbhit | ||
next_input = getwch | ||
else: | ||
# TODO: test on Linux | ||
import sys | ||
from functools import partial | ||
from select import select | ||
from termios import tcgetattr, tcsetattr, TCSADRAIN | ||
from tty import setraw | ||
|
||
@contextmanager | ||
def set_terminal_raw() -> None: | ||
fd = sys.stdin.fileno() | ||
old_settings = tcgetattr(fd) | ||
try: | ||
setraw(sys.stdin.fileno()) | ||
yield | ||
finally: | ||
tcsetattr(fd, TCSADRAIN, old_settings) | ||
|
||
def input_ready() -> bool: | ||
return select([sys.stdin], [], [], 0) == ([sys.stdin], [], []) | ||
|
||
next_input = partial(sys.stdin.read, 1) | ||
|
||
|
||
async def wait_for_key(key: str, callback: Callable[[], None], *, secondary=False) -> None: | ||
try: | ||
with set_terminal_raw(): | ||
ch = '' | ||
while ch != key: | ||
await sleep(1.0) | ||
while input_ready(): | ||
ch = next_input() | ||
if secondary: | ||
while input_ready(): | ||
next_input() | ||
callback() | ||
else: | ||
await wait_for_key(key, callback, secondary=True) | ||
except CancelledError: | ||
pass | ||
|
||
# | ||
# | ||
######################################### |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters