mirror of
https://github.com/openai/codex.git
synced 2026-11-15 21:50:46 +00:00
> large behavior change to how the TUI owns its viewport, history, and suspend behavior. > Core model is in place; a few items are still being polished before this is ready to merge. We've moved this over to a new tui2 crate from being directly on the tui crate. To enable use --enable tui2 (or the equivalent in your config.toml). See https://developers.openai.com/codex/local-config#feature-flags Note that this serves as a baseline for the changes that we're making to be applied rapidly. Tui2 may not track later changes in the main tui. It's experimental and may not be where we land on things. --- ## Summary This PR moves the Codex TUI off of “cooperating” with the terminal’s scrollback and onto a model where the in‑memory transcript is the single source of truth. The TUI now owns scrolling, selection, copy, and suspend/exit printing based on that transcript, and only writes to terminal scrollback in append‑only fashion on suspend/exit. It also fixes streaming wrapping so streamed responses reflow with the viewport, and introduces configuration to control whether we print history on suspend or only on exit. High‑level goals: - Ensure history is complete, ordered, and never silently dropped. - Print each logical history cell at most once into scrollback, even with resizes and suspends. - Make scrolling, selection, and copy match the visible transcript, not the terminal’s notion of scrollback. - Keep suspend/alt‑screen behavior predictable across terminals. --- ## Core Design Changes ### Transcript & viewport ownership - Treat the transcript as a list of **cells** (user prompts, agent messages, system/info rows, streaming segments). - On each frame: - Compute a **transcript region** as “full terminal frame minus the bottom input area”. - Flatten all cells into visual lines plus metadata (which cell + which line within that cell). - Use scroll state to choose which visual line is at the top of the region. - Clear that region and draw just the visible slice of lines. - The terminal’s scrollback is no longer part of the live layout algorithm; it is only ever written to when we decide to print history. ### User message styling - User prompts now render as clear blocks with: - A blank padding line above and below. - A full‑width background for every line in the block (including the prompt line itself). - The same block styling is used when we print history into scrollback, so the transcript looks consistent whether you are in the TUI or scrolling back after exit/suspend. --- ## Scrolling, Mouse, Selection, and Copy ### Scrolling - Scrolling is defined in terms of the flattened transcript lines: - Mouse wheel scrolls up/down by fixed line increments. - PgUp/PgDn/Home/End operate on the same scroll model. - The footer shows: - Whether you are “following live output” vs “scrolled up”. - Current scroll position (line / total). - When there is no history yet, the bottom pane is **pegged high** and gradually moves down as the transcript fills, matching the existing UX. ### Selection - Click‑and‑drag defines a **linear selection** over transcript line/column coordinates, not raw screen rows. - Selection is **content‑anchored**: - When you scroll, the selection moves with the underlying lines instead of sticking to a fixed Y position. - This holds both when scrolling manually and when new content streams in, as long as you are in “follow” mode. - The selection only covers the “transcript text” area: - Left gutter/prefix (bullets, markers) is intentionally excluded. - This keeps copy/paste cleaner and avoids including structural margin characters. ### Copy (`Ctrl+Y`) - Introduce a small clipboard abstraction (`ClipboardManager`‑style) and use a cross‑platform clipboard crate under the hood. - When `Ctrl+Y` is pressed and a non‑empty selection exists: - Re‑render the transcript region off‑screen using the same wrapping as the visible viewport. - Walk the selected line/column range over that buffer to reconstruct the exact text: - Includes spaces between words. - Preserves empty lines within the selection. - Send the resulting text to the system clipboard. - Show a short status message in the footer indicating success/failure. - Copy is **best‑effort**: - Clipboard failures (headless environment, sandbox, remote sessions) are handled gracefully via status messages; they do not crash the TUI. - Copy does *not* insert a new history entry; it only affects the status bar. --- ## Streaming and Wrapping ### Previous behavior Previously, streamed markdown: - Was wrapped at a fixed width **at commit time** inside the streaming collector. - Those wrapped `Line<'static>` values were then wrapped again at display time. - As a result, streamed paragraphs could not “un‑wrap” when the terminal width increased; they were permanently split according to the width at the start of the stream. ### New behavior This PR implements the first step from `codex-rs/tui/streaming_wrapping_design.md`: - Streaming collector is constructed **without** a fixed width for wrapping. - It still: - Buffers the full markdown source for the current stream. - Commits only at newline boundaries. - Emits logical lines as new content becomes available. - Agent message cells now wrap streamed content only at **display time**, based on the current viewport width, just like non‑streaming messages. - Consequences: - Streamed responses reflow correctly when the terminal is resized. - Animation steps are per logical line instead of per “pre‑wrapped” visual line; this makes some commits slightly larger but keeps the behavior simple and predictable. Streaming responses are still represented as a sequence of logical history entries (first line + continuations) and integrate with the same scrolling, selection, and printing model. --- ## Printing History on Suspend and Exit ### High‑water mark and append‑only scrollback - Introduce a **cell‑based high‑water mark** (`printed_history_cells`) on the transcript: - Represents “how many cells at the front of the transcript have already been printed”. - Completely independent of wrapped line counts or terminal geometry. - Whenever we print history (suspend or exit): - Take the suffix of `transcript_cells` beyond `printed_history_cells`. - Render just that suffix into styled lines at the **current** width. - Write those lines to stdout. - Advance `printed_history_cells` to cover all cells we just printed. - Older cells are never re‑rendered for scrollback. They stay in whatever wrapping they had when printed, which is acceptable as long as the logical content is present once. ### Suspend (`Ctrl+Z`) - On suspend: - Leave alt screen if active and restore normal terminal modes. - Render the not‑yet‑printed suffix of the transcript and append it to normal scrollback. - Advance the high‑water mark. - Suspend the process. - On resume (`fg`): - Re‑enter the TUI mode (alt screen + input modes). - Clear the viewport region and fully redraw from in‑memory transcript and state. This gives predictable behavior across terminals without trying to maintain scrollback live. ### Exit - On exit: - Render any remaining unprinted cells once and write them to stdout. - Add an extra blank line after the final Codex history cell before printing token usage, so the transcript and usage info are visually separated. - If you never suspended, exit prints the entire transcript exactly once. - If you suspended one or more times, exit prints only the cells appended after the last suspend. --- ## Configuration: Suspend Printing This PR also adds configuration to control **when** we print history: - New TUI config option to gate printing on suspend: - At minimum: - `print_on_suspend = true` – current behavior: print new history at each suspend *and* on exit. - `print_on_suspend = false` – only print on exit. - Default is tuned to preserve current behavior, but this can be revisited based on feedback. - The config is respected in the suspend path: - If disabled, suspend only restores terminal modes and stops rendering but does not print new history. - Exit still prints the full not‑yet‑printed suffix once. This keeps the core viewport logic agnostic to preference, while letting users who care about quiet scrollback opt out of suspend printing. --- ## Tradeoffs What we gain: - A single authoritative history model (the in‑memory transcript). - Deterministic viewport rendering independent of terminal quirks. - Suspend/exit flows that: - Print each logical history cell exactly once. - Work across resizes and different terminals. - Interact cleanly with alt screen and raw‑mode toggling. - Consistent, content‑anchored scrolling, selection, and copy. - Streaming messages that reflow correctly with the viewport width. What we accept: - Scrollback may contain older cells wrapped differently than newer ones. - Streaming responses appear in scrollback as a sequence of blocks corresponding to their streaming structure, not as a single retroactively reflowed paragraph. - We do not attempt to rewrite or reflow already‑printed scrollback. For deeper rationale and diagrams, see `docs/tui_viewport_and_history.md` and `codex-rs/tui/streaming_wrapping_design.md`. --- ## Still to Do Before This PR Is Ready These are scoped to this PR (not long‑term future work): - [ ] **Streaming wrapping polish** - Double‑check all streaming paths use display‑time wrapping only. - Ensure tests cover resizing after streaming has started. - [ ] **Suspend printing config** - Finalize config shape and default (keep existing behavior vs opt‑out). - Wire config through TUI startup and document it in the appropriate config docs. - [x] **Bottom pane positioning** - Ensure the bottom pane is pegged high when there’s no history and smoothly moves down as the transcript fills, matching the current behavior across startup and resume. - [x] **Transcript mouse scrolling** - Re‑enable wheel‑based transcript scrolling on top of the new scroll model. - Make sure mouse scroll does not get confused with “alternate scroll” modes from terminals. - [x] **Mouse selection vs streaming** - When selection is active, stop auto‑scrolling on streaming so the selection remains stable on the selected content. - Ensure that when streaming continues after selection is cleared, “follow latest output” mode resumes correctly. - [ ] **Auto‑scroll during drag** - While the user is dragging a selection, auto‑scroll when the cursor is at/near the top or bottom of the transcript viewport to allow selecting beyond the current visible window. - [ ] **Feature flag / rollout** - Investigate gating the new viewport/history behavior behind a feature flag for initial rollout, so we can fall back to the old behavior if needed during early testing. - [ ] **Before/after videos** - Capture short clips showing: - Scrolling (mouse + keys). - Selection and copy. - Streaming behavior under resize. - Suspend/resume and exit printing. - Use these to validate UX and share context in the PR discussion.
1060 lines
36 KiB
Rust
1060 lines
36 KiB
Rust
use std::io::Result;
|
|
use std::sync::Arc;
|
|
use std::time::Duration;
|
|
|
|
use crate::history_cell::HistoryCell;
|
|
use crate::history_cell::UserHistoryCell;
|
|
use crate::key_hint;
|
|
use crate::key_hint::KeyBinding;
|
|
use crate::render::Insets;
|
|
use crate::render::renderable::InsetRenderable;
|
|
use crate::render::renderable::Renderable;
|
|
use crate::style::user_message_style;
|
|
use crate::tui;
|
|
use crate::tui::TuiEvent;
|
|
use crossterm::event::KeyCode;
|
|
use crossterm::event::KeyEvent;
|
|
use crossterm::event::MouseEvent;
|
|
use crossterm::event::MouseEventKind;
|
|
use ratatui::buffer::Buffer;
|
|
use ratatui::buffer::Cell;
|
|
use ratatui::layout::Rect;
|
|
use ratatui::style::Style;
|
|
use ratatui::style::Stylize;
|
|
use ratatui::text::Line;
|
|
use ratatui::text::Span;
|
|
use ratatui::text::Text;
|
|
use ratatui::widgets::Clear;
|
|
use ratatui::widgets::Paragraph;
|
|
use ratatui::widgets::Widget;
|
|
use ratatui::widgets::WidgetRef;
|
|
use ratatui::widgets::Wrap;
|
|
|
|
pub(crate) enum Overlay {
|
|
Transcript(TranscriptOverlay),
|
|
Static(StaticOverlay),
|
|
}
|
|
|
|
impl Overlay {
|
|
pub(crate) fn new_transcript(cells: Vec<Arc<dyn HistoryCell>>) -> Self {
|
|
Self::Transcript(TranscriptOverlay::new(cells))
|
|
}
|
|
|
|
pub(crate) fn new_static_with_lines(lines: Vec<Line<'static>>, title: String) -> Self {
|
|
Self::Static(StaticOverlay::with_title(lines, title))
|
|
}
|
|
|
|
pub(crate) fn new_static_with_renderables(
|
|
renderables: Vec<Box<dyn Renderable>>,
|
|
title: String,
|
|
) -> Self {
|
|
Self::Static(StaticOverlay::with_renderables(renderables, title))
|
|
}
|
|
|
|
pub(crate) fn handle_event(&mut self, tui: &mut tui::Tui, event: TuiEvent) -> Result<()> {
|
|
match self {
|
|
Overlay::Transcript(o) => o.handle_event(tui, event),
|
|
Overlay::Static(o) => o.handle_event(tui, event),
|
|
}
|
|
}
|
|
|
|
pub(crate) fn is_done(&self) -> bool {
|
|
match self {
|
|
Overlay::Transcript(o) => o.is_done(),
|
|
Overlay::Static(o) => o.is_done(),
|
|
}
|
|
}
|
|
}
|
|
|
|
const KEY_UP: KeyBinding = key_hint::plain(KeyCode::Up);
|
|
const KEY_DOWN: KeyBinding = key_hint::plain(KeyCode::Down);
|
|
const KEY_K: KeyBinding = key_hint::plain(KeyCode::Char('k'));
|
|
const KEY_J: KeyBinding = key_hint::plain(KeyCode::Char('j'));
|
|
const KEY_PAGE_UP: KeyBinding = key_hint::plain(KeyCode::PageUp);
|
|
const KEY_PAGE_DOWN: KeyBinding = key_hint::plain(KeyCode::PageDown);
|
|
const KEY_SPACE: KeyBinding = key_hint::plain(KeyCode::Char(' '));
|
|
const KEY_SHIFT_SPACE: KeyBinding = key_hint::shift(KeyCode::Char(' '));
|
|
const KEY_HOME: KeyBinding = key_hint::plain(KeyCode::Home);
|
|
const KEY_END: KeyBinding = key_hint::plain(KeyCode::End);
|
|
const KEY_CTRL_F: KeyBinding = key_hint::ctrl(KeyCode::Char('f'));
|
|
const KEY_CTRL_D: KeyBinding = key_hint::ctrl(KeyCode::Char('d'));
|
|
const KEY_CTRL_B: KeyBinding = key_hint::ctrl(KeyCode::Char('b'));
|
|
const KEY_CTRL_U: KeyBinding = key_hint::ctrl(KeyCode::Char('u'));
|
|
const KEY_Q: KeyBinding = key_hint::plain(KeyCode::Char('q'));
|
|
const KEY_ESC: KeyBinding = key_hint::plain(KeyCode::Esc);
|
|
const KEY_ENTER: KeyBinding = key_hint::plain(KeyCode::Enter);
|
|
const KEY_CTRL_T: KeyBinding = key_hint::ctrl(KeyCode::Char('t'));
|
|
const KEY_CTRL_C: KeyBinding = key_hint::ctrl(KeyCode::Char('c'));
|
|
|
|
// Common pager navigation hints rendered on the first line
|
|
const PAGER_KEY_HINTS: &[(&[KeyBinding], &str)] = &[
|
|
(&[KEY_UP, KEY_DOWN], "to scroll"),
|
|
(&[KEY_PAGE_UP, KEY_PAGE_DOWN], "to page"),
|
|
(&[KEY_HOME, KEY_END], "to jump"),
|
|
];
|
|
|
|
// Render a single line of key hints from (key(s), description) pairs.
|
|
fn render_key_hints(area: Rect, buf: &mut Buffer, pairs: &[(&[KeyBinding], &str)]) {
|
|
let mut spans: Vec<Span<'static>> = vec![" ".into()];
|
|
let mut first = true;
|
|
for (keys, desc) in pairs {
|
|
if !first {
|
|
spans.push(" ".into());
|
|
}
|
|
for (i, key) in keys.iter().enumerate() {
|
|
if i > 0 {
|
|
spans.push("/".into());
|
|
}
|
|
spans.push(Span::from(key));
|
|
}
|
|
spans.push(" ".into());
|
|
spans.push(Span::from(desc.to_string()));
|
|
first = false;
|
|
}
|
|
Paragraph::new(vec![Line::from(spans).dim()]).render_ref(area, buf);
|
|
}
|
|
|
|
/// Generic widget for rendering a pager view.
|
|
struct PagerView {
|
|
renderables: Vec<Box<dyn Renderable>>,
|
|
scroll_offset: usize,
|
|
title: String,
|
|
last_content_height: Option<usize>,
|
|
last_rendered_height: Option<usize>,
|
|
/// If set, on next render ensure this chunk is visible.
|
|
pending_scroll_chunk: Option<usize>,
|
|
}
|
|
|
|
impl PagerView {
|
|
fn new(renderables: Vec<Box<dyn Renderable>>, title: String, scroll_offset: usize) -> Self {
|
|
Self {
|
|
renderables,
|
|
scroll_offset,
|
|
title,
|
|
last_content_height: None,
|
|
last_rendered_height: None,
|
|
pending_scroll_chunk: None,
|
|
}
|
|
}
|
|
|
|
fn content_height(&self, width: u16) -> usize {
|
|
self.renderables
|
|
.iter()
|
|
.map(|c| c.desired_height(width) as usize)
|
|
.sum()
|
|
}
|
|
|
|
fn render(&mut self, area: Rect, buf: &mut Buffer) {
|
|
Clear.render(area, buf);
|
|
self.render_header(area, buf);
|
|
let content_area = self.content_area(area);
|
|
self.update_last_content_height(content_area.height);
|
|
let content_height = self.content_height(content_area.width);
|
|
self.last_rendered_height = Some(content_height);
|
|
// If there is a pending request to scroll a specific chunk into view,
|
|
// satisfy it now that wrapping is up to date for this width.
|
|
if let Some(idx) = self.pending_scroll_chunk.take() {
|
|
self.ensure_chunk_visible(idx, content_area);
|
|
}
|
|
self.scroll_offset = self
|
|
.scroll_offset
|
|
.min(content_height.saturating_sub(content_area.height as usize));
|
|
|
|
self.render_content(content_area, buf);
|
|
|
|
self.render_bottom_bar(area, content_area, buf, content_height);
|
|
}
|
|
|
|
fn render_header(&self, area: Rect, buf: &mut Buffer) {
|
|
Span::from("/ ".repeat(area.width as usize / 2))
|
|
.dim()
|
|
.render_ref(area, buf);
|
|
let header = format!("/ {}", self.title);
|
|
header.dim().render_ref(area, buf);
|
|
}
|
|
|
|
fn render_content(&self, area: Rect, buf: &mut Buffer) {
|
|
let mut y = -(self.scroll_offset as isize);
|
|
let mut drawn_bottom = area.y;
|
|
for renderable in &self.renderables {
|
|
let top = y;
|
|
let height = renderable.desired_height(area.width) as isize;
|
|
y += height;
|
|
let bottom = y;
|
|
if bottom < area.y as isize {
|
|
continue;
|
|
}
|
|
if top > area.y as isize + area.height as isize {
|
|
break;
|
|
}
|
|
if top < 0 {
|
|
let drawn = render_offset_content(area, buf, &**renderable, (-top) as u16);
|
|
drawn_bottom = drawn_bottom.max(area.y + drawn);
|
|
} else {
|
|
let draw_height = (height as u16).min(area.height.saturating_sub(top as u16));
|
|
let draw_area = Rect::new(area.x, area.y + top as u16, area.width, draw_height);
|
|
renderable.render(draw_area, buf);
|
|
drawn_bottom = drawn_bottom.max(draw_area.y.saturating_add(draw_area.height));
|
|
}
|
|
}
|
|
|
|
for y in drawn_bottom..area.bottom() {
|
|
if area.width == 0 {
|
|
break;
|
|
}
|
|
buf[(area.x, y)] = Cell::from('~');
|
|
for x in area.x + 1..area.right() {
|
|
buf[(x, y)] = Cell::from(' ');
|
|
}
|
|
}
|
|
}
|
|
|
|
fn render_bottom_bar(
|
|
&self,
|
|
full_area: Rect,
|
|
content_area: Rect,
|
|
buf: &mut Buffer,
|
|
total_len: usize,
|
|
) {
|
|
let sep_y = content_area.bottom();
|
|
let sep_rect = Rect::new(full_area.x, sep_y, full_area.width, 1);
|
|
|
|
Span::from("─".repeat(sep_rect.width as usize))
|
|
.dim()
|
|
.render_ref(sep_rect, buf);
|
|
let percent = if total_len == 0 {
|
|
100
|
|
} else {
|
|
let max_scroll = total_len.saturating_sub(content_area.height as usize);
|
|
if max_scroll == 0 {
|
|
100
|
|
} else {
|
|
(((self.scroll_offset.min(max_scroll)) as f32 / max_scroll as f32) * 100.0).round()
|
|
as u8
|
|
}
|
|
};
|
|
let pct_text = format!(" {percent}% ");
|
|
let pct_w = pct_text.chars().count() as u16;
|
|
let pct_x = sep_rect.x + sep_rect.width - pct_w - 1;
|
|
Span::from(pct_text)
|
|
.dim()
|
|
.render_ref(Rect::new(pct_x, sep_rect.y, pct_w, 1), buf);
|
|
}
|
|
|
|
fn handle_key_event(&mut self, tui: &mut tui::Tui, key_event: KeyEvent) -> Result<()> {
|
|
match key_event {
|
|
e if KEY_UP.is_press(e) || KEY_K.is_press(e) => {
|
|
self.scroll_offset = self.scroll_offset.saturating_sub(1);
|
|
}
|
|
e if KEY_DOWN.is_press(e) || KEY_J.is_press(e) => {
|
|
self.scroll_offset = self.scroll_offset.saturating_add(1);
|
|
}
|
|
e if KEY_PAGE_UP.is_press(e)
|
|
|| KEY_SHIFT_SPACE.is_press(e)
|
|
|| KEY_CTRL_B.is_press(e) =>
|
|
{
|
|
let page_height = self.page_height(tui.terminal.viewport_area);
|
|
self.scroll_offset = self.scroll_offset.saturating_sub(page_height);
|
|
}
|
|
e if KEY_PAGE_DOWN.is_press(e) || KEY_SPACE.is_press(e) || KEY_CTRL_F.is_press(e) => {
|
|
let page_height = self.page_height(tui.terminal.viewport_area);
|
|
self.scroll_offset = self.scroll_offset.saturating_add(page_height);
|
|
}
|
|
e if KEY_CTRL_D.is_press(e) => {
|
|
let area = self.content_area(tui.terminal.viewport_area);
|
|
let half_page = (area.height as usize).saturating_add(1) / 2;
|
|
self.scroll_offset = self.scroll_offset.saturating_add(half_page);
|
|
}
|
|
e if KEY_CTRL_U.is_press(e) => {
|
|
let area = self.content_area(tui.terminal.viewport_area);
|
|
let half_page = (area.height as usize).saturating_add(1) / 2;
|
|
self.scroll_offset = self.scroll_offset.saturating_sub(half_page);
|
|
}
|
|
e if KEY_HOME.is_press(e) => {
|
|
self.scroll_offset = 0;
|
|
}
|
|
e if KEY_END.is_press(e) => {
|
|
self.scroll_offset = usize::MAX;
|
|
}
|
|
_ => {
|
|
return Ok(());
|
|
}
|
|
}
|
|
tui.frame_requester()
|
|
.schedule_frame_in(Duration::from_millis(16));
|
|
Ok(())
|
|
}
|
|
|
|
fn handle_mouse_scroll(&mut self, tui: &mut tui::Tui, event: MouseEvent) -> Result<()> {
|
|
let step: usize = 3;
|
|
match event.kind {
|
|
MouseEventKind::ScrollUp => {
|
|
self.scroll_offset = self.scroll_offset.saturating_sub(step);
|
|
}
|
|
MouseEventKind::ScrollDown => {
|
|
self.scroll_offset = self.scroll_offset.saturating_add(step);
|
|
}
|
|
_ => {
|
|
return Ok(());
|
|
}
|
|
}
|
|
tui.frame_requester()
|
|
.schedule_frame_in(Duration::from_millis(16));
|
|
Ok(())
|
|
}
|
|
|
|
/// Returns the height of one page in content rows.
|
|
///
|
|
/// Prefers the last rendered content height (excluding header/footer chrome);
|
|
/// if no render has occurred yet, falls back to the content area height
|
|
/// computed from the given viewport.
|
|
fn page_height(&self, viewport_area: Rect) -> usize {
|
|
self.last_content_height
|
|
.unwrap_or_else(|| self.content_area(viewport_area).height as usize)
|
|
}
|
|
|
|
fn update_last_content_height(&mut self, height: u16) {
|
|
self.last_content_height = Some(height as usize);
|
|
}
|
|
|
|
fn content_area(&self, area: Rect) -> Rect {
|
|
let mut area = area;
|
|
area.y = area.y.saturating_add(1);
|
|
area.height = area.height.saturating_sub(2);
|
|
area
|
|
}
|
|
}
|
|
|
|
impl PagerView {
|
|
fn is_scrolled_to_bottom(&self) -> bool {
|
|
if self.scroll_offset == usize::MAX {
|
|
return true;
|
|
}
|
|
let Some(height) = self.last_content_height else {
|
|
return false;
|
|
};
|
|
if self.renderables.is_empty() {
|
|
return true;
|
|
}
|
|
let Some(total_height) = self.last_rendered_height else {
|
|
return false;
|
|
};
|
|
if total_height <= height {
|
|
return true;
|
|
}
|
|
let max_scroll = total_height.saturating_sub(height);
|
|
self.scroll_offset >= max_scroll
|
|
}
|
|
|
|
/// Request that the given text chunk index be scrolled into view on next render.
|
|
fn scroll_chunk_into_view(&mut self, chunk_index: usize) {
|
|
self.pending_scroll_chunk = Some(chunk_index);
|
|
}
|
|
|
|
fn ensure_chunk_visible(&mut self, idx: usize, area: Rect) {
|
|
if area.height == 0 || idx >= self.renderables.len() {
|
|
return;
|
|
}
|
|
let first = self
|
|
.renderables
|
|
.iter()
|
|
.take(idx)
|
|
.map(|r| r.desired_height(area.width) as usize)
|
|
.sum();
|
|
let last = first + self.renderables[idx].desired_height(area.width) as usize;
|
|
let current_top = self.scroll_offset;
|
|
let current_bottom = current_top.saturating_add(area.height.saturating_sub(1) as usize);
|
|
if first < current_top {
|
|
self.scroll_offset = first;
|
|
} else if last > current_bottom {
|
|
self.scroll_offset = last.saturating_sub(area.height.saturating_sub(1) as usize);
|
|
}
|
|
}
|
|
}
|
|
|
|
/// A renderable that caches its desired height.
|
|
struct CachedRenderable {
|
|
renderable: Box<dyn Renderable>,
|
|
height: std::cell::Cell<Option<u16>>,
|
|
last_width: std::cell::Cell<Option<u16>>,
|
|
}
|
|
|
|
impl CachedRenderable {
|
|
fn new(renderable: impl Into<Box<dyn Renderable>>) -> Self {
|
|
Self {
|
|
renderable: renderable.into(),
|
|
height: std::cell::Cell::new(None),
|
|
last_width: std::cell::Cell::new(None),
|
|
}
|
|
}
|
|
}
|
|
|
|
impl Renderable for CachedRenderable {
|
|
fn render(&self, area: Rect, buf: &mut Buffer) {
|
|
self.renderable.render(area, buf);
|
|
}
|
|
fn desired_height(&self, width: u16) -> u16 {
|
|
if self.last_width.get() != Some(width) {
|
|
let height = self.renderable.desired_height(width);
|
|
self.height.set(Some(height));
|
|
self.last_width.set(Some(width));
|
|
}
|
|
self.height.get().unwrap_or(0)
|
|
}
|
|
}
|
|
|
|
struct CellRenderable {
|
|
cell: Arc<dyn HistoryCell>,
|
|
style: Style,
|
|
}
|
|
|
|
impl Renderable for CellRenderable {
|
|
fn render(&self, area: Rect, buf: &mut Buffer) {
|
|
let p =
|
|
Paragraph::new(Text::from(self.cell.transcript_lines(area.width))).style(self.style);
|
|
p.render(area, buf);
|
|
}
|
|
|
|
fn desired_height(&self, width: u16) -> u16 {
|
|
self.cell.desired_transcript_height(width)
|
|
}
|
|
}
|
|
|
|
pub(crate) struct TranscriptOverlay {
|
|
view: PagerView,
|
|
cells: Vec<Arc<dyn HistoryCell>>,
|
|
highlight_cell: Option<usize>,
|
|
is_done: bool,
|
|
}
|
|
|
|
impl TranscriptOverlay {
|
|
pub(crate) fn new(transcript_cells: Vec<Arc<dyn HistoryCell>>) -> Self {
|
|
Self {
|
|
view: PagerView::new(
|
|
Self::render_cells(&transcript_cells, None),
|
|
"T R A N S C R I P T".to_string(),
|
|
usize::MAX,
|
|
),
|
|
cells: transcript_cells,
|
|
highlight_cell: None,
|
|
is_done: false,
|
|
}
|
|
}
|
|
|
|
fn render_cells(
|
|
cells: &[Arc<dyn HistoryCell>],
|
|
highlight_cell: Option<usize>,
|
|
) -> Vec<Box<dyn Renderable>> {
|
|
cells
|
|
.iter()
|
|
.enumerate()
|
|
.flat_map(|(i, c)| {
|
|
let mut v: Vec<Box<dyn Renderable>> = Vec::new();
|
|
let mut cell_renderable = if c.as_any().is::<UserHistoryCell>() {
|
|
Box::new(CachedRenderable::new(CellRenderable {
|
|
cell: c.clone(),
|
|
style: if highlight_cell == Some(i) {
|
|
user_message_style().reversed()
|
|
} else {
|
|
user_message_style()
|
|
},
|
|
})) as Box<dyn Renderable>
|
|
} else {
|
|
Box::new(CachedRenderable::new(CellRenderable {
|
|
cell: c.clone(),
|
|
style: Style::default(),
|
|
})) as Box<dyn Renderable>
|
|
};
|
|
if !c.is_stream_continuation() && i > 0 {
|
|
cell_renderable = Box::new(InsetRenderable::new(
|
|
cell_renderable,
|
|
Insets::tlbr(1, 0, 0, 0),
|
|
));
|
|
}
|
|
v.push(cell_renderable);
|
|
v
|
|
})
|
|
.collect()
|
|
}
|
|
|
|
pub(crate) fn insert_cell(&mut self, cell: Arc<dyn HistoryCell>) {
|
|
let follow_bottom = self.view.is_scrolled_to_bottom();
|
|
self.cells.push(cell);
|
|
self.view.renderables = Self::render_cells(&self.cells, self.highlight_cell);
|
|
if follow_bottom {
|
|
self.view.scroll_offset = usize::MAX;
|
|
}
|
|
}
|
|
|
|
pub(crate) fn set_highlight_cell(&mut self, cell: Option<usize>) {
|
|
self.highlight_cell = cell;
|
|
self.view.renderables = Self::render_cells(&self.cells, self.highlight_cell);
|
|
if let Some(idx) = self.highlight_cell {
|
|
self.view.scroll_chunk_into_view(idx);
|
|
}
|
|
}
|
|
|
|
fn render_hints(&self, area: Rect, buf: &mut Buffer) {
|
|
let line1 = Rect::new(area.x, area.y, area.width, 1);
|
|
let line2 = Rect::new(area.x, area.y.saturating_add(1), area.width, 1);
|
|
render_key_hints(line1, buf, PAGER_KEY_HINTS);
|
|
|
|
let mut pairs: Vec<(&[KeyBinding], &str)> =
|
|
vec![(&[KEY_Q], "to quit"), (&[KEY_ESC], "to edit prev")];
|
|
if self.highlight_cell.is_some() {
|
|
pairs.push((&[KEY_ENTER], "to edit message"));
|
|
}
|
|
render_key_hints(line2, buf, &pairs);
|
|
}
|
|
|
|
pub(crate) fn render(&mut self, area: Rect, buf: &mut Buffer) {
|
|
let top_h = area.height.saturating_sub(3);
|
|
let top = Rect::new(area.x, area.y, area.width, top_h);
|
|
let bottom = Rect::new(area.x, area.y + top_h, area.width, 3);
|
|
self.view.render(top, buf);
|
|
self.render_hints(bottom, buf);
|
|
}
|
|
}
|
|
|
|
impl TranscriptOverlay {
|
|
pub(crate) fn handle_event(&mut self, tui: &mut tui::Tui, event: TuiEvent) -> Result<()> {
|
|
match event {
|
|
TuiEvent::Key(key_event) => match key_event {
|
|
e if KEY_Q.is_press(e) || KEY_CTRL_C.is_press(e) || KEY_CTRL_T.is_press(e) => {
|
|
self.is_done = true;
|
|
Ok(())
|
|
}
|
|
other => self.view.handle_key_event(tui, other),
|
|
},
|
|
TuiEvent::Mouse(mouse_event) => self.view.handle_mouse_scroll(tui, mouse_event),
|
|
TuiEvent::Draw => {
|
|
tui.draw(u16::MAX, |frame| {
|
|
self.render(frame.area(), frame.buffer);
|
|
})?;
|
|
Ok(())
|
|
}
|
|
_ => Ok(()),
|
|
}
|
|
}
|
|
pub(crate) fn is_done(&self) -> bool {
|
|
self.is_done
|
|
}
|
|
}
|
|
|
|
pub(crate) struct StaticOverlay {
|
|
view: PagerView,
|
|
is_done: bool,
|
|
}
|
|
|
|
impl StaticOverlay {
|
|
pub(crate) fn with_title(lines: Vec<Line<'static>>, title: String) -> Self {
|
|
let paragraph = Paragraph::new(Text::from(lines)).wrap(Wrap { trim: false });
|
|
Self::with_renderables(vec![Box::new(CachedRenderable::new(paragraph))], title)
|
|
}
|
|
|
|
pub(crate) fn with_renderables(renderables: Vec<Box<dyn Renderable>>, title: String) -> Self {
|
|
Self {
|
|
view: PagerView::new(renderables, title, 0),
|
|
is_done: false,
|
|
}
|
|
}
|
|
|
|
fn render_hints(&self, area: Rect, buf: &mut Buffer) {
|
|
let line1 = Rect::new(area.x, area.y, area.width, 1);
|
|
let line2 = Rect::new(area.x, area.y.saturating_add(1), area.width, 1);
|
|
render_key_hints(line1, buf, PAGER_KEY_HINTS);
|
|
let pairs: Vec<(&[KeyBinding], &str)> = vec![(&[KEY_Q], "to quit")];
|
|
render_key_hints(line2, buf, &pairs);
|
|
}
|
|
|
|
pub(crate) fn render(&mut self, area: Rect, buf: &mut Buffer) {
|
|
let top_h = area.height.saturating_sub(3);
|
|
let top = Rect::new(area.x, area.y, area.width, top_h);
|
|
let bottom = Rect::new(area.x, area.y + top_h, area.width, 3);
|
|
self.view.render(top, buf);
|
|
self.render_hints(bottom, buf);
|
|
}
|
|
}
|
|
|
|
impl StaticOverlay {
|
|
pub(crate) fn handle_event(&mut self, tui: &mut tui::Tui, event: TuiEvent) -> Result<()> {
|
|
match event {
|
|
TuiEvent::Key(key_event) => match key_event {
|
|
e if KEY_Q.is_press(e) || KEY_CTRL_C.is_press(e) => {
|
|
self.is_done = true;
|
|
Ok(())
|
|
}
|
|
other => self.view.handle_key_event(tui, other),
|
|
},
|
|
TuiEvent::Mouse(mouse_event) => self.view.handle_mouse_scroll(tui, mouse_event),
|
|
TuiEvent::Draw => {
|
|
tui.draw(u16::MAX, |frame| {
|
|
self.render(frame.area(), frame.buffer);
|
|
})?;
|
|
Ok(())
|
|
}
|
|
_ => Ok(()),
|
|
}
|
|
}
|
|
pub(crate) fn is_done(&self) -> bool {
|
|
self.is_done
|
|
}
|
|
}
|
|
|
|
fn render_offset_content(
|
|
area: Rect,
|
|
buf: &mut Buffer,
|
|
renderable: &dyn Renderable,
|
|
scroll_offset: u16,
|
|
) -> u16 {
|
|
let height = renderable.desired_height(area.width);
|
|
let mut tall_buf = Buffer::empty(Rect::new(
|
|
0,
|
|
0,
|
|
area.width,
|
|
height.min(area.height + scroll_offset),
|
|
));
|
|
renderable.render(*tall_buf.area(), &mut tall_buf);
|
|
let copy_height = area
|
|
.height
|
|
.min(tall_buf.area().height.saturating_sub(scroll_offset));
|
|
for y in 0..copy_height {
|
|
let src_y = y + scroll_offset;
|
|
for x in 0..area.width {
|
|
buf[(area.x + x, area.y + y)] = tall_buf[(x, src_y)].clone();
|
|
}
|
|
}
|
|
|
|
copy_height
|
|
}
|
|
|
|
#[cfg(test)]
|
|
mod tests {
|
|
use super::*;
|
|
use codex_core::protocol::ExecCommandSource;
|
|
use codex_core::protocol::ReviewDecision;
|
|
use insta::assert_snapshot;
|
|
use std::collections::HashMap;
|
|
use std::path::PathBuf;
|
|
use std::sync::Arc;
|
|
use std::time::Duration;
|
|
|
|
use crate::exec_cell::CommandOutput;
|
|
use crate::history_cell;
|
|
use crate::history_cell::HistoryCell;
|
|
use crate::history_cell::new_patch_event;
|
|
use codex_core::protocol::FileChange;
|
|
use codex_protocol::parse_command::ParsedCommand;
|
|
use ratatui::Terminal;
|
|
use ratatui::backend::TestBackend;
|
|
use ratatui::text::Text;
|
|
|
|
#[derive(Debug)]
|
|
struct TestCell {
|
|
lines: Vec<Line<'static>>,
|
|
}
|
|
|
|
impl crate::history_cell::HistoryCell for TestCell {
|
|
fn display_lines(&self, _width: u16) -> Vec<Line<'static>> {
|
|
self.lines.clone()
|
|
}
|
|
|
|
fn transcript_lines(&self, _width: u16) -> Vec<Line<'static>> {
|
|
self.lines.clone()
|
|
}
|
|
}
|
|
|
|
fn paragraph_block(label: &str, lines: usize) -> Box<dyn Renderable> {
|
|
let text = Text::from(
|
|
(0..lines)
|
|
.map(|i| Line::from(format!("{label}{i}")))
|
|
.collect::<Vec<_>>(),
|
|
);
|
|
Box::new(Paragraph::new(text)) as Box<dyn Renderable>
|
|
}
|
|
|
|
#[test]
|
|
fn edit_prev_hint_is_visible() {
|
|
let mut overlay = TranscriptOverlay::new(vec![Arc::new(TestCell {
|
|
lines: vec![Line::from("hello")],
|
|
})]);
|
|
|
|
// Render into a small buffer and assert the backtrack hint is present
|
|
let area = Rect::new(0, 0, 40, 10);
|
|
let mut buf = Buffer::empty(area);
|
|
overlay.render(area, &mut buf);
|
|
|
|
// Flatten buffer to a string and check for the hint text
|
|
let mut s = String::new();
|
|
for y in area.y..area.bottom() {
|
|
for x in area.x..area.right() {
|
|
s.push(buf[(x, y)].symbol().chars().next().unwrap_or(' '));
|
|
}
|
|
s.push('\n');
|
|
}
|
|
assert!(
|
|
s.contains("edit prev"),
|
|
"expected 'edit prev' hint in overlay footer, got: {s:?}"
|
|
);
|
|
}
|
|
|
|
#[test]
|
|
fn transcript_overlay_snapshot_basic() {
|
|
// Prepare a transcript overlay with a few lines
|
|
let mut overlay = TranscriptOverlay::new(vec![
|
|
Arc::new(TestCell {
|
|
lines: vec![Line::from("alpha")],
|
|
}),
|
|
Arc::new(TestCell {
|
|
lines: vec![Line::from("beta")],
|
|
}),
|
|
Arc::new(TestCell {
|
|
lines: vec![Line::from("gamma")],
|
|
}),
|
|
]);
|
|
let mut term = Terminal::new(TestBackend::new(40, 10)).expect("term");
|
|
term.draw(|f| overlay.render(f.area(), f.buffer_mut()))
|
|
.expect("draw");
|
|
assert_snapshot!(term.backend());
|
|
}
|
|
|
|
fn buffer_to_text(buf: &Buffer, area: Rect) -> String {
|
|
let mut out = String::new();
|
|
for y in area.y..area.bottom() {
|
|
for x in area.x..area.right() {
|
|
let symbol = buf[(x, y)].symbol();
|
|
if symbol.is_empty() {
|
|
out.push(' ');
|
|
} else {
|
|
out.push(symbol.chars().next().unwrap_or(' '));
|
|
}
|
|
}
|
|
// Trim trailing spaces for stability.
|
|
while out.ends_with(' ') {
|
|
out.pop();
|
|
}
|
|
out.push('\n');
|
|
}
|
|
out
|
|
}
|
|
|
|
#[test]
|
|
fn transcript_overlay_apply_patch_scroll_vt100_clears_previous_page() {
|
|
let cwd = PathBuf::from("/repo");
|
|
let mut cells: Vec<Arc<dyn HistoryCell>> = Vec::new();
|
|
|
|
let mut approval_changes = HashMap::new();
|
|
approval_changes.insert(
|
|
PathBuf::from("foo.txt"),
|
|
FileChange::Add {
|
|
content: "hello\nworld\n".to_string(),
|
|
},
|
|
);
|
|
let approval_cell: Arc<dyn HistoryCell> = Arc::new(new_patch_event(approval_changes, &cwd));
|
|
cells.push(approval_cell);
|
|
|
|
let mut apply_changes = HashMap::new();
|
|
apply_changes.insert(
|
|
PathBuf::from("foo.txt"),
|
|
FileChange::Add {
|
|
content: "hello\nworld\n".to_string(),
|
|
},
|
|
);
|
|
let apply_begin_cell: Arc<dyn HistoryCell> = Arc::new(new_patch_event(apply_changes, &cwd));
|
|
cells.push(apply_begin_cell);
|
|
|
|
let apply_end_cell: Arc<dyn HistoryCell> =
|
|
history_cell::new_approval_decision_cell(vec!["ls".into()], ReviewDecision::Approved)
|
|
.into();
|
|
cells.push(apply_end_cell);
|
|
|
|
let mut exec_cell = crate::exec_cell::new_active_exec_command(
|
|
"exec-1".into(),
|
|
vec!["bash".into(), "-lc".into(), "ls".into()],
|
|
vec![ParsedCommand::Unknown { cmd: "ls".into() }],
|
|
ExecCommandSource::Agent,
|
|
None,
|
|
true,
|
|
);
|
|
exec_cell.complete_call(
|
|
"exec-1",
|
|
CommandOutput {
|
|
exit_code: 0,
|
|
aggregated_output: "src\nREADME.md\n".into(),
|
|
formatted_output: "src\nREADME.md\n".into(),
|
|
},
|
|
Duration::from_millis(420),
|
|
);
|
|
let exec_cell: Arc<dyn HistoryCell> = Arc::new(exec_cell);
|
|
cells.push(exec_cell);
|
|
|
|
let mut overlay = TranscriptOverlay::new(cells);
|
|
let area = Rect::new(0, 0, 80, 12);
|
|
let mut buf = Buffer::empty(area);
|
|
|
|
overlay.render(area, &mut buf);
|
|
overlay.view.scroll_offset = 0;
|
|
overlay.render(area, &mut buf);
|
|
|
|
let snapshot = buffer_to_text(&buf, area);
|
|
assert_snapshot!("transcript_overlay_apply_patch_scroll_vt100", snapshot);
|
|
}
|
|
|
|
#[test]
|
|
fn transcript_overlay_keeps_scroll_pinned_at_bottom() {
|
|
let mut overlay = TranscriptOverlay::new(
|
|
(0..20)
|
|
.map(|i| {
|
|
Arc::new(TestCell {
|
|
lines: vec![Line::from(format!("line{i}"))],
|
|
}) as Arc<dyn HistoryCell>
|
|
})
|
|
.collect(),
|
|
);
|
|
let mut term = Terminal::new(TestBackend::new(40, 12)).expect("term");
|
|
term.draw(|f| overlay.render(f.area(), f.buffer_mut()))
|
|
.expect("draw");
|
|
|
|
assert!(
|
|
overlay.view.is_scrolled_to_bottom(),
|
|
"expected initial render to leave view at bottom"
|
|
);
|
|
|
|
overlay.insert_cell(Arc::new(TestCell {
|
|
lines: vec!["tail".into()],
|
|
}));
|
|
|
|
assert_eq!(overlay.view.scroll_offset, usize::MAX);
|
|
}
|
|
|
|
#[test]
|
|
fn transcript_overlay_preserves_manual_scroll_position() {
|
|
let mut overlay = TranscriptOverlay::new(
|
|
(0..20)
|
|
.map(|i| {
|
|
Arc::new(TestCell {
|
|
lines: vec![Line::from(format!("line{i}"))],
|
|
}) as Arc<dyn HistoryCell>
|
|
})
|
|
.collect(),
|
|
);
|
|
let mut term = Terminal::new(TestBackend::new(40, 12)).expect("term");
|
|
term.draw(|f| overlay.render(f.area(), f.buffer_mut()))
|
|
.expect("draw");
|
|
|
|
overlay.view.scroll_offset = 0;
|
|
|
|
overlay.insert_cell(Arc::new(TestCell {
|
|
lines: vec!["tail".into()],
|
|
}));
|
|
|
|
assert_eq!(overlay.view.scroll_offset, 0);
|
|
}
|
|
|
|
#[test]
|
|
fn static_overlay_snapshot_basic() {
|
|
// Prepare a static overlay with a few lines and a title
|
|
let mut overlay = StaticOverlay::with_title(
|
|
vec!["one".into(), "two".into(), "three".into()],
|
|
"S T A T I C".to_string(),
|
|
);
|
|
let mut term = Terminal::new(TestBackend::new(40, 10)).expect("term");
|
|
term.draw(|f| overlay.render(f.area(), f.buffer_mut()))
|
|
.expect("draw");
|
|
assert_snapshot!(term.backend());
|
|
}
|
|
|
|
/// Render transcript overlay and return visible line numbers (`line-NN`) in order.
|
|
fn transcript_line_numbers(overlay: &mut TranscriptOverlay, area: Rect) -> Vec<usize> {
|
|
let mut buf = Buffer::empty(area);
|
|
overlay.render(area, &mut buf);
|
|
|
|
let top_h = area.height.saturating_sub(3);
|
|
let top = Rect::new(area.x, area.y, area.width, top_h);
|
|
let content_area = overlay.view.content_area(top);
|
|
|
|
let mut nums = Vec::new();
|
|
for y in content_area.y..content_area.bottom() {
|
|
let mut line = String::new();
|
|
for x in content_area.x..content_area.right() {
|
|
line.push(buf[(x, y)].symbol().chars().next().unwrap_or(' '));
|
|
}
|
|
if let Some(n) = line
|
|
.split_whitespace()
|
|
.find_map(|w| w.strip_prefix("line-"))
|
|
.and_then(|s| s.parse().ok())
|
|
{
|
|
nums.push(n);
|
|
}
|
|
}
|
|
nums
|
|
}
|
|
|
|
#[test]
|
|
fn transcript_overlay_paging_is_continuous_and_round_trips() {
|
|
let mut overlay = TranscriptOverlay::new(
|
|
(0..50)
|
|
.map(|i| {
|
|
Arc::new(TestCell {
|
|
lines: vec![Line::from(format!("line-{i:02}"))],
|
|
}) as Arc<dyn HistoryCell>
|
|
})
|
|
.collect(),
|
|
);
|
|
let area = Rect::new(0, 0, 40, 15);
|
|
|
|
// Prime layout so last_content_height is populated and paging uses the real content height.
|
|
let mut buf = Buffer::empty(area);
|
|
overlay.view.scroll_offset = 0;
|
|
overlay.render(area, &mut buf);
|
|
let page_height = overlay.view.page_height(area);
|
|
|
|
// Scenario 1: starting from the top, PageDown should show the next page of content.
|
|
overlay.view.scroll_offset = 0;
|
|
let page1 = transcript_line_numbers(&mut overlay, area);
|
|
let page1_len = page1.len();
|
|
let expected_page1: Vec<usize> = (0..page1_len).collect();
|
|
assert_eq!(
|
|
page1, expected_page1,
|
|
"first page should start at line-00 and show a full page of content"
|
|
);
|
|
|
|
overlay.view.scroll_offset = overlay.view.scroll_offset.saturating_add(page_height);
|
|
let page2 = transcript_line_numbers(&mut overlay, area);
|
|
assert_eq!(
|
|
page2.len(),
|
|
page1_len,
|
|
"second page should have the same number of visible lines as the first page"
|
|
);
|
|
let expected_page2_first = *page1.last().unwrap() + 1;
|
|
assert_eq!(
|
|
page2[0], expected_page2_first,
|
|
"second page after PageDown should immediately follow the first page"
|
|
);
|
|
|
|
// Scenario 2: from an interior offset (start=3), PageDown then PageUp should round-trip.
|
|
let interior_offset = 3usize;
|
|
overlay.view.scroll_offset = interior_offset;
|
|
let before = transcript_line_numbers(&mut overlay, area);
|
|
overlay.view.scroll_offset = overlay.view.scroll_offset.saturating_add(page_height);
|
|
let _ = transcript_line_numbers(&mut overlay, area);
|
|
overlay.view.scroll_offset = overlay.view.scroll_offset.saturating_sub(page_height);
|
|
let after = transcript_line_numbers(&mut overlay, area);
|
|
assert_eq!(
|
|
before, after,
|
|
"PageDown+PageUp from interior offset ({interior_offset}) should round-trip"
|
|
);
|
|
|
|
// Scenario 3: from the top of the second page, PageUp then PageDown should round-trip.
|
|
overlay.view.scroll_offset = page_height;
|
|
let before2 = transcript_line_numbers(&mut overlay, area);
|
|
overlay.view.scroll_offset = overlay.view.scroll_offset.saturating_sub(page_height);
|
|
let _ = transcript_line_numbers(&mut overlay, area);
|
|
overlay.view.scroll_offset = overlay.view.scroll_offset.saturating_add(page_height);
|
|
let after2 = transcript_line_numbers(&mut overlay, area);
|
|
assert_eq!(
|
|
before2, after2,
|
|
"PageUp+PageDown from the top of the second page should round-trip"
|
|
);
|
|
}
|
|
|
|
#[test]
|
|
fn static_overlay_wraps_long_lines() {
|
|
let mut overlay = StaticOverlay::with_title(
|
|
vec!["a very long line that should wrap when rendered within a narrow pager overlay width".into()],
|
|
"S T A T I C".to_string(),
|
|
);
|
|
let mut term = Terminal::new(TestBackend::new(24, 8)).expect("term");
|
|
term.draw(|f| overlay.render(f.area(), f.buffer_mut()))
|
|
.expect("draw");
|
|
assert_snapshot!(term.backend());
|
|
}
|
|
|
|
#[test]
|
|
fn pager_view_content_height_counts_renderables() {
|
|
let pv = PagerView::new(
|
|
vec![paragraph_block("a", 2), paragraph_block("b", 3)],
|
|
"T".to_string(),
|
|
0,
|
|
);
|
|
|
|
assert_eq!(pv.content_height(80), 5);
|
|
}
|
|
|
|
#[test]
|
|
fn pager_view_ensure_chunk_visible_scrolls_down_when_needed() {
|
|
let mut pv = PagerView::new(
|
|
vec![
|
|
paragraph_block("a", 1),
|
|
paragraph_block("b", 3),
|
|
paragraph_block("c", 3),
|
|
],
|
|
"T".to_string(),
|
|
0,
|
|
);
|
|
let area = Rect::new(0, 0, 20, 8);
|
|
|
|
pv.scroll_offset = 0;
|
|
let content_area = pv.content_area(area);
|
|
pv.ensure_chunk_visible(2, content_area);
|
|
|
|
let mut buf = Buffer::empty(area);
|
|
pv.render(area, &mut buf);
|
|
let rendered = buffer_to_text(&buf, area);
|
|
|
|
assert!(
|
|
rendered.contains("c0"),
|
|
"expected chunk top in view: {rendered:?}"
|
|
);
|
|
assert!(
|
|
rendered.contains("c1"),
|
|
"expected chunk middle in view: {rendered:?}"
|
|
);
|
|
assert!(
|
|
rendered.contains("c2"),
|
|
"expected chunk bottom in view: {rendered:?}"
|
|
);
|
|
}
|
|
|
|
#[test]
|
|
fn pager_view_ensure_chunk_visible_scrolls_up_when_needed() {
|
|
let mut pv = PagerView::new(
|
|
vec![
|
|
paragraph_block("a", 2),
|
|
paragraph_block("b", 3),
|
|
paragraph_block("c", 3),
|
|
],
|
|
"T".to_string(),
|
|
0,
|
|
);
|
|
let area = Rect::new(0, 0, 20, 3);
|
|
|
|
pv.scroll_offset = 6;
|
|
pv.ensure_chunk_visible(0, area);
|
|
|
|
assert_eq!(pv.scroll_offset, 0);
|
|
}
|
|
|
|
#[test]
|
|
fn pager_view_is_scrolled_to_bottom_accounts_for_wrapped_height() {
|
|
let mut pv = PagerView::new(vec![paragraph_block("a", 10)], "T".to_string(), 0);
|
|
let area = Rect::new(0, 0, 20, 8);
|
|
let mut buf = Buffer::empty(area);
|
|
|
|
pv.render(area, &mut buf);
|
|
|
|
assert!(
|
|
!pv.is_scrolled_to_bottom(),
|
|
"expected view to report not at bottom when offset < max"
|
|
);
|
|
|
|
pv.scroll_offset = usize::MAX;
|
|
pv.render(area, &mut buf);
|
|
|
|
assert!(
|
|
pv.is_scrolled_to_bottom(),
|
|
"expected view to report at bottom after scrolling to end"
|
|
);
|
|
}
|
|
}
|