mirror of
https://github.com/openai/codex.git
synced 2026-12-05 01:00:54 +00:00
Keep transcript Find results readable after closing the query (#50039)
## Why Closing Find previously restored the pre-search view, making it difficult to read a match and its surrounding transcript context. ## What changed - Make `Esc` with a nonempty query close query editing while preserving the query, highlighted match, and expanded output. A second `Esc` returns to the latest output; `Ctrl+C` cancels Find and restores the pre-search presentation. - Allow paging through surrounding context and loading older history without losing the query. Keep `Ctrl+N` and `Ctrl+P` available for match navigation, and reopen Find to edit the retained query. - Show context above matches when space permits, preserve match layouts across resizing, and display navigation and history status while reading results. ## Testing Add regression tests and snapshots for retained results, surrounding context, resizing, paging around live output, older-history loading, and footer status. Update interaction tests for the additional `Esc` transition. GitOrigin-RevId: d822956d3c6ffab88fc1665ebdf0c86ccf110899
This commit is contained in:
@@ -26,7 +26,7 @@ impl App {
|
||||
HistoryRenderMode::Rich => match (
|
||||
self.overlay.as_ref(),
|
||||
view.is_detailed(),
|
||||
view.is_search_active(),
|
||||
view.is_search_editing(),
|
||||
view.is_activity_focused(),
|
||||
view.has_selection_range(),
|
||||
) {
|
||||
|
||||
@@ -16,7 +16,7 @@ impl App {
|
||||
cfg!(unix)
|
||||
&& !self.enhanced_keys_supported
|
||||
&& self.overlay.is_none()
|
||||
&& !self.transcript_view.is_search_active()
|
||||
&& !self.transcript_view.is_search_editing()
|
||||
&& self.chat_widget.no_modal_or_popup_active()
|
||||
&& matches!(key_event.code, KeyCode::Char(_))
|
||||
&& matches!(key_event.kind, KeyEventKind::Press | KeyEventKind::Repeat)
|
||||
@@ -185,7 +185,7 @@ impl App {
|
||||
use crate::keymap::KeymapContextSet;
|
||||
|
||||
if let Some(overlay) = &self.overlay {
|
||||
let context = if matches!(overlay, Overlay::Transcript(view) if view.is_search_active())
|
||||
let context = if matches!(overlay, Overlay::Transcript(view) if view.is_search_editing())
|
||||
{
|
||||
KeymapContext::Editor
|
||||
} else {
|
||||
@@ -198,7 +198,7 @@ impl App {
|
||||
contexts
|
||||
};
|
||||
}
|
||||
if self.transcript_view.is_search_active() && self.chat_widget.no_modal_or_popup_active() {
|
||||
if self.transcript_view.is_search_editing() && self.chat_widget.no_modal_or_popup_active() {
|
||||
return KeymapContextSet::new(KeymapContext::Editor).with_voice_toggle(&self.keymap);
|
||||
}
|
||||
if self.transcript_view.is_activity_focused() && self.chat_widget.no_modal_or_popup_active()
|
||||
|
||||
@@ -471,7 +471,7 @@ impl App {
|
||||
}
|
||||
let action = match event {
|
||||
TuiEvent::Key(key)
|
||||
if !self.transcript_view.is_search_active()
|
||||
if !self.transcript_view.is_search_editing()
|
||||
&& self.keymap.app.find_transcript.is_pressed(*key) =>
|
||||
{
|
||||
self.transcript_view.begin_search();
|
||||
|
||||
@@ -105,7 +105,7 @@ async fn browsing_search_and_selection_consume_escape_before_mode_exit() -> Resu
|
||||
.await?;
|
||||
assert_eq!(
|
||||
(
|
||||
app.transcript_view.is_search_active(),
|
||||
app.transcript_view.is_search_editing(),
|
||||
app.backtrack.overlay_preview_active
|
||||
),
|
||||
(false, true)
|
||||
@@ -120,6 +120,9 @@ async fn browsing_search_and_selection_consume_escape_before_mode_exit() -> Resu
|
||||
}
|
||||
assert!(app.transcript_view.has_active_interaction());
|
||||
assert_eq!(app.backtrack.nth_user_message, 1);
|
||||
app.handle_tui_event(&mut tui, &mut server, TuiEvent::Key(KeyCode::Esc.into()))
|
||||
.await?;
|
||||
assert!(app.transcript_view.has_active_interaction());
|
||||
app.handle_tui_event(&mut tui, &mut server, TuiEvent::Key(KeyCode::Esc.into()))
|
||||
.await?;
|
||||
assert_eq!(
|
||||
|
||||
@@ -415,7 +415,7 @@ async fn enter_preserves_search_backtrack_and_modal_ownership_while_scrolled() -
|
||||
let enter = TuiEvent::Key(KeyEvent::from(KeyCode::Enter));
|
||||
app.transcript_view.begin_search();
|
||||
assert!(app.handle_owned_transcript_event(&mut tui, &mut server, &enter)?);
|
||||
assert!(app.transcript_view.is_search_active());
|
||||
assert!(app.transcript_view.is_search_editing());
|
||||
assert!(!app.transcript_view.is_following());
|
||||
app.handle_owned_transcript_event(
|
||||
&mut tui,
|
||||
|
||||
@@ -853,7 +853,7 @@ async fn inline_transcript_search_draws_and_escape_precedes_backtrack() -> Resul
|
||||
)
|
||||
.await?;
|
||||
assert!(
|
||||
matches!(&app.overlay, Some(Overlay::Transcript(overlay)) if !overlay.has_active_interaction())
|
||||
matches!(&app.overlay, Some(Overlay::Transcript(overlay)) if overlay.has_active_interaction() && !overlay.is_search_editing())
|
||||
);
|
||||
assert!(!app.backtrack.overlay_preview_active);
|
||||
app.handle_backtrack_overlay_event(
|
||||
@@ -862,6 +862,13 @@ async fn inline_transcript_search_draws_and_escape_precedes_backtrack() -> Resul
|
||||
TuiEvent::Key(KeyEvent::new(KeyCode::Esc, KeyModifiers::NONE)),
|
||||
)
|
||||
.await?;
|
||||
assert!(!app.backtrack.overlay_preview_active);
|
||||
app.handle_backtrack_overlay_event(
|
||||
&mut tui,
|
||||
&mut app_server,
|
||||
TuiEvent::Key(KeyEvent::new(KeyCode::Esc, KeyModifiers::NONE)),
|
||||
)
|
||||
.await?;
|
||||
assert!(app.backtrack.overlay_preview_active);
|
||||
app.close_transcript_overlay(&mut tui);
|
||||
app_server.shutdown().await?;
|
||||
@@ -962,8 +969,8 @@ async fn find_owns_editor_chords_without_changing_the_composer_draft() -> Result
|
||||
)
|
||||
.await?;
|
||||
assert!(!match &app.overlay {
|
||||
Some(Overlay::Transcript(overlay)) => overlay.is_search_active(),
|
||||
_ => app.transcript_view.is_search_active(),
|
||||
Some(Overlay::Transcript(overlay)) => overlay.is_search_editing(),
|
||||
_ => app.transcript_view.is_search_editing(),
|
||||
});
|
||||
assert_eq!(
|
||||
app.chat_widget.composer_text_with_pending(),
|
||||
@@ -1005,7 +1012,7 @@ async fn offline_find_closes_before_the_next_ctrl_c_quits() -> Result<()> {
|
||||
.await?,
|
||||
AppRunControl::Continue
|
||||
));
|
||||
assert!(!app.transcript_view.is_search_active());
|
||||
assert!(!app.transcript_view.is_search_editing());
|
||||
assert_eq!(
|
||||
app.chat_widget.composer_text_with_pending(),
|
||||
"offline draft"
|
||||
@@ -1089,9 +1096,9 @@ async fn offline_backtrack_keeps_the_preview_and_draft_without_reverting() -> Re
|
||||
)
|
||||
.await?;
|
||||
assert!(if owned {
|
||||
app.transcript_view.is_search_active()
|
||||
app.transcript_view.is_search_editing()
|
||||
} else {
|
||||
matches!(&app.overlay, Some(Overlay::Transcript(overlay)) if overlay.is_search_active())
|
||||
matches!(&app.overlay, Some(Overlay::Transcript(overlay)) if overlay.is_search_editing())
|
||||
});
|
||||
for (key, preview_active, draft) in [
|
||||
(KeyCode::Esc, true, "offline draft"),
|
||||
|
||||
@@ -67,7 +67,7 @@ impl App {
|
||||
};
|
||||
if !tui.is_owned_screen()
|
||||
|| self.overlay.is_some()
|
||||
|| self.transcript_view.is_search_active()
|
||||
|| self.transcript_view.is_search_editing()
|
||||
|| !allowed
|
||||
{
|
||||
return None;
|
||||
|
||||
@@ -8644,7 +8644,7 @@ async fn prompt_edit_reverts_earlier_and_first_visible_prompts_in_place() -> Res
|
||||
);
|
||||
// Editing the first visible prompt also discards search state from the removed window.
|
||||
app.transcript_view.begin_search();
|
||||
assert!(app.transcript_view.is_search_active());
|
||||
assert!(app.transcript_view.is_search_editing());
|
||||
app.chat_widget.restore_thread_input_state(
|
||||
/*input_state*/ None,
|
||||
crate::chatwidget::ThreadInputStateRestoreMode {
|
||||
@@ -8668,7 +8668,7 @@ async fn prompt_edit_reverts_earlier_and_first_visible_prompts_in_place() -> Res
|
||||
Box::pin(app.handle_event(&mut tui, &mut app_server, event)).await?;
|
||||
}
|
||||
}
|
||||
assert!(!app.transcript_view.is_search_active());
|
||||
assert!(!app.transcript_view.is_search_editing());
|
||||
assert!(app.transcript_view.is_following());
|
||||
assert_eq!(app.chat_widget.thread_id(), Some(source_thread_id));
|
||||
assert_eq!(
|
||||
|
||||
@@ -123,7 +123,7 @@ impl TranscriptOverlay {
|
||||
.map(|column| (status.x + column, status.y));
|
||||
if let Some(notice) = &self.notice {
|
||||
let notice = Line::from(notice.clone()).dim();
|
||||
if self.view.is_search_active() {
|
||||
if self.view.is_search_editing() {
|
||||
footer.text.lines.truncate(/*len*/ 1);
|
||||
footer.text.lines.push(notice);
|
||||
} else {
|
||||
@@ -214,7 +214,7 @@ impl TranscriptOverlay {
|
||||
self.view.handle_mouse(mouse, &self.cells)
|
||||
}
|
||||
TuiEvent::Paste(text) => {
|
||||
if self.view.is_search_active() {
|
||||
if self.view.is_search_editing() {
|
||||
self.view.end_selection(&self.cells);
|
||||
}
|
||||
self.view.paste_search(&text).then_some(ViewAction::Changed)
|
||||
@@ -252,8 +252,8 @@ impl TranscriptOverlay {
|
||||
self.view.begin_search();
|
||||
}
|
||||
|
||||
pub(crate) fn is_search_active(&self) -> bool {
|
||||
self.view.is_search_active()
|
||||
pub(crate) fn is_search_editing(&self) -> bool {
|
||||
self.view.is_search_editing()
|
||||
}
|
||||
|
||||
pub(crate) fn owns_interaction_key(&self, key: KeyEvent) -> bool {
|
||||
@@ -415,7 +415,7 @@ impl TranscriptOverlay {
|
||||
if key.kind == KeyEventKind::Release {
|
||||
return None;
|
||||
}
|
||||
if !self.view.is_search_active()
|
||||
if !self.view.is_search_editing()
|
||||
&& (!self.view.has_active_interaction() || !self.view.owns_interaction_key(key))
|
||||
&& self.keymap.find.is_pressed(key)
|
||||
{
|
||||
|
||||
@@ -277,7 +277,7 @@ async fn opening_find_retains_the_selected_mutable_revision() -> Result<()> {
|
||||
&mut tui,
|
||||
TuiEvent::Key(KeyEvent::new(KeyCode::F(3), KeyModifiers::NONE)),
|
||||
)?;
|
||||
assert!(overlay.view.is_search_active());
|
||||
assert!(overlay.view.is_search_editing());
|
||||
overlay.handle_event(
|
||||
&mut tui,
|
||||
TuiEvent::Key(KeyEvent::new(KeyCode::Esc, KeyModifiers::NONE)),
|
||||
|
||||
@@ -1088,7 +1088,7 @@ impl PickerState {
|
||||
self.chord_matcher.cancel();
|
||||
return Some(key);
|
||||
}
|
||||
let context = if matches!(&self.overlay, Some(Overlay::Transcript(overlay)) if overlay.is_search_active())
|
||||
let context = if matches!(&self.overlay, Some(Overlay::Transcript(overlay)) if overlay.is_search_editing())
|
||||
{
|
||||
crate::keymap::KeymapContext::Editor
|
||||
} else if self.overlay.is_some() {
|
||||
|
||||
@@ -411,9 +411,10 @@ impl TranscriptView {
|
||||
let index = self.next_nonempty(cells, index).unwrap_or(index);
|
||||
let bottom = self.bottom_start(cells);
|
||||
if rows > 0 && (index, row) >= bottom {
|
||||
if self.selection.is_none() {
|
||||
if self.selection.is_none() && !self.search.is_active() {
|
||||
self.jump_to_latest();
|
||||
} else {
|
||||
self.release_live_reading();
|
||||
self.position = Position::Latest;
|
||||
}
|
||||
return;
|
||||
@@ -471,7 +472,7 @@ impl TranscriptView {
|
||||
|
||||
pub(crate) fn needs_history(&mut self, cells: &[Arc<dyn HistoryCell>]) -> bool {
|
||||
self.search.needs_history(self.history)
|
||||
|| (!self.search.is_active() && self.near_start(cells))
|
||||
|| (self.search.allows_viewport_paging() && self.near_start(cells))
|
||||
}
|
||||
|
||||
pub(crate) fn near_start(&mut self, cells: &[Arc<dyn HistoryCell>]) -> bool {
|
||||
|
||||
@@ -74,9 +74,9 @@ fn escape_closes_selection_and_search_before_returning_to_latest() {
|
||||
let escape = KeyEvent::new(KeyCode::Esc, KeyModifiers::NONE);
|
||||
view.handle_key(escape, &cells);
|
||||
assert!(view.selection.is_none());
|
||||
assert!(view.is_search_active());
|
||||
assert!(view.is_search_editing());
|
||||
view.handle_key(escape, &cells);
|
||||
assert!(!view.is_search_active());
|
||||
assert!(!view.is_search_editing());
|
||||
assert!(!view.is_following());
|
||||
assert!(matches!(
|
||||
view.handle_key(escape, &cells),
|
||||
|
||||
@@ -54,7 +54,7 @@ impl TranscriptView {
|
||||
|| self.tail_visible
|
||||
|| self.highlight.is_some()
|
||||
|| self.selection.is_some()
|
||||
|| self.is_search_active()
|
||||
|| self.is_search_editing()
|
||||
{
|
||||
return None;
|
||||
}
|
||||
|
||||
@@ -158,17 +158,17 @@ fn copying_selection_at_bottom_does_not_show_back_to_bottom() {
|
||||
fn closing_find_at_bottom_does_not_show_back_to_bottom() {
|
||||
let cells = vec![cell("one\ntwo\nthree\nfour\nneedle")];
|
||||
let mut view = TranscriptView::default();
|
||||
let before = paint(&mut view, &cells, /*width*/ 40);
|
||||
paint(&mut view, &cells, /*width*/ 40);
|
||||
view.begin_selection(&cells, /*column*/ 0, /*row*/ 2, /*clicks*/ 2);
|
||||
view.end_drag();
|
||||
view.begin_search();
|
||||
view.paste_search("needle");
|
||||
assert!(!view.advance_search(&cells));
|
||||
paint(&mut view, &cells, /*width*/ 40);
|
||||
let before = paint(&mut view, &cells, /*width*/ 40);
|
||||
|
||||
view.handle_key(KeyCode::Esc.into(), &cells);
|
||||
let after = paint(&mut view, &cells, /*width*/ 40);
|
||||
assert!(!view.is_search_active());
|
||||
assert!(!view.is_search_editing());
|
||||
assert!(!view.is_following());
|
||||
assert!(view.tail_visible);
|
||||
assert!(view.follow_control.area.is_none());
|
||||
|
||||
@@ -66,6 +66,13 @@ impl TranscriptView {
|
||||
is_interactive: true,
|
||||
});
|
||||
}
|
||||
if self.search.is_reading() && !pending {
|
||||
return Some(TranscriptFooter {
|
||||
text: self.search.status_line(width, self.history).into(),
|
||||
cursor_column: None,
|
||||
is_interactive: false,
|
||||
});
|
||||
}
|
||||
if self.is_activity_focused() {
|
||||
return self.disclosure_footer(width);
|
||||
}
|
||||
|
||||
@@ -267,12 +267,48 @@ fn loading_and_failure_preserve_search_query_and_caret() {
|
||||
}
|
||||
insta::assert_snapshot!(hints.join("\n"));
|
||||
view.history = TranscriptHistoryState::Complete;
|
||||
view.handle_key(KeyEvent::new(KeyCode::Esc, KeyModifiers::NONE), &cells);
|
||||
assert!(!view.is_search_active());
|
||||
assert!(!view.advance_search(&cells));
|
||||
view.handle_key(KeyCode::Esc.into(), &cells);
|
||||
insta::assert_snapshot!(view.footer(/*width*/ 32, MotionMode::Reduced).unwrap().text.to_string(), @"No matches · esc latest");
|
||||
view.handle_key(
|
||||
KeyEvent::new(KeyCode::Char('c'), KeyModifiers::CONTROL),
|
||||
&cells,
|
||||
);
|
||||
assert!(!view.is_search_editing());
|
||||
assert!(!view.is_detailed());
|
||||
assert!(view.footer(/*width*/ 32, MotionMode::Reduced).is_none());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn reading_a_find_result_shows_history_loading_and_failure() {
|
||||
let cells: Vec<Arc<dyn HistoryCell>> = vec![Arc::new(PlainHistoryCell::new(vec![
|
||||
"visible needle".into(),
|
||||
]))];
|
||||
let mut view = TranscriptView::default();
|
||||
render(&mut view, &cells);
|
||||
view.begin_search();
|
||||
view.paste_search("needle");
|
||||
assert!(!view.advance_search(&cells));
|
||||
view.handle_key(KeyCode::Esc.into(), &cells);
|
||||
|
||||
let mut hints = Vec::new();
|
||||
for history in [
|
||||
TranscriptHistoryState::LoadingOlder,
|
||||
TranscriptHistoryState::Failed,
|
||||
TranscriptHistoryState::Complete,
|
||||
] {
|
||||
view.history = history;
|
||||
let footer = view.footer(/*width*/ 80, MotionMode::Reduced).unwrap();
|
||||
assert_eq!((footer.cursor_column, footer.is_interactive), (None, false));
|
||||
hints.push(footer.text.to_string());
|
||||
}
|
||||
insta::assert_snapshot!(hints.join("\n"), @"
|
||||
↑ Loading earlier messages… · esc latest
|
||||
Retry history: ⌥</⌃home. esc latest
|
||||
ctrl+n next · ctrl+p previous · esc latest
|
||||
");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn loading_completion_restores_unseen_activity_and_failure_stops_motion() {
|
||||
let mut cells: Vec<Arc<dyn HistoryCell>> = vec![Arc::new(PlainHistoryCell::new(vec![
|
||||
|
||||
@@ -149,8 +149,11 @@ impl TranscriptView {
|
||||
|| (modifiers == KeyModifiers::NONE
|
||||
&& matches!(code, KeyCode::PageUp | KeyCode::PageDown));
|
||||
}
|
||||
self.is_search_active()
|
||||
&& (matches!(code, KeyCode::Esc | KeyCode::Enter)
|
||||
self.search.is_active()
|
||||
&& (code == KeyCode::Esc
|
||||
|| (code == KeyCode::Enter && self.is_search_editing())
|
||||
|| (modifiers == KeyModifiers::NONE
|
||||
&& matches!(code, KeyCode::PageUp | KeyCode::PageDown))
|
||||
|| (modifiers == KeyModifiers::CONTROL
|
||||
&& matches!(code, KeyCode::Char('c' | 'n' | 'p'))))
|
||||
}
|
||||
|
||||
@@ -101,6 +101,9 @@ impl TranscriptView {
|
||||
return None;
|
||||
}
|
||||
let key = self.entry_key(cells, index);
|
||||
if let Some(layout) = self.search.match_layout(key) {
|
||||
return Some(layout);
|
||||
}
|
||||
if let Some(layout) = self
|
||||
.snapshot()
|
||||
.and_then(|snapshot| snapshot.pinned.get(&key))
|
||||
|
||||
@@ -3,21 +3,21 @@
|
||||
//! Search retains one match and one entry being scanned. Each frame examines a bounded text
|
||||
//! chunk; reaching the oldest loaded entry asks the app's existing history pager to continue.
|
||||
//! The pager owns loading and failure status; search only remembers that it needs another page.
|
||||
//! Closing the query keeps the match readable; cancellation restores the original presentation.
|
||||
|
||||
use crate::bottom_pane::TextArea;
|
||||
use crate::bottom_pane::TextAreaState;
|
||||
use crate::keymap::RuntimeKeymap;
|
||||
use crossterm::event::KeyCode;
|
||||
use crossterm::event::KeyEvent;
|
||||
use crossterm::event::KeyModifiers;
|
||||
use ratatui::text::Span;
|
||||
use ratatui::widgets::StatefulWidgetRef;
|
||||
use std::ops::Range;
|
||||
use unicode_segmentation::UnicodeSegmentation;
|
||||
use unicode_width::UnicodeWidthStr;
|
||||
|
||||
use super::*;
|
||||
|
||||
#[path = "search_footer.rs"]
|
||||
mod footer;
|
||||
|
||||
const QUERY_BYTES: usize = 4096;
|
||||
const SCAN_BYTES: usize = 16 * 1024;
|
||||
const ENTRIES_PER_FRAME: usize = 8;
|
||||
@@ -48,10 +48,18 @@ enum Progress {
|
||||
struct Match {
|
||||
anchor: Anchor,
|
||||
end: usize,
|
||||
layout: Arc<TextLayout>,
|
||||
}
|
||||
|
||||
#[derive(Clone, Copy, Eq, PartialEq)]
|
||||
enum SearchMode {
|
||||
Closed,
|
||||
Editing,
|
||||
Reading,
|
||||
}
|
||||
|
||||
pub(super) struct Search {
|
||||
active: bool,
|
||||
mode: SearchMode,
|
||||
editor: TextArea,
|
||||
folded_query: String,
|
||||
saved_position: Position,
|
||||
@@ -67,7 +75,7 @@ pub(super) struct Search {
|
||||
impl Default for Search {
|
||||
fn default() -> Self {
|
||||
Self {
|
||||
active: false,
|
||||
mode: SearchMode::Closed,
|
||||
editor: TextArea::new(),
|
||||
folded_query: String::new(),
|
||||
saved_position: Position::default(),
|
||||
@@ -85,7 +93,7 @@ impl Default for Search {
|
||||
impl TranscriptView {
|
||||
pub(crate) fn begin_search(&mut self) {
|
||||
self.cancel_beginning();
|
||||
if self.search.active {
|
||||
if self.is_search_editing() {
|
||||
return;
|
||||
}
|
||||
let saved_position = self.position;
|
||||
@@ -94,8 +102,16 @@ impl TranscriptView {
|
||||
.take()
|
||||
.map(|selection| selection.snapshot)
|
||||
.or(self.held_reading.take());
|
||||
if self.search.is_reading() {
|
||||
self.held_reading = saved_snapshot;
|
||||
self.search.mode = SearchMode::Editing;
|
||||
if self.search.current.is_none() {
|
||||
self.search.progress = Progress::Restart;
|
||||
}
|
||||
return;
|
||||
}
|
||||
let saved_detailed = self.detailed;
|
||||
self.search.active = true;
|
||||
self.search.mode = SearchMode::Editing;
|
||||
self.disclosure.focused = None;
|
||||
self.set_presentation(/*detailed*/ true, self.mode);
|
||||
self.search.saved_position = saved_position;
|
||||
@@ -115,42 +131,8 @@ impl TranscriptView {
|
||||
self.live_key = None;
|
||||
}
|
||||
|
||||
pub(crate) fn is_search_active(&self) -> bool {
|
||||
self.search.active
|
||||
}
|
||||
|
||||
/// Project the editor and its caret from the same one-row viewport into the existing footer.
|
||||
pub(crate) fn search_footer(&self, width: u16) -> Option<(Line<'static>, u16)> {
|
||||
if !self.search.active || width == 0 {
|
||||
return None;
|
||||
}
|
||||
let area = Rect::new(/*x*/ 0, /*y*/ 0, width, /*height*/ 1);
|
||||
let mut buffer = Buffer::empty(area);
|
||||
let prefix_width = width.saturating_sub(/*rhs*/ 1).min(/*other*/ 6);
|
||||
Line::from("Find: ").dim().render(
|
||||
Rect::new(/*x*/ 0, /*y*/ 0, prefix_width, /*height*/ 1),
|
||||
&mut buffer,
|
||||
);
|
||||
let query_area = Rect::new(
|
||||
prefix_width,
|
||||
/*y*/ 0,
|
||||
width - prefix_width,
|
||||
/*height*/ 1,
|
||||
);
|
||||
let mut state = TextAreaState::default();
|
||||
StatefulWidgetRef::render_ref(&&self.search.editor, query_area, &mut buffer, &mut state);
|
||||
let (cursor_column, _) = self
|
||||
.search
|
||||
.editor
|
||||
.cursor_pos_with_state(query_area, state)?;
|
||||
let mut spans = Vec::new();
|
||||
let mut column = 0;
|
||||
while column < width {
|
||||
let cell = &buffer[(column, 0)];
|
||||
spans.push(Span::styled(cell.symbol().to_string(), cell.style()));
|
||||
column += cell.symbol().width().max(/*other*/ 1) as u16;
|
||||
}
|
||||
Some((Line::from(spans), cursor_column))
|
||||
pub(crate) fn is_search_editing(&self) -> bool {
|
||||
self.search.mode == SearchMode::Editing
|
||||
}
|
||||
|
||||
pub(super) fn handle_search_key(
|
||||
@@ -158,14 +140,27 @@ impl TranscriptView {
|
||||
key: KeyEvent,
|
||||
cells: &[Arc<dyn HistoryCell>],
|
||||
) -> bool {
|
||||
if !self.search.active {
|
||||
if !self.search.is_active() {
|
||||
return false;
|
||||
}
|
||||
let (code, modifiers) = crate::key_hint::normalize_key_parts(key.code, key.modifiers);
|
||||
match (code, modifiers) {
|
||||
(KeyCode::Esc, _) if self.search.is_reading() => self.jump_to_latest(),
|
||||
(KeyCode::Esc, _) if !self.search.editor.is_empty() => {
|
||||
self.search.mode = SearchMode::Reading;
|
||||
self.search.progress = if matches!(self.search.progress, Progress::Exhausted) {
|
||||
Progress::Exhausted
|
||||
} else if self.search.current.is_some() {
|
||||
Progress::Found
|
||||
} else {
|
||||
Progress::Idle
|
||||
};
|
||||
self.search.scanning_layout = None;
|
||||
}
|
||||
(KeyCode::Esc, _) | (KeyCode::Char('c'), KeyModifiers::CONTROL) => {
|
||||
self.cancel_search();
|
||||
}
|
||||
(KeyCode::Enter, _) if self.search.is_reading() => return false,
|
||||
// Ctrl+N/P stay distinct when a legacy terminal encodes Shift+Enter as Enter.
|
||||
(KeyCode::Enter, KeyModifiers::NONE) | (KeyCode::Char('n'), KeyModifiers::CONTROL) => {
|
||||
self.next_search_match(cells, Direction::Newer);
|
||||
@@ -173,6 +168,8 @@ impl TranscriptView {
|
||||
(KeyCode::Enter, KeyModifiers::SHIFT) | (KeyCode::Char('p'), KeyModifiers::CONTROL) => {
|
||||
self.next_search_match(cells, Direction::Older);
|
||||
}
|
||||
(KeyCode::PageUp | KeyCode::PageDown, KeyModifiers::NONE) => return false,
|
||||
_ if JumpTarget::from_key(key).is_some() || self.search.is_reading() => return false,
|
||||
_ => {
|
||||
let before = self.search.editor.text().to_string();
|
||||
let cursor = self.search.editor.cursor();
|
||||
@@ -190,7 +187,7 @@ impl TranscriptView {
|
||||
}
|
||||
}
|
||||
}
|
||||
// Find owns every key until it closes, including unbound keys and editor chords.
|
||||
// Query editing owns other keys; reading and page navigation use the transcript.
|
||||
true
|
||||
}
|
||||
|
||||
@@ -200,7 +197,7 @@ impl TranscriptView {
|
||||
cells: &[Arc<dyn HistoryCell>],
|
||||
retiring: std::ops::Range<usize>,
|
||||
) {
|
||||
if self.search.active
|
||||
if self.search.is_active()
|
||||
&& self.search.saved_snapshot.is_none()
|
||||
&& let Position::Reading(anchor) = self.search.saved_position
|
||||
&& cells[retiring]
|
||||
@@ -218,7 +215,7 @@ impl TranscriptView {
|
||||
|
||||
/// Restore the pre-search presentation before an explicit return or cancellation.
|
||||
pub(crate) fn cancel_search(&mut self) {
|
||||
if !self.search.active {
|
||||
if !self.search.is_active() {
|
||||
return;
|
||||
}
|
||||
let position = self.search.saved_position;
|
||||
@@ -240,7 +237,7 @@ impl TranscriptView {
|
||||
});
|
||||
let detailed = self.search.saved_detailed;
|
||||
self.set_presentation(detailed, self.mode);
|
||||
self.search.active = false;
|
||||
self.search.mode = SearchMode::Closed;
|
||||
self.search.editor.set_text_clearing_elements("");
|
||||
self.search.query_changed();
|
||||
self.search.progress = Progress::Idle;
|
||||
@@ -250,7 +247,7 @@ impl TranscriptView {
|
||||
}
|
||||
|
||||
pub(crate) fn paste_search(&mut self, text: &str) -> bool {
|
||||
if !self.search.active {
|
||||
if !self.is_search_editing() {
|
||||
return false;
|
||||
}
|
||||
let normalized = text.replace("\r\n", "\n").replace('\r', "\n");
|
||||
@@ -275,8 +272,12 @@ impl TranscriptView {
|
||||
|
||||
/// Restart after a presentation change invalidates the searched text and its source offsets.
|
||||
pub(crate) fn restart_search(&mut self) {
|
||||
if self.search.active {
|
||||
if self.search.is_active() {
|
||||
self.search.query_changed();
|
||||
// Reading should not jump when scrolling releases a retained live revision.
|
||||
if self.search.is_reading() {
|
||||
self.search.progress = Progress::Idle;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -302,6 +303,8 @@ impl TranscriptView {
|
||||
pub(super) fn invalidate_held_search(&mut self) {
|
||||
if self.search.has_active_query() {
|
||||
self.restart_search();
|
||||
// Scrolling away from a retained match must not immediately find it again.
|
||||
self.search.progress = Progress::Idle;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -311,8 +314,13 @@ impl TranscriptView {
|
||||
return;
|
||||
}
|
||||
self.rewrap_snapshot(width);
|
||||
if let Some(found) = &mut self.search.current {
|
||||
found.layout = Arc::new(found.layout.rewrap(width));
|
||||
}
|
||||
self.area.width = width;
|
||||
if self.held_reading.is_none() || !matches!(self.search.progress, Progress::Found) {
|
||||
if self.search.current.is_none()
|
||||
|| !matches!(self.search.progress, Progress::Found | Progress::Exhausted)
|
||||
{
|
||||
self.restart_search();
|
||||
}
|
||||
}
|
||||
@@ -368,9 +376,28 @@ impl TranscriptView {
|
||||
};
|
||||
self.position = Position::Reading(anchor);
|
||||
self.hold_live_reading(current_cells, Arc::clone(&layout));
|
||||
let end = window.start + range.end;
|
||||
let row = layout.row_for_offset(anchor.offset);
|
||||
let match_rows = layout.row_for_offset(end - 1) - row + 1;
|
||||
let context_rows = usize::from(self.area.height / 3)
|
||||
.min(usize::from(self.area.height).saturating_sub(match_rows));
|
||||
// Keep the hit's retained revision while placing its lead-in above it, even
|
||||
// in the preceding entry, without clipping a match that fits in the viewport.
|
||||
let (index, row) =
|
||||
self.move_rows(cells, anchor.index, row, -(context_rows as isize));
|
||||
if let Some(context) = self.layout(cells, index) {
|
||||
let offset = context.position_at(row, /*column*/ 0);
|
||||
self.position = Position::Reading(Anchor {
|
||||
key: self.entry_key(cells, index),
|
||||
index,
|
||||
offset,
|
||||
row_bias: context.row_for_offset(offset) as isize - row as isize,
|
||||
});
|
||||
}
|
||||
self.search.current = Some(Match {
|
||||
anchor,
|
||||
end: window.start + range.end,
|
||||
end,
|
||||
layout: Arc::clone(&layout),
|
||||
});
|
||||
self.search.progress = Progress::Found;
|
||||
self.search.scanning_layout = None;
|
||||
@@ -498,7 +525,8 @@ impl TranscriptView {
|
||||
.pinned
|
||||
.get(&anchor.key)
|
||||
.and_then(|layout| layout.text().get(..offset));
|
||||
let same_prefix = self.current_layout(cells, index).is_some_and(|layout| {
|
||||
let layout = self.current_layout(cells, index);
|
||||
let same_prefix = layout.as_ref().is_some_and(|layout| {
|
||||
prefix.is_some_and(|prefix| layout.text().starts_with(prefix))
|
||||
});
|
||||
anchor = Anchor {
|
||||
@@ -507,12 +535,13 @@ impl TranscriptView {
|
||||
offset: if same_prefix { offset } else { 0 },
|
||||
row_bias: 0,
|
||||
};
|
||||
self.search.current = same_prefix.then_some(Match {
|
||||
self.search.current = layout.filter(|_| same_prefix).map(|layout| Match {
|
||||
anchor: Anchor {
|
||||
offset: match_start,
|
||||
..anchor
|
||||
},
|
||||
end: match_end,
|
||||
layout,
|
||||
});
|
||||
self.visible.clear();
|
||||
self.position = Position::Reading(
|
||||
@@ -527,16 +556,21 @@ impl TranscriptView {
|
||||
}
|
||||
self.search.progress = Progress::Scanning(Cursor { anchor, direction });
|
||||
} else if matches!(self.search.progress, Progress::Idle | Progress::Exhausted) {
|
||||
self.start_search_scan(cells, Direction::Older);
|
||||
self.start_search_scan(cells, direction);
|
||||
}
|
||||
}
|
||||
|
||||
fn start_search_scan(&mut self, cells: &[Arc<dyn HistoryCell>], direction: Direction) {
|
||||
self.search.page_start = None;
|
||||
let count = cells.len() + usize::from(self.live.is_some());
|
||||
let index = match direction {
|
||||
Direction::Older => count.saturating_sub(/*rhs*/ 1),
|
||||
Direction::Newer => 0,
|
||||
let index = if self.search.is_reading() {
|
||||
// Without a retained match, resume from the visible entry, not a transcript end.
|
||||
self.start(cells).0
|
||||
} else {
|
||||
match direction {
|
||||
Direction::Older => count.saturating_sub(/*rhs*/ 1),
|
||||
Direction::Newer => 0,
|
||||
}
|
||||
};
|
||||
let offset = match direction {
|
||||
Direction::Older => usize::MAX,
|
||||
@@ -632,12 +666,23 @@ impl TranscriptView {
|
||||
}
|
||||
|
||||
impl Search {
|
||||
pub(super) fn match_layout(&self, key: EntryKey) -> Option<Arc<TextLayout>> {
|
||||
self.current
|
||||
.as_ref()
|
||||
.filter(|found| found.anchor.key == key)
|
||||
.map(|found| Arc::clone(&found.layout))
|
||||
}
|
||||
|
||||
pub(super) fn is_active(&self) -> bool {
|
||||
self.active
|
||||
self.mode != SearchMode::Closed
|
||||
}
|
||||
|
||||
pub(super) fn is_reading(&self) -> bool {
|
||||
self.mode == SearchMode::Reading
|
||||
}
|
||||
|
||||
pub(super) fn has_active_query(&self) -> bool {
|
||||
self.active && !self.editor.is_empty()
|
||||
self.is_active() && !self.editor.is_empty()
|
||||
}
|
||||
|
||||
pub(super) fn needs_history(&self, history: TranscriptHistoryState) -> bool {
|
||||
@@ -645,41 +690,13 @@ impl Search {
|
||||
&& history == TranscriptHistoryState::Partial
|
||||
}
|
||||
|
||||
pub(super) fn status_line(&self, width: u16, history: TranscriptHistoryState) -> Line<'static> {
|
||||
let previous = crate::key_hint::ctrl(crossterm::event::KeyCode::Char('p')).display_label();
|
||||
let retry_hint = format!("{previous} retry");
|
||||
let unavailable_hint = format!("History unavailable · {retry_hint}");
|
||||
let next_hint = format!("enter next · {previous} previous");
|
||||
let exhausted_hint = format!("No more matches · {next_hint}");
|
||||
let (status, compact) = match self.progress {
|
||||
Progress::Idle => ("Type to find", "Type to find"),
|
||||
Progress::Restart | Progress::Scanning(_) => ("Searching…", "Searching…"),
|
||||
Progress::AwaitingHistory if history == TranscriptHistoryState::Failed => {
|
||||
(unavailable_hint.as_str(), retry_hint.as_str())
|
||||
}
|
||||
Progress::AwaitingHistory => ("Searching earlier history…", "Loading…"),
|
||||
Progress::Found => (next_hint.as_str(), "enter next"),
|
||||
Progress::Exhausted if self.current.is_some() => {
|
||||
(exhausted_hint.as_str(), "enter next")
|
||||
}
|
||||
Progress::Exhausted => ("No matches", "No matches"),
|
||||
};
|
||||
let limit = if self.query_truncated {
|
||||
" · query limited to 4 KiB"
|
||||
} else {
|
||||
""
|
||||
};
|
||||
crate::footer_hint::first_fitting_line(
|
||||
[
|
||||
format!("{status} · full transcript · esc close{limit}"),
|
||||
format!("{status} · esc close{limit}"),
|
||||
format!("{compact} · esc close"),
|
||||
format!("{compact} · esc"),
|
||||
"esc close".to_owned(),
|
||||
]
|
||||
.map(|hint| super::footer::navigation_line(&hint)),
|
||||
width,
|
||||
)
|
||||
pub(super) fn allows_viewport_paging(&self) -> bool {
|
||||
!self.is_active()
|
||||
|| (self.has_active_query()
|
||||
&& matches!(
|
||||
self.progress,
|
||||
Progress::Idle | Progress::Found | Progress::Exhausted
|
||||
))
|
||||
}
|
||||
|
||||
fn query_changed(&mut self) {
|
||||
|
||||
@@ -0,0 +1,104 @@
|
||||
//! Render Find query input and navigation hints without owning search transitions.
|
||||
|
||||
use super::*;
|
||||
use crate::bottom_pane::TextAreaState;
|
||||
use ratatui::text::Span;
|
||||
use ratatui::widgets::StatefulWidgetRef;
|
||||
use unicode_width::UnicodeWidthStr;
|
||||
|
||||
impl TranscriptView {
|
||||
/// Project the editor and its caret from the same one-row viewport into the existing footer.
|
||||
pub(crate) fn search_footer(&self, width: u16) -> Option<(Line<'static>, u16)> {
|
||||
if !self.is_search_editing() || width == 0 {
|
||||
return None;
|
||||
}
|
||||
let area = Rect::new(/*x*/ 0, /*y*/ 0, width, /*height*/ 1);
|
||||
let mut buffer = Buffer::empty(area);
|
||||
let prefix_width = width.saturating_sub(/*rhs*/ 1).min(/*other*/ 6);
|
||||
Line::from("Find: ").dim().render(
|
||||
Rect::new(/*x*/ 0, /*y*/ 0, prefix_width, /*height*/ 1),
|
||||
&mut buffer,
|
||||
);
|
||||
let query_area = Rect::new(
|
||||
prefix_width,
|
||||
/*y*/ 0,
|
||||
width - prefix_width,
|
||||
/*height*/ 1,
|
||||
);
|
||||
let mut state = TextAreaState::default();
|
||||
StatefulWidgetRef::render_ref(&&self.search.editor, query_area, &mut buffer, &mut state);
|
||||
let (cursor_column, _) = self
|
||||
.search
|
||||
.editor
|
||||
.cursor_pos_with_state(query_area, state)?;
|
||||
let mut spans = Vec::new();
|
||||
let mut column = 0;
|
||||
while column < width {
|
||||
let cell = &buffer[(column, 0)];
|
||||
spans.push(Span::styled(cell.symbol().to_string(), cell.style()));
|
||||
column += cell.symbol().width().max(/*other*/ 1) as u16;
|
||||
}
|
||||
Some((Line::from(spans), cursor_column))
|
||||
}
|
||||
}
|
||||
|
||||
impl Search {
|
||||
pub(in crate::transcript_view) fn status_line(
|
||||
&self,
|
||||
width: u16,
|
||||
history: TranscriptHistoryState,
|
||||
) -> Line<'static> {
|
||||
let previous = crate::key_hint::ctrl(crossterm::event::KeyCode::Char('p')).display_label();
|
||||
let retry_hint = format!("{previous} retry");
|
||||
let unavailable_hint = format!("History unavailable · {retry_hint}");
|
||||
let next_hint = format!("enter next · {previous} previous");
|
||||
let exhausted_hint = format!("No more matches · {next_hint}");
|
||||
let (status, compact) = match self.progress {
|
||||
Progress::Idle | Progress::Found if self.is_reading() => {
|
||||
("ctrl+n next · ctrl+p previous", "ctrl+n next")
|
||||
}
|
||||
Progress::Idle => ("Type to find", "Type to find"),
|
||||
Progress::Restart | Progress::Scanning(_) => ("Searching…", "Searching…"),
|
||||
Progress::AwaitingHistory if history == TranscriptHistoryState::Failed => {
|
||||
(unavailable_hint.as_str(), retry_hint.as_str())
|
||||
}
|
||||
Progress::AwaitingHistory => ("Searching earlier history…", "Loading…"),
|
||||
Progress::Found => (next_hint.as_str(), "enter next"),
|
||||
Progress::Exhausted if self.is_reading() && self.current.is_some() => (
|
||||
"No more matches · ctrl+n next · ctrl+p previous",
|
||||
"ctrl+n next",
|
||||
),
|
||||
Progress::Exhausted if self.current.is_some() => {
|
||||
(exhausted_hint.as_str(), "enter next")
|
||||
}
|
||||
Progress::Exhausted => ("No matches", "No matches"),
|
||||
};
|
||||
let limit = if self.query_truncated {
|
||||
" · query limited to 4 KiB"
|
||||
} else {
|
||||
""
|
||||
};
|
||||
if self.is_reading() {
|
||||
return crate::footer_hint::first_fitting_line(
|
||||
[
|
||||
format!("{status} · esc latest"),
|
||||
format!("{compact} · esc latest"),
|
||||
"esc latest".to_owned(),
|
||||
]
|
||||
.map(|hint| crate::transcript_view::footer::navigation_line(&hint)),
|
||||
width,
|
||||
);
|
||||
}
|
||||
crate::footer_hint::first_fitting_line(
|
||||
[
|
||||
format!("{status} · full transcript · esc close{limit}"),
|
||||
format!("{status} · esc close{limit}"),
|
||||
format!("{compact} · esc close"),
|
||||
format!("{compact} · esc"),
|
||||
"esc close".to_owned(),
|
||||
]
|
||||
.map(|hint| crate::transcript_view::footer::navigation_line(&hint)),
|
||||
width,
|
||||
)
|
||||
}
|
||||
}
|
||||
@@ -30,6 +30,99 @@ fn finish_scan(view: &mut TranscriptView, cells: &[Arc<dyn HistoryCell>]) {
|
||||
panic!("search did not yield after bounded scanning frames");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn find_keeps_context_and_allows_reading_without_losing_the_query() {
|
||||
let cells = vec![
|
||||
cell("earlier needle"),
|
||||
cell("explanation before the result"),
|
||||
cell("needle in the result"),
|
||||
cell("explanation after the result"),
|
||||
cell("latest message"),
|
||||
];
|
||||
let area = Rect::new(
|
||||
/*x*/ 0, /*y*/ 0, /*width*/ 40, /*height*/ 6,
|
||||
);
|
||||
let mut view = TranscriptView::default();
|
||||
view.render(area, &mut Buffer::empty(area), &cells);
|
||||
view.begin_search();
|
||||
view.paste_search("needle");
|
||||
finish_scan(&mut view, &cells);
|
||||
let mut found = Buffer::empty(area);
|
||||
view.render(area, &mut found, &cells);
|
||||
insta::assert_snapshot!("find_context", format!("{found:?}"));
|
||||
|
||||
let matched = view.position;
|
||||
view.handle_key(KeyCode::PageDown.into(), &cells);
|
||||
assert_eq!(
|
||||
(view.is_following(), view.search.editor.text()),
|
||||
(true, "needle")
|
||||
);
|
||||
view.handle_key(KeyCode::PageUp.into(), &cells);
|
||||
finish_scan(&mut view, &cells);
|
||||
let scrolled = view.position;
|
||||
assert_ne!(scrolled, matched);
|
||||
assert_eq!(view.search.editor.text(), "needle");
|
||||
view.handle_key(KeyCode::Esc.into(), &cells);
|
||||
insta::assert_snapshot!(
|
||||
view.footer(/*width*/ 80, crate::motion::MotionMode::Reduced).unwrap().text.to_string(),
|
||||
@"ctrl+n next · ctrl+p previous · esc latest"
|
||||
);
|
||||
assert_eq!(
|
||||
(
|
||||
view.position,
|
||||
view.is_search_editing(),
|
||||
view.has_active_interaction()
|
||||
),
|
||||
(scrolled, false, true),
|
||||
);
|
||||
assert!(view.handle_key(KeyCode::Enter.into(), &cells).is_none());
|
||||
view.begin_search();
|
||||
assert_eq!(
|
||||
(view.position, view.search.editor.text()),
|
||||
(scrolled, "needle")
|
||||
);
|
||||
view.handle_key(KeyCode::Esc.into(), &cells);
|
||||
view.handle_key(
|
||||
KeyEvent::new(KeyCode::Char('p'), KeyModifiers::CONTROL),
|
||||
&cells,
|
||||
);
|
||||
finish_scan(&mut view, &cells);
|
||||
assert_eq!(view.search.current.as_ref().unwrap().anchor.index, 0);
|
||||
view.handle_key(KeyCode::Esc.into(), &cells);
|
||||
assert_eq!(
|
||||
(
|
||||
view.is_following(),
|
||||
view.is_detailed(),
|
||||
view.has_active_interaction()
|
||||
),
|
||||
(true, false, false),
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn find_reduces_context_to_keep_a_tall_match_visible() {
|
||||
let cells = vec![
|
||||
cell("earlier"),
|
||||
cell("one two three four five"),
|
||||
cell("later"),
|
||||
];
|
||||
let mut frames = Vec::new();
|
||||
for height in [6, 5, 3] {
|
||||
let area = Rect::new(/*x*/ 0, /*y*/ 0, /*width*/ 6, height);
|
||||
let mut view = TranscriptView {
|
||||
area,
|
||||
..TranscriptView::default()
|
||||
};
|
||||
view.begin_search();
|
||||
view.paste_search("one two three four five");
|
||||
finish_scan(&mut view, &cells);
|
||||
let mut buffer = Buffer::empty(area);
|
||||
view.render(area, &mut buffer, &cells);
|
||||
frames.push(format!("height {height}\n{buffer:?}"));
|
||||
}
|
||||
insta::assert_snapshot!("find_context_for_tall_matches", frames.join("\n"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn selection_pauses_find_and_normalized_navigation_resumes_the_same_query() {
|
||||
let cells = vec![cell("older needle"), cell("newer needle")];
|
||||
@@ -50,7 +143,7 @@ fn selection_pauses_find_and_normalized_navigation_resumes_the_same_query() {
|
||||
KeyEvent::new(KeyCode::Char('p'), KeyModifiers::CONTROL),
|
||||
&cells,
|
||||
);
|
||||
view.begin_selection(&cells, /*column*/ 0, /*row*/ 0, /*clicks*/ 3);
|
||||
view.begin_selection(&cells, /*column*/ 0, /*row*/ 1, /*clicks*/ 3);
|
||||
let selected = view.selected_text(&cells);
|
||||
assert!(!view.advance_search(&cells));
|
||||
assert_eq!(view.selected_text(&cells), selected);
|
||||
@@ -71,13 +164,202 @@ fn selection_pauses_find_and_normalized_navigation_resumes_the_same_query() {
|
||||
KeyEvent::new(KeyCode::Char('c'), KeyModifiers::CONTROL),
|
||||
&cells,
|
||||
);
|
||||
assert!(!view.is_search_active());
|
||||
assert!(!view.is_search_editing());
|
||||
insta::assert_snapshot!(
|
||||
"selection_owns_find_highlight",
|
||||
format!("{selected_frame:?}")
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn paging_away_from_a_live_match_does_not_restart_find() {
|
||||
let cells = vec![cell("earlier context")];
|
||||
let area = Rect::new(
|
||||
/*x*/ 0, /*y*/ 0, /*width*/ 30, /*height*/ 3,
|
||||
);
|
||||
let mut view = TranscriptView {
|
||||
area,
|
||||
..TranscriptView::default()
|
||||
};
|
||||
view.sync_live_tail(
|
||||
/*width*/ 30,
|
||||
/*key*/ None,
|
||||
|_| Some(vec![HyperlinkLine::from("live needle")]),
|
||||
);
|
||||
view.begin_search();
|
||||
view.paste_search("needle");
|
||||
finish_scan(&mut view, &cells);
|
||||
assert!(view.held_reading.is_some());
|
||||
view.handle_key(KeyCode::PageUp.into(), &cells);
|
||||
let scrolled = view.position;
|
||||
assert!(view.held_reading.is_none());
|
||||
finish_scan(&mut view, &cells);
|
||||
view.handle_key(KeyCode::Esc.into(), &cells);
|
||||
assert_eq!(
|
||||
(
|
||||
view.position,
|
||||
view.search.editor.text(),
|
||||
view.search.is_reading()
|
||||
),
|
||||
(scrolled, "needle", true),
|
||||
);
|
||||
view.render(area, &mut Buffer::empty(area), &cells);
|
||||
view.begin_selection(&cells, /*column*/ 0, /*row*/ 0, /*clicks*/ 2);
|
||||
assert_eq!(view.selected_text(&cells).as_deref(), Some("earlier"));
|
||||
view.begin_search();
|
||||
assert!(view.selection.is_none());
|
||||
finish_scan(&mut view, &cells);
|
||||
assert_eq!(
|
||||
view.search.current.as_ref().unwrap().anchor.key,
|
||||
EntryKey::Live
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn reading_without_a_match_searches_from_the_visible_entry_in_each_direction() {
|
||||
let cells = vec![cell("older needle"), cell("context"), cell("newer needle")];
|
||||
for (key, expected) in [('p', 0), ('n', 2)] {
|
||||
let mut view = TranscriptView {
|
||||
area: Rect::new(
|
||||
/*x*/ 0, /*y*/ 0, /*width*/ 30, /*height*/ 1,
|
||||
),
|
||||
position: Position::Reading(Anchor {
|
||||
key: EntryKey::cell(&cells[1]),
|
||||
index: 1,
|
||||
offset: 0,
|
||||
row_bias: 0,
|
||||
}),
|
||||
..TranscriptView::default()
|
||||
};
|
||||
view.begin_search();
|
||||
view.paste_search("needle");
|
||||
// Close the query before its initial scan has found a match.
|
||||
view.handle_key(KeyCode::Esc.into(), &cells);
|
||||
view.handle_key(
|
||||
KeyEvent::new(KeyCode::Char(key), KeyModifiers::CONTROL),
|
||||
&cells,
|
||||
);
|
||||
finish_scan(&mut view, &cells);
|
||||
assert_eq!(
|
||||
view.search.current.as_ref().map(|found| found.anchor.key),
|
||||
Some(EntryKey::cell(&cells[expected]))
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn resizing_a_read_find_result_preserves_relative_navigation() {
|
||||
let cells = vec![
|
||||
cell("older explanation with needle in a wrapped result"),
|
||||
cell("newer explanation with needle in a wrapped result"),
|
||||
];
|
||||
let mut view = TranscriptView {
|
||||
area: Rect::new(
|
||||
/*x*/ 0, /*y*/ 0, /*width*/ 30, /*height*/ 3,
|
||||
),
|
||||
..TranscriptView::default()
|
||||
};
|
||||
view.begin_search();
|
||||
view.paste_search("needle");
|
||||
finish_scan(&mut view, &cells);
|
||||
view.handle_key(KeyCode::Esc.into(), &cells);
|
||||
view.prepare_width(/*width*/ 20);
|
||||
let mut resized = Buffer::empty(view.area);
|
||||
view.render(view.area, &mut resized, &cells);
|
||||
insta::assert_snapshot!("find_context_after_resize", format!("{resized:?}"));
|
||||
assert_eq!(
|
||||
view.search.current.as_ref().map(|found| found.anchor.index),
|
||||
Some(1)
|
||||
);
|
||||
for (key, expected) in [('p', 0), ('n', 1)] {
|
||||
view.handle_key(
|
||||
KeyEvent::new(KeyCode::Char(key), KeyModifiers::CONTROL),
|
||||
&cells,
|
||||
);
|
||||
finish_scan(&mut view, &cells);
|
||||
assert_eq!(
|
||||
view.search.current.as_ref().map(|found| found.anchor.index),
|
||||
Some(expected)
|
||||
);
|
||||
}
|
||||
view.handle_key(
|
||||
KeyEvent::new(KeyCode::Char('n'), KeyModifiers::CONTROL),
|
||||
&cells,
|
||||
);
|
||||
finish_scan(&mut view, &cells);
|
||||
view.prepare_width(/*width*/ 25);
|
||||
view.handle_key(
|
||||
KeyEvent::new(KeyCode::Char('p'), KeyModifiers::CONTROL),
|
||||
&cells,
|
||||
);
|
||||
finish_scan(&mut view, &cells);
|
||||
assert_eq!(
|
||||
view.search.current.as_ref().map(|found| found.anchor.index),
|
||||
Some(0)
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn reading_a_find_result_loads_older_context() {
|
||||
let cells = vec![cell("needle"), cell("latest context")];
|
||||
let mut view = TranscriptView {
|
||||
area: Rect::new(
|
||||
/*x*/ 0, /*y*/ 0, /*width*/ 30, /*height*/ 3,
|
||||
),
|
||||
history: TranscriptHistoryState::Partial,
|
||||
..TranscriptView::default()
|
||||
};
|
||||
view.begin_search();
|
||||
view.paste_search("needle");
|
||||
finish_scan(&mut view, &cells);
|
||||
view.handle_key(KeyCode::PageUp.into(), &cells);
|
||||
assert!(view.needs_history(&cells));
|
||||
view.handle_key(KeyCode::Esc.into(), &cells);
|
||||
view.handle_key(KeyCode::PageUp.into(), &cells);
|
||||
assert!(view.needs_history(&cells));
|
||||
assert_eq!(view.search.editor.text(), "needle");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn paging_below_a_live_find_result_resumes_current_output() {
|
||||
let cells = vec![cell("earlier context")];
|
||||
let area = Rect::new(
|
||||
/*x*/ 0, /*y*/ 0, /*width*/ 30, /*height*/ 3,
|
||||
);
|
||||
let mut view = TranscriptView {
|
||||
area,
|
||||
..TranscriptView::default()
|
||||
};
|
||||
view.sync_live_tail(
|
||||
/*width*/ 30,
|
||||
/*key*/ None,
|
||||
|_| Some(vec![HyperlinkLine::from("live needle")]),
|
||||
);
|
||||
view.begin_search();
|
||||
view.paste_search("needle");
|
||||
finish_scan(&mut view, &cells);
|
||||
view.handle_key(KeyCode::PageDown.into(), &cells);
|
||||
view.sync_live_tail(/*width*/ 30, /*key*/ None, |_| {
|
||||
Some(vec![HyperlinkLine::from("updated live output")])
|
||||
});
|
||||
finish_scan(&mut view, &cells);
|
||||
let mut buffer = Buffer::empty(area);
|
||||
view.render(area, &mut buffer, &cells);
|
||||
assert_eq!(
|
||||
(
|
||||
view.is_following(),
|
||||
view.held_reading.is_none(),
|
||||
view.search.editor.text()
|
||||
),
|
||||
(true, true, "needle"),
|
||||
);
|
||||
insta::assert_snapshot!(crate::transcript_view::tests::text(&buffer), @"
|
||||
earlier context
|
||||
|
||||
updated live output
|
||||
");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn literal_search_preserves_unicode_source_offsets() {
|
||||
let text = "界 CAFÉ [x].* İ";
|
||||
@@ -241,7 +523,7 @@ fn empty_find_keeps_following_new_output_after_a_page_join() {
|
||||
assert_eq!(
|
||||
(
|
||||
view.is_following(),
|
||||
view.is_search_active(),
|
||||
view.is_search_editing(),
|
||||
view.held_reading.is_none()
|
||||
),
|
||||
(true, true, true),
|
||||
@@ -568,7 +850,7 @@ fn next_previous_highlight_and_cancel_share_the_transcript_position() {
|
||||
view.render(area, &mut buffer, &cells);
|
||||
assert_eq!(
|
||||
(0..10)
|
||||
.map(|column| buffer[(column, 0)].modifier.contains(Modifier::REVERSED))
|
||||
.map(|column| buffer[(column, 1)].modifier.contains(Modifier::REVERSED))
|
||||
.collect::<Vec<_>>(),
|
||||
vec![
|
||||
true, true, true, true, true, true, false, false, false, false
|
||||
@@ -577,7 +859,10 @@ fn next_previous_highlight_and_cancel_share_the_transcript_position() {
|
||||
insta::assert_snapshot!(view.search.status_line(/*width*/ 80, view.history).to_string(), @"enter next · ⌃p previous · full transcript · esc close");
|
||||
view.handle_search_key(KeyEvent::new(KeyCode::Left, KeyModifiers::NONE), &cells);
|
||||
assert!(matches!(view.search.progress, Progress::Found));
|
||||
view.handle_search_key(KeyEvent::new(KeyCode::Esc, KeyModifiers::NONE), &cells);
|
||||
view.handle_search_key(
|
||||
KeyEvent::new(KeyCode::Char('c'), KeyModifiers::CONTROL),
|
||||
&cells,
|
||||
);
|
||||
assert_eq!((view.position, view.search.is_active()), (original, false));
|
||||
}
|
||||
|
||||
@@ -666,6 +951,14 @@ fn find_reveals_hidden_command_output_and_restores_compact_presentation() {
|
||||
&cells,
|
||||
);
|
||||
assert!(view.is_detailed());
|
||||
let mut found = Buffer::empty(view.area);
|
||||
view.render(view.area, &mut found, &cells);
|
||||
view.handle_search_key(KeyEvent::new(KeyCode::Esc, KeyModifiers::NONE), &cells);
|
||||
let mut reading = Buffer::empty(view.area);
|
||||
view.render(view.area, &mut reading, &cells);
|
||||
assert_eq!(reading, found);
|
||||
assert!(!view.is_search_editing());
|
||||
assert!(view.is_detailed());
|
||||
view.handle_search_key(KeyEvent::new(KeyCode::Esc, KeyModifiers::NONE), &cells);
|
||||
assert_eq!(
|
||||
(view.is_detailed(), view.position),
|
||||
@@ -715,7 +1008,10 @@ fn search_preserves_both_presentation_positions() {
|
||||
view.paste_search("needle");
|
||||
finish_scan(&mut view, &cells);
|
||||
assert_eq!(view.search.current.as_ref().unwrap().anchor.index, 0);
|
||||
view.handle_search_key(KeyEvent::new(KeyCode::Esc, KeyModifiers::NONE), &cells);
|
||||
view.handle_search_key(
|
||||
KeyEvent::new(KeyCode::Char('c'), KeyModifiers::CONTROL),
|
||||
&cells,
|
||||
);
|
||||
assert_eq!((view.position, view.saved_position), positions);
|
||||
view.render(area, &mut Buffer::empty(area), &cells);
|
||||
|
||||
@@ -957,8 +1253,8 @@ fn leaving_a_regrouped_search_match_restarts_from_current_history() {
|
||||
);
|
||||
let mut buffer = Buffer::empty(area);
|
||||
view.render(area, &mut buffer, &cells);
|
||||
assert_eq!(view.visible[0].key, EntryKey::cell(&replacement));
|
||||
assert!(buffer[(7, 0)].modifier.contains(Modifier::REVERSED));
|
||||
assert_eq!(view.visible[1].key, EntryKey::cell(&replacement));
|
||||
assert!(buffer[(7, 1)].modifier.contains(Modifier::REVERSED));
|
||||
}
|
||||
|
||||
#[test]
|
||||
@@ -1005,7 +1301,10 @@ fn refining_a_query_restores_the_reading_position_while_history_loads() {
|
||||
let mut loading = Buffer::empty(area);
|
||||
view.render(area, &mut loading, &cells);
|
||||
assert_eq!(loading, before);
|
||||
view.handle_search_key(KeyEvent::new(KeyCode::Esc, KeyModifiers::NONE), &cells);
|
||||
view.handle_search_key(
|
||||
KeyEvent::new(KeyCode::Char('c'), KeyModifiers::CONTROL),
|
||||
&cells,
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1080,7 +1379,7 @@ fn selection_keys_precede_find_and_typing_returns_to_query() {
|
||||
),
|
||||
Some(ViewAction::Copy(_))
|
||||
));
|
||||
assert!(view.is_search_active());
|
||||
assert!(view.is_search_editing());
|
||||
view.handle_key(
|
||||
KeyEvent::new(KeyCode::Char('x'), KeyModifiers::NONE),
|
||||
&cells,
|
||||
|
||||
@@ -125,7 +125,7 @@ fn copy_shortcuts_clear_selection_only_after_confirmed_delivery() {
|
||||
);
|
||||
assert_eq!(copied, Ok(status));
|
||||
assert_eq!(
|
||||
(view.selected_text(&cells), view.is_search_active()),
|
||||
(view.selected_text(&cells), view.is_search_editing()),
|
||||
(
|
||||
(status == CopyStatus::Unconfirmed).then(|| text.clone()),
|
||||
searching
|
||||
|
||||
+20
@@ -0,0 +1,20 @@
|
||||
---
|
||||
source: tui/src/transcript_view/search_tests.rs
|
||||
expression: "format!(\"{found:?}\")"
|
||||
---
|
||||
Buffer {
|
||||
area: Rect { x: 0, y: 0, width: 40, height: 6 },
|
||||
content: [
|
||||
"explanation before the result ",
|
||||
" ",
|
||||
"needle in the result ",
|
||||
" ",
|
||||
"explanation after the result ",
|
||||
" ",
|
||||
],
|
||||
styles: [
|
||||
x: 0, y: 0, fg: Reset, bg: Reset, underline: Reset, modifier: NONE,
|
||||
x: 0, y: 2, fg: Reset, bg: Reset, underline: Reset, modifier: REVERSED,
|
||||
x: 6, y: 2, fg: Reset, bg: Reset, underline: Reset, modifier: NONE,
|
||||
]
|
||||
}
|
||||
+17
@@ -0,0 +1,17 @@
|
||||
---
|
||||
source: tui/src/transcript_view/search_tests.rs
|
||||
expression: "format!(\"{resized:?}\")"
|
||||
---
|
||||
Buffer {
|
||||
area: Rect { x: 0, y: 0, width: 20, height: 3 },
|
||||
content: [
|
||||
" ",
|
||||
"newer explanation ",
|
||||
"with needle in a ",
|
||||
],
|
||||
styles: [
|
||||
x: 0, y: 0, fg: Reset, bg: Reset, underline: Reset, modifier: NONE,
|
||||
x: 5, y: 2, fg: Reset, bg: Reset, underline: Reset, modifier: REVERSED,
|
||||
x: 11, y: 2, fg: Reset, bg: Reset, underline: Reset, modifier: NONE,
|
||||
]
|
||||
}
|
||||
+69
@@ -0,0 +1,69 @@
|
||||
---
|
||||
source: tui/src/transcript_view/search_tests.rs
|
||||
expression: "frames.join(\"\\n\")"
|
||||
---
|
||||
height 6
|
||||
Buffer {
|
||||
area: Rect { x: 0, y: 0, width: 6, height: 6 },
|
||||
content: [
|
||||
" ",
|
||||
"one ",
|
||||
"two ",
|
||||
"three ",
|
||||
"four ",
|
||||
"five ",
|
||||
],
|
||||
styles: [
|
||||
x: 0, y: 0, fg: Reset, bg: Reset, underline: Reset, modifier: NONE,
|
||||
x: 0, y: 1, fg: Reset, bg: Reset, underline: Reset, modifier: REVERSED,
|
||||
x: 3, y: 1, fg: Reset, bg: Reset, underline: Reset, modifier: NONE,
|
||||
x: 0, y: 2, fg: Reset, bg: Reset, underline: Reset, modifier: REVERSED,
|
||||
x: 3, y: 2, fg: Reset, bg: Reset, underline: Reset, modifier: NONE,
|
||||
x: 0, y: 3, fg: Reset, bg: Reset, underline: Reset, modifier: REVERSED,
|
||||
x: 5, y: 3, fg: Reset, bg: Reset, underline: Reset, modifier: NONE,
|
||||
x: 0, y: 4, fg: Reset, bg: Reset, underline: Reset, modifier: REVERSED,
|
||||
x: 4, y: 4, fg: Reset, bg: Reset, underline: Reset, modifier: NONE,
|
||||
x: 0, y: 5, fg: Reset, bg: Reset, underline: Reset, modifier: REVERSED,
|
||||
x: 4, y: 5, fg: Reset, bg: Reset, underline: Reset, modifier: NONE,
|
||||
]
|
||||
}
|
||||
height 5
|
||||
Buffer {
|
||||
area: Rect { x: 0, y: 0, width: 6, height: 5 },
|
||||
content: [
|
||||
"one ",
|
||||
"two ",
|
||||
"three ",
|
||||
"four ",
|
||||
"five ",
|
||||
],
|
||||
styles: [
|
||||
x: 0, y: 0, fg: Reset, bg: Reset, underline: Reset, modifier: REVERSED,
|
||||
x: 3, y: 0, fg: Reset, bg: Reset, underline: Reset, modifier: NONE,
|
||||
x: 0, y: 1, fg: Reset, bg: Reset, underline: Reset, modifier: REVERSED,
|
||||
x: 3, y: 1, fg: Reset, bg: Reset, underline: Reset, modifier: NONE,
|
||||
x: 0, y: 2, fg: Reset, bg: Reset, underline: Reset, modifier: REVERSED,
|
||||
x: 5, y: 2, fg: Reset, bg: Reset, underline: Reset, modifier: NONE,
|
||||
x: 0, y: 3, fg: Reset, bg: Reset, underline: Reset, modifier: REVERSED,
|
||||
x: 4, y: 3, fg: Reset, bg: Reset, underline: Reset, modifier: NONE,
|
||||
x: 0, y: 4, fg: Reset, bg: Reset, underline: Reset, modifier: REVERSED,
|
||||
x: 4, y: 4, fg: Reset, bg: Reset, underline: Reset, modifier: NONE,
|
||||
]
|
||||
}
|
||||
height 3
|
||||
Buffer {
|
||||
area: Rect { x: 0, y: 0, width: 6, height: 3 },
|
||||
content: [
|
||||
"one ",
|
||||
"two ",
|
||||
"three ",
|
||||
],
|
||||
styles: [
|
||||
x: 0, y: 0, fg: Reset, bg: Reset, underline: Reset, modifier: REVERSED,
|
||||
x: 3, y: 0, fg: Reset, bg: Reset, underline: Reset, modifier: NONE,
|
||||
x: 0, y: 1, fg: Reset, bg: Reset, underline: Reset, modifier: REVERSED,
|
||||
x: 3, y: 1, fg: Reset, bg: Reset, underline: Reset, modifier: NONE,
|
||||
x: 0, y: 2, fg: Reset, bg: Reset, underline: Reset, modifier: REVERSED,
|
||||
x: 5, y: 2, fg: Reset, bg: Reset, underline: Reset, modifier: NONE,
|
||||
]
|
||||
}
|
||||
+4
-4
@@ -1,18 +1,18 @@
|
||||
---
|
||||
source: tui/src/transcript_view/search_tests.rs
|
||||
assertion_line: 63
|
||||
expression: "format!(\"{selected_frame:?}\")"
|
||||
---
|
||||
Buffer {
|
||||
area: Rect { x: 0, y: 0, width: 32, height: 4 },
|
||||
content: [
|
||||
"newer needle ",
|
||||
" ",
|
||||
"newer needle ",
|
||||
" ",
|
||||
" ",
|
||||
],
|
||||
styles: [
|
||||
x: 0, y: 0, fg: Reset, bg: Reset, underline: Reset, modifier: REVERSED,
|
||||
x: 12, y: 0, fg: Reset, bg: Reset, underline: Reset, modifier: NONE,
|
||||
x: 0, y: 0, fg: Reset, bg: Reset, underline: Reset, modifier: NONE,
|
||||
x: 0, y: 1, fg: Reset, bg: Reset, underline: Reset, modifier: REVERSED,
|
||||
x: 12, y: 1, fg: Reset, bg: Reset, underline: Reset, modifier: NONE,
|
||||
]
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user