Skip to content

Commit 9d8d7d8

Browse files
authored
Middle-truncate tool output and show more lines (#2096)
Command output can contain important bits of information at the beginning or end. This shows a bit more output and truncates in the middle. This will work better paired with #2095 which will omit output for simple successful reads/searches/etc. <img width="1262" height="496" alt="CleanShot 2025-08-09 at 13 01 05" src="https://github.com/user-attachments/assets/9d989eb6-f81e-4118-9745-d20728eeef71" /> ------ https://chatgpt.com/codex/tasks/task_i_68978cd19f9c832cac4975e44dcd99a0
1 parent f146981 commit 9d8d7d8

File tree

1 file changed

+45
-33
lines changed

1 file changed

+45
-33
lines changed

codex-rs/tui/src/history_cell.rs

Lines changed: 45 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,7 @@ pub(crate) enum HistoryCell {
133133
PatchApplyResult { view: TextBlock },
134134
}
135135

136-
const TOOL_CALL_MAX_LINES: usize = 3;
136+
const TOOL_CALL_MAX_LINES: usize = 5;
137137

138138
fn title_case(s: &str) -> String {
139139
if s.is_empty() {
@@ -194,6 +194,48 @@ impl HistoryCell {
194194
.unwrap_or(0)
195195
}
196196

197+
fn output_lines(src: &str) -> Vec<Line<'static>> {
198+
let lines: Vec<&str> = src.lines().collect();
199+
let total = lines.len();
200+
let limit = TOOL_CALL_MAX_LINES;
201+
202+
let mut out = Vec::new();
203+
204+
let head_end = total.min(limit);
205+
for (i, raw) in lines[..head_end].iter().enumerate() {
206+
let mut line = ansi_escape_line(raw);
207+
let prefix = if i == 0 { " ⎿ " } else { " " };
208+
line.spans.insert(0, prefix.into());
209+
line.spans.iter_mut().for_each(|span| {
210+
span.style = span.style.add_modifier(Modifier::DIM);
211+
});
212+
out.push(line);
213+
}
214+
215+
// If we will ellipsize less than the limit, just show it.
216+
let show_ellipsis = total > 2 * limit;
217+
if show_ellipsis {
218+
let omitted = total - 2 * limit;
219+
out.push(Line::from(format!("… +{omitted} lines")));
220+
}
221+
222+
let tail_start = if show_ellipsis {
223+
total - limit
224+
} else {
225+
head_end
226+
};
227+
for raw in lines[tail_start..].iter() {
228+
let mut line = ansi_escape_line(raw);
229+
line.spans.insert(0, " ".into());
230+
line.spans.iter_mut().for_each(|span| {
231+
span.style = span.style.add_modifier(Modifier::DIM);
232+
});
233+
out.push(line);
234+
}
235+
236+
out
237+
}
238+
197239
pub(crate) fn new_session_info(
198240
config: &Config,
199241
event: SessionConfiguredEvent,
@@ -308,27 +350,7 @@ impl HistoryCell {
308350
}
309351

310352
let src = if exit_code == 0 { stdout } else { stderr };
311-
312-
let mut lines_iter = src.lines();
313-
for (idx, raw) in lines_iter.by_ref().take(TOOL_CALL_MAX_LINES).enumerate() {
314-
let mut line = ansi_escape_line(raw);
315-
let prefix = if idx == 0 { " ⎿ " } else { " " };
316-
line.spans.insert(0, prefix.into());
317-
line.spans.iter_mut().for_each(|span| {
318-
span.style = span.style.add_modifier(Modifier::DIM);
319-
});
320-
lines.push(line);
321-
}
322-
let remaining = lines_iter.count();
323-
if remaining > 0 {
324-
let mut more = Line::from(format!("... +{remaining} lines"));
325-
// Continuation/ellipsis is treated as a subsequent line for prefixing
326-
more.spans.insert(0, " ".into());
327-
more.spans.iter_mut().for_each(|span| {
328-
span.style = span.style.add_modifier(Modifier::DIM);
329-
});
330-
lines.push(more);
331-
}
353+
lines.extend(Self::output_lines(&src));
332354
lines.push(Line::from(""));
333355

334356
HistoryCell::CompletedExecCommand {
@@ -813,17 +835,7 @@ impl HistoryCell {
813835
lines.push(Line::from("✘ Failed to apply patch".magenta().bold()));
814836

815837
if !stderr.trim().is_empty() {
816-
let mut iter = stderr.lines();
817-
for (i, raw) in iter.by_ref().take(TOOL_CALL_MAX_LINES).enumerate() {
818-
let prefix = if i == 0 { " ⎿ " } else { " " };
819-
let s = format!("{prefix}{raw}");
820-
lines.push(ansi_escape_line(&s).dim());
821-
}
822-
let remaining = iter.count();
823-
if remaining > 0 {
824-
lines.push(Line::from(""));
825-
lines.push(Line::from(format!("... +{remaining} lines")).dim());
826-
}
838+
lines.extend(Self::output_lines(&stderr));
827839
}
828840

829841
lines.push(Line::from(""));

0 commit comments

Comments
 (0)