Extract history pager state

This commit is contained in:
Johannes Altmanninger 2024-09-28 11:03:21 +02:00
parent 263f1b35de
commit ae7fd770ee

View file

@ -499,13 +499,8 @@ pub struct ReaderData {
history: Arc<History>, history: Arc<History>,
/// The history search. /// The history search.
history_search: ReaderHistorySearch, history_search: ReaderHistorySearch,
/// Whether the in-pager history search is active. /// In-pager history search.
history_pager_active: bool, history_pager: Option<HistoryPager>,
/// The direction of the last successful history pager search.
history_pager_direction: SearchDirection,
/// The range in history covered by the history pager's current page.
history_pager_history_index_start: usize,
history_pager_history_index_end: usize,
/// The cursor selection mode. /// The cursor selection mode.
cursor_selection_mode: CursorSelectionMode, cursor_selection_mode: CursorSelectionMode,
@ -1129,10 +1124,7 @@ impl ReaderData {
queued_repaint: false, queued_repaint: false,
history, history,
history_search: Default::default(), history_search: Default::default(),
history_pager_active: Default::default(), history_pager: None,
history_pager_direction: SearchDirection::Forward,
history_pager_history_index_start: usize::MAX,
history_pager_history_index_end: usize::MAX,
cursor_selection_mode: CursorSelectionMode::Exclusive, cursor_selection_mode: CursorSelectionMode::Exclusive,
cursor_end_mode: CursorEndMode::Exclusive, cursor_end_mode: CursorEndMode::Exclusive,
selection: Default::default(), selection: Default::default(),
@ -1162,7 +1154,7 @@ impl ReaderData {
} }
fn is_navigating_pager_contents(&self) -> bool { fn is_navigating_pager_contents(&self) -> bool {
self.pager.is_navigating_contents() || self.history_pager_active self.pager.is_navigating_contents() || self.history_pager.is_some()
} }
fn edit_line(&self, elt: EditableLineTag) -> &EditableLine { fn edit_line(&self, elt: EditableLineTag) -> &EditableLine {
@ -1214,7 +1206,7 @@ impl ReaderData {
GENERATION.fetch_add(1, Ordering::Relaxed); GENERATION.fetch_add(1, Ordering::Relaxed);
} }
EditableLineTag::SearchField => { EditableLineTag::SearchField => {
if self.history_pager_active { if self.history_pager.is_some() {
self.fill_history_pager( self.fill_history_pager(
HistoryPagerInvocation::Anew, HistoryPagerInvocation::Anew,
SearchDirection::Backward, SearchDirection::Backward,
@ -2370,7 +2362,7 @@ impl<'a> Reader<'a> {
} }
} }
rl::PagerToggleSearch => { rl::PagerToggleSearch => {
if self.history_pager_active { if self.history_pager.is_some() {
self.fill_history_pager( self.fill_history_pager(
HistoryPagerInvocation::Advance, HistoryPagerInvocation::Advance,
SearchDirection::Forward, SearchDirection::Forward,
@ -2632,7 +2624,7 @@ impl<'a> Reader<'a> {
} }
} }
rl::HistoryPager => { rl::HistoryPager => {
if self.history_pager_active { if self.history_pager.is_some() {
self.fill_history_pager( self.fill_history_pager(
HistoryPagerInvocation::Advance, HistoryPagerInvocation::Advance,
SearchDirection::Backward, SearchDirection::Backward,
@ -2644,9 +2636,11 @@ impl<'a> Reader<'a> {
self.cycle_command_line = self.command_line.text().to_owned(); self.cycle_command_line = self.command_line.text().to_owned();
self.cycle_cursor_pos = self.command_line.position(); self.cycle_cursor_pos = self.command_line.position();
self.history_pager_active = true; self.history_pager = Some(HistoryPager {
self.history_pager_history_index_start = 0; direction: SearchDirection::Backward,
self.history_pager_history_index_end = 0; history_index_start: 0,
history_index_end: 0,
});
// Update the pager data. // Update the pager data.
self.pager.set_search_field_shown(true); self.pager.set_search_field_shown(true);
self.pager.set_prefix( self.pager.set_prefix(
@ -2698,7 +2692,7 @@ impl<'a> Reader<'a> {
self.input_data.function_set_status(true); self.input_data.function_set_status(true);
return; return;
} }
if !self.history_pager_active { if self.history_pager.is_none() {
self.input_data.function_set_status(false); self.input_data.function_set_status(false);
return; return;
} }
@ -3348,7 +3342,7 @@ impl<'a> Reader<'a> {
// using a backslash, insert a newline. // using a backslash, insert a newline.
// If the user hits return while navigating the pager, it only clears the pager. // If the user hits return while navigating the pager, it only clears the pager.
if self.is_navigating_pager_contents() { if self.is_navigating_pager_contents() {
if self.history_pager_active && self.pager.selected_completion_idx.is_none() { if self.history_pager.is_some() && self.pager.selected_completion_idx.is_none() {
self.data.command_line.push_edit( self.data.command_line.push_edit(
Edit::new( Edit::new(
0..self.data.command_line.len(), 0..self.data.command_line.len(),
@ -3456,7 +3450,7 @@ impl ReaderData {
// Ensure we have no pager contents. // Ensure we have no pager contents.
fn clear_pager(&mut self) { fn clear_pager(&mut self) {
self.pager.clear(); self.pager.clear();
self.history_pager_active = false; self.history_pager = None;
self.command_line_has_transient_edit = false; self.command_line_has_transient_edit = false;
} }
@ -4503,12 +4497,20 @@ struct HistoryPagerResult {
} }
#[derive(Eq, PartialEq)] #[derive(Eq, PartialEq)]
pub enum HistoryPagerInvocation { enum HistoryPagerInvocation {
Anew, Anew,
Advance, Advance,
Refresh, Refresh,
} }
struct HistoryPager {
/// The direction of the last successful history pager search.
direction: SearchDirection,
/// The range in history covered by the history pager's current page.
history_index_start: usize,
history_index_end: usize,
}
fn history_pager_search( fn history_pager_search(
history: &Arc<History>, history: &Arc<History>,
direction: SearchDirection, direction: SearchDirection,
@ -4580,15 +4582,17 @@ impl ReaderData {
index = 0; index = 0;
} }
HistoryPagerInvocation::Advance => { HistoryPagerInvocation::Advance => {
let history_pager = self.history_pager.as_ref().unwrap();
index = match direction { index = match direction {
SearchDirection::Forward => self.history_pager_history_index_start, SearchDirection::Forward => history_pager.history_index_start,
SearchDirection::Backward => self.history_pager_history_index_end, SearchDirection::Backward => history_pager.history_index_end,
} }
} }
HistoryPagerInvocation::Refresh => { HistoryPagerInvocation::Refresh => {
// Redo the previous search previous direction. // Redo the previous search previous direction.
direction = self.history_pager_direction; let history_pager = self.history_pager.as_ref().unwrap();
index = self.history_pager_history_index_start; direction = history_pager.direction;
index = history_pager.history_index_start;
old_pager_index = Some(self.pager.selected_completion_index()); old_pager_index = Some(self.pager.selected_completion_index());
} }
} }
@ -4611,15 +4615,16 @@ impl ReaderData {
zelf.flash(); zelf.flash();
return; return;
} }
zelf.history_pager_direction = direction; let history_pager = zelf.history_pager.as_mut().unwrap();
history_pager.direction = direction;
match direction { match direction {
SearchDirection::Forward => { SearchDirection::Forward => {
zelf.history_pager_history_index_start = result.final_index; history_pager.history_index_start = result.final_index;
zelf.history_pager_history_index_end = index; history_pager.history_index_end = index;
} }
SearchDirection::Backward => { SearchDirection::Backward => {
zelf.history_pager_history_index_start = index; history_pager.history_index_start = index;
zelf.history_pager_history_index_end = result.final_index; history_pager.history_index_end = result.final_index;
} }
}; };
zelf.pager.extra_progress_text = if result.have_more_results { zelf.pager.extra_progress_text = if result.have_more_results {