mirror of
https://github.com/openai/codex.git
synced 2026-11-16 22:00:36 +00:00
Render async questions and replies with Markdown in the TUI (#57292)
## Why Async question replies appeared as plain user text, leaving Markdown formatting literal and repeating the full question above each answer. ## What changed - Accent question titles with a `?` prefix while preserving Markdown in titles and suggestions. - Render replies with a `↪ Re:` question caption and a Markdown answer. Limit captions to two rows with an ellipsis on overflow, while keeping answers complete. - Apply the same rendering to live messages and loaded transcripts, preserving inline styles, hyperlinks, and Markdown when copying selections. ## Testing Add snapshots and assertions for matching live and loaded transcript rendering, plus tests for caption clipping, hyperlink bounds, Markdown copying, partial selections, and whitespace-only or nonprinting answers. GitOrigin-RevId: 0290d8a2ebfb42bd5d7374c7037cde5ae8a13b5d
This commit is contained in:
@@ -5,6 +5,7 @@ use crate::history_cell::HistoryCell;
|
||||
use pretty_assertions::assert_eq;
|
||||
use ratatui::buffer::Buffer;
|
||||
use ratatui::layout::Rect;
|
||||
use ratatui::style::Modifier;
|
||||
use std::path::Path;
|
||||
|
||||
#[path = "table_copy_tests.rs"]
|
||||
@@ -304,6 +305,101 @@ fn mixed_plain_output_remains_literal_in_html() {
|
||||
);
|
||||
}
|
||||
|
||||
fn reply_rows(question: &str, answer: &str, width: u16) -> Vec<HyperlinkLine> {
|
||||
let reply = crate::async_question_reply::AsyncQuestionReply {
|
||||
question_item_id: "ask".into(),
|
||||
question: question.into(),
|
||||
answer: answer.into(),
|
||||
};
|
||||
crate::history_cell::new_user_prompt(answer.into(), vec![], vec![], vec![])
|
||||
.with_async_replies(vec![reply], Path::new("/"))
|
||||
.display_hyperlink_lines(width)
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn reply_answer_distinguishes_whitespace_from_nonprinting_markdown() {
|
||||
let definition = "[docs]: https://example.test";
|
||||
for (answer, expected) in [
|
||||
(" \t\n ", vec!["", "↪ Re: Why?", ""]),
|
||||
(
|
||||
definition,
|
||||
vec!["", "↪ Re: Why?", "", " [docs]: https://example.test", ""],
|
||||
),
|
||||
] {
|
||||
let rows: Vec<_> = reply_rows("Why?", answer, /*width*/ 60)
|
||||
.into_iter()
|
||||
.map(|row| row.line.to_string())
|
||||
.collect();
|
||||
assert_eq!(rows, expected);
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn reply_caption_clips_on_overflow_and_copies_with_its_markdown_answer() {
|
||||
for (width, clipped) in [(17, false), (12, true)] {
|
||||
let rows = reply_rows(
|
||||
"https://example.test",
|
||||
"12345678901234567890\n\n**Tail**",
|
||||
width,
|
||||
);
|
||||
let last = &rows[2]; // The cell starts with one padding row and two caption rows.
|
||||
assert_eq!(last.line.to_string().ends_with('…'), clipped);
|
||||
assert_eq!(
|
||||
last.hyperlinks.last().unwrap().columns.end,
|
||||
last.width() - usize::from(clipped)
|
||||
);
|
||||
let tail = rows
|
||||
.iter()
|
||||
.flat_map(|row| &row.line.spans)
|
||||
.find(|span| span.content == "Tail")
|
||||
.unwrap();
|
||||
assert!(tail.style.add_modifier.contains(Modifier::BOLD));
|
||||
let layout = TextLayout::new(rows, width);
|
||||
let (markdown, _) = payload(&layout, 0..layout.text().len());
|
||||
assert_eq!(markdown.contains('…'), clipped);
|
||||
assert_eq!(markdown.matches("Re: ").count(), 1, "{markdown}");
|
||||
assert!(
|
||||
markdown.contains("12345678901234567890") && markdown.contains("**Tail**"),
|
||||
"{markdown}"
|
||||
);
|
||||
assert!(!markdown.contains("```"), "{markdown}");
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn full_reply_caption_preserves_links_and_partial_selections() {
|
||||
let width = 44;
|
||||
for prefix in ["", "> ", "- > "] {
|
||||
let question = format!("{prefix}Read the [setup guide](https://example.test/setup)?");
|
||||
let rows = reply_rows(&question, "Yes", width);
|
||||
let first = rows[1].line.to_string();
|
||||
let layout = TextLayout::new(rows, width);
|
||||
assert!(
|
||||
layout
|
||||
.text()
|
||||
.trim_start()
|
||||
.starts_with(first.trim_start_matches("↪ ").trim_end()),
|
||||
"{}",
|
||||
layout.text()
|
||||
);
|
||||
let (copy, _) = payload(&layout, 0..layout.text().len());
|
||||
assert!(
|
||||
copy.trim_start()
|
||||
.starts_with(&format!("Re: {prefix}Read the [setup guide")),
|
||||
"{copy}"
|
||||
);
|
||||
assert!(copy.contains("](<https://example.test/setup>)?"), "{copy}");
|
||||
let start = layout.text().find("guide").unwrap();
|
||||
let question_layout = markdown_layout(&question, width);
|
||||
let original = question_layout.text().find("guide").unwrap();
|
||||
assert_eq!(
|
||||
payload(&layout, start..start + 5),
|
||||
payload(&question_layout, original..original + 5),
|
||||
"{prefix}"
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn mixed_user_prose_preserves_literal_text_without_code_fences() {
|
||||
let cell = crate::history_cell::new_user_prompt(
|
||||
|
||||
Reference in New Issue
Block a user