Skip to content

Commit

Permalink
Add sphinx.util._io
Browse files Browse the repository at this point in the history
  • Loading branch information
AA-Turner committed Sep 20, 2023
1 parent 8e768e6 commit bf620e9
Showing 1 changed file with 31 additions and 0 deletions.
31 changes: 31 additions & 0 deletions sphinx/util/_io.py
@@ -0,0 +1,31 @@
from typing import TYPE_CHECKING

from sphinx.util.console import strip_colors

if TYPE_CHECKING:
from typing import Protocol

class SupportsWrite(Protocol):
def write(self, text: str, /) -> None:
...


class TeeStripANSI:
"""File-like object writing to two streams."""
def __init__(
self,
stream_term: SupportsWrite,
stream_file: SupportsWrite,
) -> None:
self.stream_term = stream_term
self.stream_file = stream_file

def write(self, text: str, /) -> None:
self.stream_term.write(text)
self.stream_file.write(strip_colors(text))

def flush(self) -> None:
if hasattr(self.stream_term, 'flush'):
self.stream_term.flush()
if hasattr(self.stream_file, 'flush'):
self.stream_file.flush()

0 comments on commit bf620e9

Please sign in to comment.