-
Notifications
You must be signed in to change notification settings - Fork 788
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Move responsibility for writing in to driver (#2273)
* Move responsibility for writing in to driver * remove driver property * optimization for segments * force terminal * Update src/textual/drivers/_writer_thread.py Co-authored-by: Rodrigo Girão Serrão <5621605+rodrigogiraoserrao@users.noreply.github.com> * no safe box * safe box false * force null file --------- Co-authored-by: Rodrigo Girão Serrão <5621605+rodrigogiraoserrao@users.noreply.github.com>
- Loading branch information
1 parent
6369c37
commit c249548
Showing
6 changed files
with
145 additions
and
95 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,69 @@ | ||
from __future__ import annotations | ||
|
||
import sys | ||
import threading | ||
from queue import Queue | ||
from typing import IO | ||
|
||
from typing_extensions import Final | ||
|
||
MAX_QUEUED_WRITES: Final[int] = 30 | ||
|
||
|
||
class WriterThread(threading.Thread): | ||
"""A thread / file-like to do writes to stdout in the background.""" | ||
|
||
def __init__(self, file: IO[str]) -> None: | ||
super().__init__(daemon=True) | ||
self._queue: Queue[str | None] = Queue(MAX_QUEUED_WRITES) | ||
self._file = file | ||
|
||
def write(self, text: str) -> None: | ||
"""Write text. Text will be enqueued for writing. | ||
Args: | ||
text: Text to write to the file. | ||
""" | ||
self._queue.put(text) | ||
|
||
def isatty(self) -> bool: | ||
"""Pretend to be a terminal. | ||
Returns: | ||
True. | ||
""" | ||
return True | ||
|
||
def fileno(self) -> int: | ||
"""Get file handle number. | ||
Returns: | ||
File number of proxied file. | ||
""" | ||
return self._file.fileno() | ||
|
||
def flush(self) -> None: | ||
"""Flush the file (a no-op, because flush is done in the thread).""" | ||
return | ||
|
||
def run(self) -> None: | ||
"""Run the thread.""" | ||
write = self._file.write | ||
flush = self._file.flush | ||
get = self._queue.get | ||
qsize = self._queue.qsize | ||
# Read from the queue, write to the file. | ||
# Flush when there is a break. | ||
while True: | ||
text: str | None = get() | ||
if text is None: | ||
break | ||
write(text) | ||
if qsize() == 0: | ||
flush() | ||
flush() | ||
|
||
def stop(self) -> None: | ||
"""Stop the thread, and block until it finished.""" | ||
self._queue.put(None) | ||
self.join() |
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
Oops, something went wrong.