prefer ratatui Stylized for constructing lines/spans (#3068)

no functional change, just simplifying ratatui styling and adding
guidance in AGENTS.md for future.
This commit is contained in:
Jeremy Rose
2025-09-02 16:19:54 -07:00
committed by GitHub
parent 0d5ffb000e
commit 578ff09e17
18 changed files with 203 additions and 311 deletions

View File

@@ -3,10 +3,8 @@
use ratatui::backend::TestBackend;
use ratatui::layout::Rect;
use ratatui::style::Color;
use ratatui::style::Style;
use ratatui::style::Stylize;
use ratatui::text::Line;
use ratatui::text::Span;
// Small helper macro to assert a collection contains an item with a clearer
// failure message.
@@ -80,7 +78,7 @@ fn basic_insertion_no_wrap() {
let area = Rect::new(0, 5, 20, 1);
let mut scenario = TestScenario::new(20, 6, area);
let lines = vec![Line::from("first"), Line::from("second")];
let lines = vec!["first".into(), "second".into()];
let buf = scenario.run_insert(lines);
let rows = scenario.screen_rows_from_bytes(&buf);
assert_contains!(rows, String::from("first"));
@@ -102,7 +100,7 @@ fn long_token_wraps() {
let mut scenario = TestScenario::new(20, 6, area);
let long = "A".repeat(45); // > 2 lines at width 20
let lines = vec![Line::from(long.clone())];
let lines = vec![long.clone().into()];
let buf = scenario.run_insert(lines);
let mut parser = vt100::Parser::new(6, 20, 0);
parser.process(&buf);
@@ -134,7 +132,7 @@ fn emoji_and_cjk() {
let mut scenario = TestScenario::new(20, 6, area);
let text = String::from("😀😀😀😀😀 你好世界");
let lines = vec![Line::from(text.clone())];
let lines = vec![text.clone().into()];
let buf = scenario.run_insert(lines);
let rows = scenario.screen_rows_from_bytes(&buf);
let reconstructed: String = rows.join("").chars().filter(|c| *c != ' ').collect();
@@ -151,10 +149,7 @@ fn mixed_ansi_spans() {
let area = Rect::new(0, 5, 20, 1);
let mut scenario = TestScenario::new(20, 6, area);
let line = Line::from(vec![
Span::styled("red", Style::default().fg(Color::Red)),
Span::raw("+plain"),
]);
let line = vec!["red".red(), "+plain".into()].into();
let buf = scenario.run_insert(vec![line]);
let rows = scenario.screen_rows_from_bytes(&buf);
assert_contains!(rows, String::from("red+plain"));
@@ -165,7 +160,7 @@ fn cursor_restoration() {
let area = Rect::new(0, 5, 20, 1);
let mut scenario = TestScenario::new(20, 6, area);
let lines = vec![Line::from("x")];
let lines = vec!["x".into()];
let buf = scenario.run_insert(lines);
let s = String::from_utf8_lossy(&buf);
// CUP to 1;1 (ANSI: ESC[1;1H)
@@ -187,7 +182,7 @@ fn word_wrap_no_mid_word_split() {
let mut scenario = TestScenario::new(40, 10, area);
let sample = "Years passed, and Willowmere thrived in peace and friendship. Miras herb garden flourished with both ordinary and enchanted plants, and travelers spoke of the kindness of the woman who tended them.";
let buf = scenario.run_insert(vec![Line::from(sample)]);
let buf = scenario.run_insert(vec![sample.into()]);
let rows = scenario.screen_rows_from_bytes(&buf);
let joined = rows.join("\n");
assert!(
@@ -203,7 +198,7 @@ fn em_dash_and_space_word_wrap() {
let mut scenario = TestScenario::new(40, 10, area);
let sample = "Mara found an old key on the shore. Curious, she opened a tarnished box half-buried in sand—and inside lay a single, glowing seed.";
let buf = scenario.run_insert(vec![Line::from(sample)]);
let buf = scenario.run_insert(vec![sample.into()]);
let rows = scenario.screen_rows_from_bytes(&buf);
let joined = rows.join("\n");
assert!(
@@ -218,7 +213,7 @@ fn pre_scroll_region_down() {
let area = Rect::new(0, 3, 20, 1);
let mut scenario = TestScenario::new(20, 6, area);
let lines = vec![Line::from("first"), Line::from("second")];
let lines = vec!["first".into(), "second".into()];
let buf = scenario.run_insert(lines);
let s = String::from_utf8_lossy(&buf);
// Expect we limited scroll region to [top+1 .. screen_height] => [4 .. 6] (1-based)

View File

@@ -24,10 +24,7 @@ fn live_001_commit_on_overflow() {
// Keep the last 3 in the live ring; commit the first 2.
let commit_rows = rb.drain_commit_ready(3);
let lines: Vec<Line<'static>> = commit_rows
.into_iter()
.map(|r| Line::from(r.text))
.collect();
let lines: Vec<Line<'static>> = commit_rows.into_iter().map(|r| r.text.into()).collect();
let mut buf: Vec<u8> = Vec::new();
codex_tui::insert_history::insert_history_lines_to_writer(&mut term, &mut buf, lines);
@@ -80,10 +77,7 @@ fn live_002_pre_scroll_and_commit() {
// Keep 3, commit 1.
let commit_rows = rb.drain_commit_ready(3);
let lines: Vec<Line<'static>> = commit_rows
.into_iter()
.map(|r| Line::from(r.text))
.collect();
let lines: Vec<Line<'static>> = commit_rows.into_iter().map(|r| r.text.into()).collect();
let mut buf: Vec<u8> = Vec::new();
codex_tui::insert_history::insert_history_lines_to_writer(&mut term, &mut buf, lines);

View File

@@ -2,7 +2,6 @@
use ratatui::backend::TestBackend;
use ratatui::layout::Rect;
use ratatui::text::Line;
fn term(viewport: Rect) -> codex_tui::custom_terminal::Terminal<TestBackend> {
let backend = TestBackend::new(20, 6);
@@ -23,7 +22,7 @@ fn stream_commit_trickle_no_duplication() {
codex_tui::insert_history::insert_history_lines_to_writer(
&mut t,
&mut out1,
vec![Line::from("one")],
vec!["one".into()],
);
// Step 2: later commit next row
@@ -31,7 +30,7 @@ fn stream_commit_trickle_no_duplication() {
codex_tui::insert_history::insert_history_lines_to_writer(
&mut t,
&mut out2,
vec![Line::from("two")],
vec!["two".into()],
);
let combined = [out1, out2].concat();
@@ -62,7 +61,7 @@ fn live_ring_rows_not_inserted_into_history() {
codex_tui::insert_history::insert_history_lines_to_writer(
&mut t,
&mut buf,
vec![Line::from("one"), Line::from("two")],
vec!["one".into(), "two".into()],
);
// The live ring might display tail+head rows like ["two", "three"],