mirror of
https://github.com/Textualize/textual.git
synced 2025-10-17 02:38:12 +03:00
docstring
This commit is contained in:
26
src/textual/_wait.py
Normal file
26
src/textual/_wait.py
Normal file
@@ -0,0 +1,26 @@
|
|||||||
|
from asyncio import sleep
|
||||||
|
from time import process_time, time
|
||||||
|
|
||||||
|
|
||||||
|
SLEEP_GRANULARITY: float = 1 / 50
|
||||||
|
SLEEP_IDLE: float = SLEEP_GRANULARITY / 2.0
|
||||||
|
|
||||||
|
|
||||||
|
async def wait_for_idle(min_sleep: float = 0.01, max_sleep: float = 1) -> None:
|
||||||
|
"""Wait until the cpu isn't working very hard.
|
||||||
|
|
||||||
|
Args:
|
||||||
|
min_sleep: Minimum time to wait. Defaults to 0.01.
|
||||||
|
max_sleep: Maximum time to wait. Defaults to 1.
|
||||||
|
"""
|
||||||
|
start_time = time()
|
||||||
|
|
||||||
|
while True:
|
||||||
|
cpu_time = process_time()
|
||||||
|
await sleep(SLEEP_GRANULARITY)
|
||||||
|
cpu_elapsed = process_time() - cpu_time
|
||||||
|
elapsed_time = time() - start_time
|
||||||
|
if elapsed_time >= max_sleep:
|
||||||
|
break
|
||||||
|
if elapsed_time > min_sleep and cpu_elapsed < SLEEP_IDLE:
|
||||||
|
break
|
||||||
@@ -68,6 +68,7 @@ from .messages import CallbackType
|
|||||||
from .reactive import Reactive
|
from .reactive import Reactive
|
||||||
from .renderables.blank import Blank
|
from .renderables.blank import Blank
|
||||||
from .screen import Screen
|
from .screen import Screen
|
||||||
|
from ._wait import wait_for_idle
|
||||||
from .widget import AwaitMount, Widget
|
from .widget import AwaitMount, Widget
|
||||||
|
|
||||||
if TYPE_CHECKING:
|
if TYPE_CHECKING:
|
||||||
@@ -796,11 +797,11 @@ class App(Generic[ReturnType], DOMNode):
|
|||||||
app = self
|
app = self
|
||||||
driver = app._driver
|
driver = app._driver
|
||||||
assert driver is not None
|
assert driver is not None
|
||||||
await asyncio.sleep(0.02)
|
# await asyncio.sleep(0.02)
|
||||||
|
await wait_for_idle(0)
|
||||||
for key in keys:
|
for key in keys:
|
||||||
if key == "_":
|
if key == "_":
|
||||||
print("(pause 50ms)")
|
continue
|
||||||
await asyncio.sleep(0.05)
|
|
||||||
elif key.startswith("wait:"):
|
elif key.startswith("wait:"):
|
||||||
_, wait_ms = key.split(":")
|
_, wait_ms = key.split(":")
|
||||||
print(f"(pause {wait_ms}ms)")
|
print(f"(pause {wait_ms}ms)")
|
||||||
@@ -822,13 +823,8 @@ class App(Generic[ReturnType], DOMNode):
|
|||||||
print(f"press {key!r} (char={char!r})")
|
print(f"press {key!r} (char={char!r})")
|
||||||
key_event = events.Key(app, key, char)
|
key_event = events.Key(app, key, char)
|
||||||
driver.send_event(key_event)
|
driver.send_event(key_event)
|
||||||
# TODO: A bit of a fudge - extra sleep after tabbing to help guard against race
|
await wait_for_idle(0)
|
||||||
# condition between widget-level key handling and app/screen level handling.
|
|
||||||
# More information here: https://github.com/Textualize/textual/issues/1009
|
|
||||||
# This conditional sleep can be removed after that issue is closed.
|
|
||||||
if key == "tab":
|
|
||||||
await asyncio.sleep(0.05)
|
|
||||||
await asyncio.sleep(0.025)
|
|
||||||
await app._animator.wait_for_idle()
|
await app._animator.wait_for_idle()
|
||||||
|
|
||||||
@asynccontextmanager
|
@asynccontextmanager
|
||||||
|
|||||||
@@ -6,6 +6,7 @@ import asyncio
|
|||||||
from typing import Generic
|
from typing import Generic
|
||||||
|
|
||||||
from .app import App, ReturnType
|
from .app import App, ReturnType
|
||||||
|
from ._wait import wait_for_idle
|
||||||
|
|
||||||
|
|
||||||
@rich.repr.auto(angular=True)
|
@rich.repr.auto(angular=True)
|
||||||
@@ -52,4 +53,5 @@ class Pilot(Generic[ReturnType]):
|
|||||||
Args:
|
Args:
|
||||||
result: The app result returned by `run` or `run_async`.
|
result: The app result returned by `run` or `run_async`.
|
||||||
"""
|
"""
|
||||||
|
await wait_for_idle()
|
||||||
self.app.exit(result)
|
self.app.exit(result)
|
||||||
|
|||||||
@@ -190,6 +190,6 @@ def test_demo(snap_compare):
|
|||||||
"""Test the demo app (python -m textual)"""
|
"""Test the demo app (python -m textual)"""
|
||||||
assert snap_compare(
|
assert snap_compare(
|
||||||
Path("../../src/textual/demo.py"),
|
Path("../../src/textual/demo.py"),
|
||||||
press=["down", "down", "down", "_", "_", "_"],
|
press=["down", "down", "down"],
|
||||||
terminal_size=(100, 30),
|
terminal_size=(100, 30),
|
||||||
)
|
)
|
||||||
|
|||||||
Reference in New Issue
Block a user