Skip to content

Commit 704b6a2

Browse files
committed
Speed up output rendering by removing console.capture()'s from tui output
1 parent c2640a8 commit 704b6a2

1 file changed

Lines changed: 21 additions & 18 deletions

File tree

cecli/tui/widgets/output.py

Lines changed: 21 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -199,12 +199,9 @@ def add_output_styled(self, text: str, styles=None):
199199
if not styles:
200200
styles = dict()
201201

202-
style = RichStyle(**styles)
203-
with self.app.console.capture() as capture:
204-
self.app.console.print(Text(text), style=style)
205-
capture_text = capture.get()
206-
207-
self.output(Padding(capture_text, (0, 0, 0, 2)))
202+
# Create styled Text object directly
203+
styled_text = Text(text, style=RichStyle(**styles))
204+
self.output(Padding(styled_text, (0, 0, 0, 2)))
208205

209206
def add_tool_call(self, lines: list):
210207
"""Add a tool call with themed styling.
@@ -289,23 +286,29 @@ def output(self, text, check_duplicates=True, render_markdown=False):
289286
check_duplicates: If True, check for duplicate newlines before writing
290287
render_markdown: If True and app config allows, render as markdown
291288
"""
289+
# Get plain text for duplicate checking BEFORE any markdown conversion
290+
plain_text = ""
291+
if isinstance(text, str):
292+
plain_text = text
293+
elif isinstance(text, Text):
294+
plain_text = text.plain
295+
elif isinstance(text, Markdown):
296+
# For Markdown objects, we need to get the source markdown text
297+
# Markdown objects have a .markup attribute with the source
298+
plain_text = text.markup if hasattr(text, "markup") else str(text)
299+
else:
300+
# For other types, convert to string
301+
plain_text = str(text)
302+
292303
# Check if we should render as markdown
293304
if render_markdown and hasattr(self.app, "render_markdown") and self.app.render_markdown:
294305
# Only render string content as markdown
295306
if isinstance(text, str):
296307
text = Markdown(text)
297308

298-
with self.app.console.capture() as capture:
299-
self.app.console.print(text)
300-
check = Text(capture.get()).plain
301-
302-
# self.write(str(self._write_history))
303-
# self.write(repr(check))
304-
305-
# Check for duplicate newlines
306-
309+
# Check for duplicate newlines using the plain text we extracted
307310
if check_duplicates and len(self._write_history) >= 2:
308-
nl_check = check in ["", "\n", "\\n"]
311+
nl_check = plain_text in ["", "\n", "\\n"]
309312
nl_last = self._write_history[-1] in ["", "\n", "\\n"]
310313
nl_penultimate = self._write_history[-2] in ["", "\n", "\\n"] or self._write_history[
311314
-2
@@ -317,8 +320,8 @@ def output(self, text, check_duplicates=True, render_markdown=False):
317320
# Call the actual write method
318321
self.write(text)
319322

320-
# Log the write
321-
self._write_history.append(check)
323+
# Log the write using the plain text
324+
self._write_history.append(plain_text)
322325

323326
# Keep history size manageable
324327
if len(self._write_history) > 5:

0 commit comments

Comments
 (0)