Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement some small fixes and find end position of match #131

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 7 additions & 7 deletions src/core/commands.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,13 +72,13 @@ impl PartialEq for Command {
impl Debug for Command {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
Self::SetData(text) => write!(f, "SetData({:?})", text),
Self::AppendData(text) => write!(f, "AppendData({:?})", text),
Self::SetPrompt(text) => write!(f, "SetPrompt({:?})", text),
Self::SendMessage(text) => write!(f, "SendMessage({:?})", text),
Self::SetLineNumbers(ln) => write!(f, "SetLineNumbers({:?})", ln),
Self::LineWrapping(lw) => write!(f, "LineWrapping({:?})", lw),
Self::SetExitStrategy(es) => write!(f, "SetExitStrategy({:?})", es),
Self::SetData(text) => write!(f, "SetData({text:?})"),
Self::AppendData(text) => write!(f, "AppendData({text:?})"),
Self::SetPrompt(text) => write!(f, "SetPrompt({text:?})"),
Self::SendMessage(text) => write!(f, "SendMessage({text:?})"),
Self::SetLineNumbers(ln) => write!(f, "SetLineNumbers({ln:?})"),
Self::LineWrapping(lw) => write!(f, "LineWrapping({lw:?})"),
Self::SetExitStrategy(es) => write!(f, "SetExitStrategy({es:?})"),
Self::SetInputClassifier(_) => write!(f, "SetInputClassifier"),
Self::ShowPrompt(show) => write!(f, "ShowPrompt({show:?})"),
Self::FormatRedrawPrompt => write!(f, "FormatRedrawPrompt"),
Expand Down
36 changes: 24 additions & 12 deletions src/core/ev_handler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ use super::CommandQueue;
use super::{commands::Command, utils::term};
#[cfg(feature = "search")]
use crate::search;
use crate::search::SearchMatch;
use crate::{error::MinusError, input::InputEvent, PagerState};

/// Respond based on the type of command
Expand Down Expand Up @@ -138,16 +139,21 @@ pub fn handle_event(
if p.search_state.search_term.is_some() =>
{
// Move to next search match after the current upper_mark
let position_of_next_match =
search::next_nth_match(&p.search_state.search_idx, p.upper_mark, 1);
let position_of_next_match = search::next_nth_match(
&p.search_state.search_idx,
p.upper_mark,
p.left_mark + p.cols,
1,
);
if let Some(pnm) = position_of_next_match {
p.search_state.search_mark = pnm;
let upper_mark = *p
let upper_mark = p
.search_state
.search_idx
.iter()
.nth(p.search_state.search_mark)
.unwrap();
.unwrap()
.row;
command_queue.push_back_unchecked(Command::UserInput(InputEvent::UpdateUpperMark(
upper_mark,
)));
Expand All @@ -164,7 +170,7 @@ pub fn handle_event(
}
// Decrement the s_mark and get the preceding index
p.search_state.search_mark = p.search_state.search_mark.saturating_sub(1);
if let Some(y) = p
if let Some(SearchMatch { row: y, .. }) = p
.search_state
.search_idx
.iter()
Expand All @@ -185,16 +191,21 @@ pub fn handle_event(
if p.search_state.search_term.is_some() =>
{
// Move to next nth search match after the current upper_mark
let position_of_next_match =
search::next_nth_match(&p.search_state.search_idx, p.upper_mark, n);
let position_of_next_match = search::next_nth_match(
&p.search_state.search_idx,
p.upper_mark,
p.left_mark + p.cols,
n,
);
if let Some(pnm) = position_of_next_match {
p.search_state.search_mark = pnm;
let upper_mark = *p
let upper_mark = p
.search_state
.search_idx
.iter()
.nth(p.search_state.search_mark)
.unwrap();
.unwrap()
.row;

// Ensure there is enough text available after location corresponding to
// position_of_next_match so that we can display a pagefull of data. If not,
Expand All @@ -204,12 +215,13 @@ pub fn handle_event(
> p.screen.formatted_lines_count().saturating_add(1)
{
p.search_state.search_mark = p.search_state.search_mark.saturating_sub(1);
p.upper_mark = *p
p.upper_mark = p
.search_state
.search_idx
.iter()
.nth(p.search_state.search_mark)
.unwrap();
.unwrap()
.row;
}
command_queue.push_back_unchecked(Command::UserInput(InputEvent::UpdateUpperMark(
upper_mark,
Expand All @@ -227,7 +239,7 @@ pub fn handle_event(
}
// Decrement the s_mark and get the preceding index
p.search_state.search_mark = p.search_state.search_mark.saturating_sub(n);
if let Some(y) = p
if let Some(SearchMatch { row: y, .. }) = p
.search_state
.search_idx
.iter()
Expand Down
23 changes: 12 additions & 11 deletions src/input/hashed_event_register.rs
Original file line number Diff line number Diff line change
Expand Up @@ -231,20 +231,20 @@ where
/// Prefer using this over [add_key_events](HashedEventRegister::add_key_events).
///
/// # Example
/// ```
/// ```should_panic
/// use minus::input::{InputEvent, HashedEventRegister, crossterm_event};
///
/// let mut input_register = HashedEventRegister::default();
///
/// input_register.add_key_events(&["down"], |_, ps| {
/// input_register.add_key_events_checked(&["down"], |_, ps| {
/// InputEvent::UpdateUpperMark(ps.upper_mark.saturating_sub(1))
/// });
/// }, false);
/// ```
pub fn add_key_events_checked(
&mut self,
desc: &[&str],
remap: bool,
cb: impl Fn(Event, &PagerState) -> InputEvent + Send + Sync + 'static,
remap: bool,
) {
let v = Arc::new(cb);
for k in desc {
Expand Down Expand Up @@ -315,26 +315,27 @@ where
///
/// Prefer using this over [add_mouse_events](HashedEventRegister::add_mouse_events).
/// # Example
/// ```
/// ```should_panic
/// use minus::input::{InputEvent, HashedEventRegister};
///
/// let mut input_register = HashedEventRegister::default();
///
/// input_register.add_mouse_events(&["scroll:down"], |_, ps| {
/// input_register.add_mouse_events_checked(&["scroll:down"], |_, ps| {
/// InputEvent::UpdateUpperMark(ps.upper_mark.saturating_sub(5))
/// });
/// }, false);
/// ```
pub fn add_mouse_events_checked(
&mut self,
desc: &[&str],
cb: impl Fn(Event, &PagerState) -> InputEvent + Send + Sync + 'static,
remap: bool,
) {
let v = Arc::new(cb);
for k in desc {
self.0.insert(
Event::Mouse(super::definitions::mousedefs::parse_mouse_event(k)).into(),
v.clone(),
);
let def: EventWrapper =
Event::Mouse(super::definitions::mousedefs::parse_mouse_event(k)).into();
assert!(self.0.contains_key(&def) && remap, "");
self.0.insert(def, v.clone());
}
}

Expand Down
25 changes: 16 additions & 9 deletions src/screen/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
//! This module is still a work is progress and is subject to change.
use crate::{
minus_core::{self, utils::LinesRowMap},
search::SearchMatch,
LineNumbers,
};
#[cfg(feature = "search")]
Expand All @@ -11,7 +12,10 @@ use regex::Regex;
use std::borrow::Cow;

#[cfg(feature = "search")]
use {crate::search, std::collections::BTreeSet};
use {
crate::search::{self, SearchIndex},
std::collections::BTreeSet,
};

// |||||||||||||||||||||||||||||||||||||||||||||||||||||||
// TYPES TO BETTER DESCRIBE THE PURPOSE OF STRINGS
Expand Down Expand Up @@ -282,7 +286,7 @@ pub(crate) struct FormatResult {
pub num_unterminated: usize,
/// If search is active, this contains the indices where search matches in the incoming text have been found
#[cfg(feature = "search")]
pub append_search_idx: BTreeSet<usize>,
pub append_search_idx: SearchIndex,
/// Map of where first row of each line is placed inside in
/// [`PagerState::formatted_lines`](crate::state::PagerState::formatted_lines)
pub lines_to_row_map: LinesRowMap,
Expand Down Expand Up @@ -468,7 +472,11 @@ where
fr.append_search_idx = fr
.append_search_idx
.iter()
.map(|i| opts.formatted_lines_count + i)
.map(|i| SearchMatch {
row: i.row + formatted_row_count,
col: i.col,
shifted_col: i.shifted_col,
})
.collect();
}

Expand Down Expand Up @@ -510,7 +518,7 @@ pub(crate) fn formatted_line<'a>(
cols: usize,
line_wrapping: bool,
#[cfg(feature = "search")] formatted_idx: usize,
#[cfg(feature = "search")] search_idx: &mut BTreeSet<usize>,
#[cfg(feature = "search")] search_idx: &mut SearchIndex,
#[cfg(feature = "search")] search_term: &Option<regex::Regex>,
) -> Rows {
assert!(
Expand Down Expand Up @@ -546,15 +554,14 @@ pub(crate) fn formatted_line<'a>(

// highlight the lines with matching search terms
// If a match is found, add this line's index to PagerState::search_idx
#[cfg_attr(not(feature = "search"), allow(unused_mut))]
#[cfg_attr(not(feature = "search"), allow(unused_variables))]
#[cfg_attr(not(feature = "search"), allow(unused_mut, unused_variables))]
let mut handle_search = |row: &mut Cow<'a, str>, wrap_idx: usize| {
#[cfg(feature = "search")]
if let Some(st) = search_term.as_ref() {
let (highlighted_row, is_match) = search::highlight_line_matches(row, st, false);
if is_match {
if let Some(highlighted_row) =
search::highlight_line_matches(row, st, search_idx, formatted_idx + wrap_idx, false)
{
*row.to_mut() = highlighted_row;
search_idx.insert(formatted_idx + wrap_idx);
}
}
};
Expand Down
Loading
Loading