Skip to content

Commit 4b0f5eb

Browse files
tui: wrapping bugfix (#4674)
this fixes an issue where text lines with long words would sometimes overflow. - the default penalties for the OptimalFit algorithm allow overflowing in some cases. this seems insane to me, and i considered just banning the OptimalFit algorithm by disabling the 'smawk' feature on textwrap, but decided to keep it and just bump the overflow penalty to ~infinity since optimal fit does sometimes produce nicer wrappings. it's not clear this is worth it, though, and maybe we should just dump the optimal fit algorithm completely. - user history messages weren't rendering with the same wrap algorithm as used in the composer, which sometimes resulted in wrapping messages differently in the history vs. in the composer.
1 parent 75176da commit 4b0f5eb

File tree

2 files changed

+8
-2
lines changed

2 files changed

+8
-2
lines changed

codex-rs/tui/src/history_cell.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,8 @@ impl HistoryCell for UserHistoryCell {
104104
.lines()
105105
.map(|l| Line::from(l).style(style))
106106
.collect::<Vec<_>>(),
107-
RtOptions::new(wrap_width as usize),
107+
// Wrap algorithm matches textarea.rs.
108+
RtOptions::new(wrap_width as usize).wrap_algorithm(textwrap::WrapAlgorithm::FirstFit),
108109
);
109110

110111
lines.push(Line::from("").style(style));

codex-rs/tui/src/wrapping.rs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ use ratatui::text::Line;
22
use ratatui::text::Span;
33
use std::ops::Range;
44
use textwrap::Options;
5+
use textwrap::wrap_algorithms::Penalties;
56

67
use crate::render::line_utils::push_owned_lines;
78

@@ -90,7 +91,11 @@ impl<'a> RtOptions<'a> {
9091
subsequent_indent: Line::default(),
9192
break_words: true,
9293
word_separator: textwrap::WordSeparator::new(),
93-
wrap_algorithm: textwrap::WrapAlgorithm::new(),
94+
wrap_algorithm: textwrap::WrapAlgorithm::OptimalFit(Penalties {
95+
// ~infinite overflow penalty, we never want to overflow a line.
96+
overflow_penalty: usize::MAX / 4,
97+
..Default::default()
98+
}),
9499
word_splitter: textwrap::WordSplitter::HyphenSplitter,
95100
}
96101
}

0 commit comments

Comments
 (0)