docstring

This commit is contained in:
Will McGugan
2023-01-26 16:01:48 +01:00
parent d8e6cfdd50
commit 41be84b1a5
4 changed files with 35 additions and 11 deletions

26
src/textual/_wait.py Normal file
View 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

View File

@@ -68,6 +68,7 @@ from .messages import CallbackType
from .reactive import Reactive
from .renderables.blank import Blank
from .screen import Screen
from ._wait import wait_for_idle
from .widget import AwaitMount, Widget
if TYPE_CHECKING:
@@ -796,11 +797,11 @@ class App(Generic[ReturnType], DOMNode):
app = self
driver = app._driver
assert driver is not None
await asyncio.sleep(0.02)
# await asyncio.sleep(0.02)
await wait_for_idle(0)
for key in keys:
if key == "_":
print("(pause 50ms)")
await asyncio.sleep(0.05)
continue
elif key.startswith("wait:"):
_, wait_ms = key.split(":")
print(f"(pause {wait_ms}ms)")
@@ -822,13 +823,8 @@ class App(Generic[ReturnType], DOMNode):
print(f"press {key!r} (char={char!r})")
key_event = events.Key(app, key, char)
driver.send_event(key_event)
# TODO: A bit of a fudge - extra sleep after tabbing to help guard against race
# 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 wait_for_idle(0)
await app._animator.wait_for_idle()
@asynccontextmanager

View File

@@ -6,6 +6,7 @@ import asyncio
from typing import Generic
from .app import App, ReturnType
from ._wait import wait_for_idle
@rich.repr.auto(angular=True)
@@ -52,4 +53,5 @@ class Pilot(Generic[ReturnType]):
Args:
result: The app result returned by `run` or `run_async`.
"""
await wait_for_idle()
self.app.exit(result)

View File

@@ -190,6 +190,6 @@ def test_demo(snap_compare):
"""Test the demo app (python -m textual)"""
assert snap_compare(
Path("../../src/textual/demo.py"),
press=["down", "down", "down", "_", "_", "_"],
press=["down", "down", "down"],
terminal_size=(100, 30),
)