Unverified Commit a17841ac authored by Vladimir Blagojevic's avatar Vladimir Blagojevic Committed by GitHub
Browse files

Generate: Enable easier TextStreamer customization (#22516)

parent 80d1319e
...@@ -88,7 +88,7 @@ class TextStreamer(BaseStreamer): ...@@ -88,7 +88,7 @@ class TextStreamer(BaseStreamer):
printable_text = text[self.print_len : text.rfind(" ") + 1] printable_text = text[self.print_len : text.rfind(" ") + 1]
self.print_len += len(printable_text) self.print_len += len(printable_text)
print(printable_text, flush=True, end="") self.on_finalized_text(printable_text)
def end(self): def end(self):
"""Flushes any remaining cache and prints a newline to stdout.""" """Flushes any remaining cache and prints a newline to stdout."""
...@@ -102,7 +102,11 @@ class TextStreamer(BaseStreamer): ...@@ -102,7 +102,11 @@ class TextStreamer(BaseStreamer):
printable_text = "" printable_text = ""
# Print a newline (and the remaining text, if any) # Print a newline (and the remaining text, if any)
print(printable_text, flush=True) self.on_finalized_text(printable_text, stream_end=True)
def on_finalized_text(self, token: str, stream_end: bool = False):
"""Prints the new text to stdout."""
print(token, flush=True, end="" if not stream_end else None)
class TextIteratorStreamer(BaseStreamer): class TextIteratorStreamer(BaseStreamer):
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment