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

Ansi search #18

Merged
merged 2 commits into from
Mar 28, 2021
Merged
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
71 changes: 61 additions & 10 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 3 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ name = "rp"
path = "src/main.rs"

[features]
logging = ["simplelog", "log"]
logging = ["simplelog", "log", "log-panics"]

[dependencies]
ahash = "0.7.2"
Expand All @@ -35,9 +35,10 @@ memchr = "2.3.4"
pico-args = "0.4.0"
rayon = "1.5.0"
smallvec = "1.6.1"
twoway = "0.2.1"
vte = "0.10.0"
log = { version = "0.4.14", optional = true }
simplelog = { version = "0.10.0", optional = true }
log-panics = { version = "2.0.0", optional = true }

[profile.release]
lto = true
1 change: 1 addition & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ fn main() -> Result<()> {
File::create("rp.log")?,
)
.unwrap();
log_panics::init();
}

let args = match Args::parse() {
Expand Down
102 changes: 79 additions & 23 deletions src/writer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ fn get_output() -> File {
#[derive(Clone, Copy)]
pub struct SearchPosition {
start: u32,
end: u32,
}

#[derive(Clone, PartialEq, Eq)]
Expand Down Expand Up @@ -138,7 +139,6 @@ pub struct UiContext<'b> {
scroll: usize,
terminal_line: usize,
keymap: AHashMap<KeyEvent, KeyBehavior>,
search_text_size: usize,
need_redraw: bool,
prompt_outdated: bool,
prompt_state: PromptState,
Expand All @@ -163,7 +163,6 @@ impl<'b> UiContext<'b> {
search_positions: Vec::new(),
terminal_line: y as usize - 1,
keymap: default_keymap(),
search_text_size: 0,
need_redraw: true,
prompt_state: PromptState::Normal,
prompt_outdated: true,
Expand Down Expand Up @@ -200,14 +199,13 @@ impl<'b> UiContext<'b> {
{
let mut prev_pos = 0;
for pos in search.iter() {
let end = pos.start as usize + self.search_text_size;
self.output_buf
.extend_from_slice(&line[prev_pos..pos.start as usize]);
queue!(self.output_buf, SetAttribute(Attribute::Reverse))?;
self.output_buf
.extend_from_slice(&line[pos.start as usize..end]);
queue!(self.output_buf, SetAttribute(Attribute::Reset))?;
prev_pos = end;
.extend_from_slice(&line[pos.start as usize..pos.end as usize]);
queue!(self.output_buf, SetAttribute(Attribute::NoReverse))?;
prev_pos = pos.end as usize;
}
self.output_buf.extend_from_slice(&line[prev_pos..]);
queue!(self.output_buf, Clear(ClearType::UntilNewLine))?;
Expand All @@ -219,7 +217,7 @@ impl<'b> UiContext<'b> {
queue!(self.output_buf, Clear(ClearType::CurrentLine))?;
self.output_buf.extend_from_slice(self.prompt.as_bytes());
#[cfg(feature = "logging")]
log::trace!("Write {} bytes", buf.len());
log::trace!("Write {} bytes", self.output_buf.len());
self.output.write(&self.output_buf)?;
self.output.flush()?;
self.need_redraw = false;
Expand Down Expand Up @@ -352,27 +350,23 @@ impl<'b> UiContext<'b> {
#[cfg(feature = "logging")]
log::debug!("Search: {:?}", needle);

self.search_text_size = needle.len();
self.need_redraw = true;

self.lines
.par_iter()
.map(|bytes| {
let mut v = SearchPositionArr::new();
let mut prev_pos = 0;

while let Some(pos) = twoway::find_bytes(&bytes[prev_pos..], needle.as_bytes()) {
let start = prev_pos + pos;

v.push(SearchPosition {
start: start as u32,
});

prev_pos = start + needle.len();
}
.map_init(
|| vte::Parser::new(),
|parser, bytes| {
let mut performer = RpPerform::new(needle);

for (i, b) in bytes.iter().enumerate() {
performer.update_pos(i);
parser.advance(&mut performer, *b);
}

v
})
performer.into_arr()
},
)
.collect_into_vec(&mut self.search_positions);

self.move_search(true);
Expand Down Expand Up @@ -579,3 +573,65 @@ impl<'b> Drop for UiContext<'b> {
disable_raw_mode().ok();
}
}

struct RpPerform<'b> {
arr: SearchPositionArr,
matching: bool,
start: usize,
pos: usize,
needle: &'b str,
left_chars: std::iter::Peekable<std::str::Chars<'b>>,
}

impl<'b> RpPerform<'b> {
pub fn new(needle: &'b str) -> Self {
Self {
arr: SearchPositionArr::new(),
matching: false,
start: 0,
pos: 0,
needle,
left_chars: needle.chars().peekable(),
}
}

pub fn update_pos(&mut self, pos: usize) {
if !self.matching {
self.start = pos;
}
self.pos = pos;
}

pub fn reset(&mut self) {
self.start = self.pos;
self.left_chars = self.needle.chars().peekable();
self.matching = false;
}

pub fn into_arr(self) -> SearchPositionArr {
self.arr
}
}

impl<'b> vte::Perform for RpPerform<'b> {
fn print(&mut self, c: char) {
match self.left_chars.peek() {
Some(p) => {
if c == *p {
self.matching = true;
self.left_chars.next();
if self.left_chars.peek().is_none() {
self.arr.push(SearchPosition {
start: self.start as u32,
end: self.pos as u32 + 1,
});
self.reset();
}
} else {
self.reset();
}
}
None => {}
}
}
}