Skip to content

Commit edd225a

Browse files
committed
Improve shell command output handling to prevent screen flicker
- Buffer shell command output instead of streaming it - Yield shell output as a single chunk after completion - Allow truncation to happen once instead of per-chunk - Maintain streaming behavior for non-shell languages This significantly improves the user experience when viewing large files with commands like 'type' or 'cat' by preventing excessive screen updates. Note: Long-running shell commands won't show progress until completion. Related: #issue_number (if there is one)
1 parent 6b1acdd commit edd225a

File tree

1 file changed

+21
-1
lines changed

1 file changed

+21
-1
lines changed

interpreter/core/computer/terminal/terminal.py

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,10 @@ def _streaming_run(self, language, code, display=False):
163163
else:
164164
self._active_languages[language] = lang_class()
165165
try:
166+
# Buffer for shell output - collect all output before displaying to prevent screen flicker
167+
# Note: This means shell commands won't show progress until completion
168+
shell_output = ""
169+
166170
for chunk in self._active_languages[language].run(code):
167171
# self.format_to_recipient can format some messages as having a certain recipient.
168172
# Here we add that to the LMC messages:
@@ -180,16 +184,32 @@ def _streaming_run(self, language, code, display=False):
180184
+ content.split("@@@HIDE_TRACEBACK@@@")[-1].strip()
181185
)
182186

187+
# For shell commands, buffer the output instead of streaming
188+
# This prevents screen flicker with large outputs like 'type' or 'cat'
189+
if language == "shell":
190+
shell_output += chunk["content"]
191+
continue
192+
193+
# For non-shell output or non-output chunks, yield normally
183194
yield chunk
184195

185-
# Print it also if display = True
196+
# Print if display=True (but not for shell output which is handled separately)
186197
if (
187198
display
188199
and chunk.get("format") != "active_line"
189200
and chunk.get("content")
201+
and language != "shell"
190202
):
191203
print(chunk["content"], end="")
192204

205+
# After collecting all shell output, yield it as one chunk for truncation
206+
if language == "shell" and shell_output:
207+
yield {
208+
"type": "console",
209+
"format": "output",
210+
"content": shell_output
211+
}
212+
193213
except GeneratorExit:
194214
self.stop()
195215

0 commit comments

Comments
 (0)