mirror of
https://github.com/openai/codex.git
synced 2026-11-27 23:31:09 +00:00
Upgrade Ratatui to 0.30.2 (#35959)
## What changed - Upgrade `ratatui`, `ratatui-macros`, `crossterm`, and `ansi-to-tui`, and adapt the TUI to their updated rendering, backend, color-conversion, and cell APIs. - Use Ratatui's cell-width and continuation-cell metadata when mapping OSC 8 hyperlinks so links remain aligned across wrapped wide glyphs and halfwidth characters with dakuten. - Preserve explicit hyperlinks in the sign-in success screen and empty MCP state without embedding escape sequences in the text passed to Ratatui. ## Testing - Add regression coverage for hyperlink placement across wide glyphs and halfwidth dakuten, hyperlink preservation in onboarding and MCP output, and display-width handling. GitOrigin-RevId: 8460873e5a266923acd0417bc46264845eab0134
This commit is contained in:
1 parent
e1895710ad
commit
00cb5c465b
43 files changed
+1097
-303
No files matched your search
@@ -6,6 +6,8 @@
|
||||
use std::ops::Range;
|
||||
|
||||
use ratatui::buffer::Buffer;
|
||||
use ratatui::buffer::CellDiffOption;
|
||||
use ratatui::buffer::CellWidth;
|
||||
use ratatui::layout::Rect;
|
||||
use ratatui::style::Color;
|
||||
use ratatui::style::Modifier;
|
||||
@@ -15,8 +17,6 @@ use ratatui::text::Text;
|
||||
use ratatui::widgets::Paragraph;
|
||||
use ratatui::widgets::Widget;
|
||||
use ratatui::widgets::Wrap;
|
||||
use unicode_width::UnicodeWidthChar;
|
||||
use unicode_width::UnicodeWidthStr;
|
||||
use url::Url;
|
||||
|
||||
use crate::render::line_utils::line_to_borrowed;
|
||||
@@ -85,12 +85,16 @@ impl HyperlinkLine {
|
||||
}
|
||||
|
||||
pub(crate) fn width(&self) -> usize {
|
||||
self.line.width()
|
||||
self.line
|
||||
.spans
|
||||
.iter()
|
||||
.map(|span| usize::from(span.content.as_ref().cell_width()))
|
||||
.sum()
|
||||
}
|
||||
|
||||
pub(crate) fn push_span(&mut self, span: Span<'static>, destination: Option<&str>) {
|
||||
let start = self.width();
|
||||
let end = start + span.content.width();
|
||||
let end = start + usize::from(span.content.as_ref().cell_width());
|
||||
self.line.push_span(span);
|
||||
if end > start
|
||||
&& let Some(destination) = destination.and_then(web_destination)
|
||||
@@ -153,7 +157,7 @@ pub(crate) fn prefix_hyperlink_lines(
|
||||
} else {
|
||||
subsequent_prefix.clone()
|
||||
};
|
||||
let shift = prefix.content.width();
|
||||
let shift = usize::from(prefix.content.as_ref().cell_width());
|
||||
let mut spans = Vec::with_capacity(line.line.spans.len() + 1);
|
||||
spans.push(prefix);
|
||||
spans.extend(line.line.spans);
|
||||
@@ -227,7 +231,10 @@ pub(crate) fn remap_wrapped_line(
|
||||
if index > 0 {
|
||||
let trimmed = source_text[source_byte..].trim_start_matches(char::is_whitespace);
|
||||
let skipped = source_text[source_byte..].len() - trimmed.len();
|
||||
source_column += source_text[source_byte..source_byte + skipped].width();
|
||||
source_column += source_text[source_byte..source_byte + skipped]
|
||||
.chars()
|
||||
.map(char_cell_width)
|
||||
.sum::<usize>();
|
||||
source_byte += skipped;
|
||||
}
|
||||
|
||||
@@ -237,9 +244,9 @@ pub(crate) fn remap_wrapped_line(
|
||||
continue;
|
||||
};
|
||||
let mapped = &rendered[rendered_start..];
|
||||
let mut output_column = rendered[..rendered_start].width();
|
||||
let mut output_column = usize::from(rendered[..rendered_start].cell_width());
|
||||
for ch in mapped.chars() {
|
||||
let width = ch.width().unwrap_or(/*default*/ 0);
|
||||
let width = char_cell_width(ch);
|
||||
while source
|
||||
.hyperlinks
|
||||
.get(link_index)
|
||||
@@ -312,8 +319,8 @@ pub(crate) fn web_links_in_text(text: &str) -> Vec<TerminalHyperlink> {
|
||||
let Some(destination) = web_destination(candidate) else {
|
||||
continue;
|
||||
};
|
||||
let start = text[..raw_start + trimmed_start].width();
|
||||
let end = start + candidate.width();
|
||||
let start = usize::from(text[..raw_start + trimmed_start].cell_width());
|
||||
let end = start + usize::from(candidate.cell_width());
|
||||
links.push(TerminalHyperlink::web(start..end, destination));
|
||||
}
|
||||
links
|
||||
@@ -427,7 +434,7 @@ pub(crate) fn decorate_spans(line: &HyperlinkLine) -> Vec<Span<'static>> {
|
||||
let mut active_destination: Option<String> = None;
|
||||
for span in &line.line.spans {
|
||||
for ch in span.content.chars() {
|
||||
let width = ch.width().unwrap_or(/*default*/ 0);
|
||||
let width = char_cell_width(ch);
|
||||
while line
|
||||
.hyperlinks
|
||||
.get(link_index)
|
||||
@@ -480,6 +487,14 @@ fn append_to_last_span(out: &mut [Span<'static>], content: &str) {
|
||||
}
|
||||
}
|
||||
|
||||
fn char_cell_width(ch: char) -> usize {
|
||||
if ch.is_control() {
|
||||
return 0;
|
||||
}
|
||||
let mut encoded = [0; 4];
|
||||
usize::from(ch.encode_utf8(&mut encoded).cell_width())
|
||||
}
|
||||
|
||||
pub(crate) fn mark_buffer_hyperlinks(
|
||||
buf: &mut Buffer,
|
||||
area: Rect,
|
||||
@@ -509,10 +524,19 @@ pub(crate) fn mark_buffer_hyperlinks(
|
||||
paragraph.render(layout_area, &mut layout);
|
||||
let rendered_lines = (0..layout_area.height)
|
||||
.map(|row| {
|
||||
let mut trailing_columns = 0usize;
|
||||
let text = (0..layout_area.width)
|
||||
.filter_map(|column| {
|
||||
if trailing_columns > 0 {
|
||||
trailing_columns -= 1;
|
||||
return None;
|
||||
}
|
||||
let cell = &layout[(column, row)];
|
||||
(!cell.skip).then(|| cell.symbol())
|
||||
if cell.diff_option == CellDiffOption::Skip {
|
||||
return None;
|
||||
}
|
||||
trailing_columns = usize::from(cell.cell_width()).saturating_sub(1);
|
||||
Some(cell.symbol())
|
||||
})
|
||||
.collect::<String>();
|
||||
Line::from(text.trim_end().to_string())
|
||||
@@ -520,7 +544,12 @@ pub(crate) fn mark_buffer_hyperlinks(
|
||||
.collect();
|
||||
for (row, rendered) in remap_wrapped_line(line, rendered_lines).iter().enumerate() {
|
||||
for link in &rendered.hyperlinks {
|
||||
let mut trailing_columns = 0usize;
|
||||
for column in link.columns.clone() {
|
||||
if trailing_columns > 0 {
|
||||
trailing_columns -= 1;
|
||||
continue;
|
||||
}
|
||||
let row = logical_row + row;
|
||||
if row < scroll_rows || row - scroll_rows >= usize::from(area.height) {
|
||||
continue;
|
||||
@@ -528,9 +557,10 @@ pub(crate) fn mark_buffer_hyperlinks(
|
||||
let x = area.x + column as u16;
|
||||
let y = area.y + (row - scroll_rows) as u16;
|
||||
let cell = &mut buf[(x, y)];
|
||||
if cell.skip || cell.symbol().trim().is_empty() {
|
||||
if cell.diff_option == CellDiffOption::Skip {
|
||||
continue;
|
||||
}
|
||||
trailing_columns = usize::from(cell.cell_width()).saturating_sub(1);
|
||||
let symbol = link.terminal_destination().map_or_else(
|
||||
|| cell.symbol().to_string(),
|
||||
|destination| {
|
||||
@@ -568,7 +598,10 @@ fn mark_matching_cells(
|
||||
}
|
||||
for position in area.positions() {
|
||||
let cell = &mut buf[position];
|
||||
if !cell.skip && !cell.symbol().trim().is_empty() && matches(cell) {
|
||||
if cell.diff_option != CellDiffOption::Skip
|
||||
&& !cell.symbol().trim().is_empty()
|
||||
&& matches(cell)
|
||||
{
|
||||
let symbol = osc8_hyperlink(destination, cell.symbol());
|
||||
cell.set_symbol(&symbol);
|
||||
}
|
||||
@@ -611,7 +644,7 @@ mod tests {
|
||||
assert_eq!(
|
||||
web_links_in_text(&format!("See ({destination}).")),
|
||||
vec![TerminalHyperlink::web(
|
||||
/*columns*/ 5..5 + destination.width(),
|
||||
/*columns*/ 5..5 + usize::from(destination.cell_width()),
|
||||
destination.to_string(),
|
||||
)]
|
||||
);
|
||||
@@ -623,7 +656,7 @@ mod tests {
|
||||
let line = HyperlinkLine {
|
||||
line: Line::from(destination),
|
||||
hyperlinks: vec![TerminalHyperlink::web(
|
||||
/*columns*/ 0..destination.width(),
|
||||
/*columns*/ 0..usize::from(destination.cell_width()),
|
||||
destination.to_string(),
|
||||
)],
|
||||
};
|
||||
@@ -662,13 +695,15 @@ mod tests {
|
||||
let text = "alpha 😀here middle there end";
|
||||
let first_start = text.find("here").expect("first link");
|
||||
let second_start = text.find("there").expect("second link");
|
||||
let first_column = usize::from(text[..first_start].cell_width());
|
||||
let second_column = usize::from(text[..second_start].cell_width());
|
||||
let mut source = HyperlinkLine::new(Line::from(text));
|
||||
source.hyperlinks.push(TerminalHyperlink::web(
|
||||
text[..first_start].width()..text[..first_start].width() + "here".width(),
|
||||
first_column..first_column + usize::from("here".cell_width()),
|
||||
"https://example.com/first".to_string(),
|
||||
));
|
||||
source.hyperlinks.push(TerminalHyperlink::web(
|
||||
text[..second_start].width()..text[..second_start].width() + "there".width(),
|
||||
second_column..second_column + usize::from("there".cell_width()),
|
||||
"https://example.com/second".to_string(),
|
||||
));
|
||||
|
||||
@@ -706,7 +741,7 @@ mod tests {
|
||||
let destination = "https://example.com/path";
|
||||
let mut line = HyperlinkLine::new(Line::from(format!("See {destination} now")));
|
||||
line.hyperlinks.push(TerminalHyperlink::web(
|
||||
/*columns*/ 4..4 + destination.width(),
|
||||
/*columns*/ 4..4 + usize::from(destination.cell_width()),
|
||||
destination.to_string(),
|
||||
));
|
||||
let area = Rect::new(
|
||||
@@ -731,6 +766,62 @@ mod tests {
|
||||
assert_eq!(linked_text, destination);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn buffer_hyperlinks_follow_wrapped_wide_glyphs() {
|
||||
let destination = "https://example.com/wide";
|
||||
let mut line = HyperlinkLine::new(Line::from("前文 "));
|
||||
line.push_span("漢字漢字".into(), Some(destination));
|
||||
line.push_span(" 後文".into(), /*destination*/ None);
|
||||
let area = Rect::new(
|
||||
/*x*/ 0, /*y*/ 0, /*width*/ 6, /*height*/ 4,
|
||||
);
|
||||
let mut buf = Buffer::empty(area);
|
||||
|
||||
Paragraph::new(Text::from(line.line.clone()))
|
||||
.wrap(Wrap { trim: false })
|
||||
.render(area, &mut buf);
|
||||
mark_buffer_hyperlinks(&mut buf, area, &[line], /*scroll_rows*/ 0);
|
||||
|
||||
let linked_text = area
|
||||
.positions()
|
||||
.filter_map(|position| {
|
||||
let symbol = buf[position].symbol();
|
||||
symbol
|
||||
.contains(&format!("\x1b]8;;{destination}\x07"))
|
||||
.then(|| strip_osc8(symbol))
|
||||
})
|
||||
.collect::<String>();
|
||||
assert_eq!(linked_text, "漢字漢字");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn buffer_hyperlinks_follow_wrapped_halfwidth_dakuten() {
|
||||
let destination = "https://example.com/dakuten";
|
||||
let mut line = HyperlinkLine::new(Line::from("ガ "));
|
||||
line.push_span("パlink".into(), Some(destination));
|
||||
line.push_span(" tail".into(), /*destination*/ None);
|
||||
let area = Rect::new(
|
||||
/*x*/ 0, /*y*/ 0, /*width*/ 5, /*height*/ 4,
|
||||
);
|
||||
let mut buf = Buffer::empty(area);
|
||||
|
||||
Paragraph::new(Text::from(line.line.clone()))
|
||||
.wrap(Wrap { trim: false })
|
||||
.render(area, &mut buf);
|
||||
mark_buffer_hyperlinks(&mut buf, area, &[line], /*scroll_rows*/ 0);
|
||||
|
||||
let linked_text = area
|
||||
.positions()
|
||||
.filter_map(|position| {
|
||||
let symbol = buf[position].symbol();
|
||||
symbol
|
||||
.contains(&format!("\x1b]8;;{destination}\x07"))
|
||||
.then(|| strip_osc8(symbol))
|
||||
})
|
||||
.collect::<String>();
|
||||
assert_eq!(linked_text, "パlink");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn trusted_file_destination_receives_osc8_without_enabling_plain_file_links() {
|
||||
let temp_dir = tempfile::tempdir().expect("temp directory");
|
||||
|
||||
Reference in new issue
Block a user