Skip to content
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
27 changes: 27 additions & 0 deletions Cargo.lock

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

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ tokio = { version = "1.52", optional = true, features = ["rt"] }
toml = "1.1"
unicode-width = "0.2"
regex = "1.12"
fancy-regex = "0.16"

[build-dependencies]
anyhow = "1.0"
Expand Down
1 change: 1 addition & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ mod columns;
mod config;
mod opt;
mod process;
mod search_regex;
mod style;
mod term_info;
mod util;
Expand Down
60 changes: 60 additions & 0 deletions src/search_regex.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
use anyhow::Error;

pub enum SearchRegex {
Fast(regex::Regex),
Fancy(fancy_regex::Regex),
}

impl SearchRegex {
pub fn new(pattern: &str, ignore_case: bool) -> Result<Self, Error> {
if let Ok(regex) = regex::RegexBuilder::new(pattern)
.case_insensitive(ignore_case)
.build()
{
return Ok(Self::Fast(regex));
}

let regex = fancy_regex::RegexBuilder::new(pattern)
.case_insensitive(ignore_case)
.build()?;
Ok(Self::Fancy(regex))
}

pub fn is_match(&self, text: &str) -> Result<bool, Error> {
match self {
Self::Fast(regex) => Ok(regex.is_match(text)),
Self::Fancy(regex) => Ok(regex.is_match(text)?),
}
}
}

#[cfg(test)]
mod tests {
use super::SearchRegex;

#[test]
fn uses_fast_engine_for_standard_regex() {
let regex = SearchRegex::new("proc.*", false).unwrap();

assert!(matches!(regex, SearchRegex::Fast(_)));
assert!(regex.is_match("procs").unwrap());
}

#[test]
fn falls_back_to_fancy_engine_for_lookahead() {
let regex = SearchRegex::new("proc(?=s)", false).unwrap();

assert!(matches!(regex, SearchRegex::Fancy(_)));
assert!(regex.is_match("procs").unwrap());
assert!(!regex.is_match("procx").unwrap());
}

#[test]
fn falls_back_to_fancy_engine_for_negative_lookahead() {
let regex = SearchRegex::new("proc(?!x)", false).unwrap();

assert!(matches!(regex, SearchRegex::Fancy(_)));
assert!(regex.is_match("procs").unwrap());
assert!(!regex.is_match("procx").unwrap());
}
}
23 changes: 12 additions & 11 deletions src/view.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use crate::columns::*;
use crate::config::*;
use crate::opt::{ArgColorMode, ArgPagerMode};
use crate::process::collect_proc;
use crate::search_regex::SearchRegex;
use crate::style::{apply_color, apply_style, color_to_column_style};
use crate::term_info::TermInfo;
use crate::util::{
Expand All @@ -13,7 +14,6 @@ use crate::util::{
use anyhow::{Error, bail};
#[cfg(not(target_os = "windows"))]
use pager::Pager;
use regex::RegexBuilder;
use std::collections::HashMap;
use std::time::Duration;

Expand Down Expand Up @@ -52,10 +52,8 @@ impl View {

// Adding the sort column to inserts if not already present
match (&opt.sorta, &opt.sortd) {
(_, Some(col)) | (Some(col), _) => {
if !opt.insert.contains(col) {
opt.insert.push(col.clone());
}
(_, Some(col)) | (Some(col), _) if !opt.insert.contains(col) => {
opt.insert.push(col.clone());
}
_ => {}
}
Expand Down Expand Up @@ -263,9 +261,7 @@ impl View {
ConfigSearchCase::Insensitive => true,
ConfigSearchCase::Sensitive => false,
};
let regex = RegexBuilder::new(pattern)
.case_insensitive(ignore_case)
.build()?;
let regex = SearchRegex::new(pattern, ignore_case)?;
Some(regex)
} else {
None
Expand Down Expand Up @@ -316,7 +312,7 @@ impl View {
} else if opt.keyword.is_empty() {
true
} else if let Some(regex) = &regex {
View::search_regex(*pid, cols_searchable.as_slice(), regex)
View::search_regex(*pid, cols_searchable.as_slice(), regex)?
} else {
View::search(
*pid,
Expand Down Expand Up @@ -714,8 +710,13 @@ impl View {
}
}

fn search_regex(pid: i32, cols: &[&dyn Column], regex: &regex::Regex) -> bool {
cols.iter().any(|c| regex.is_match(&c.display_json(pid)))
fn search_regex(pid: i32, cols: &[&dyn Column], regex: &SearchRegex) -> Result<bool, Error> {
for c in cols {
if regex.is_match(&c.display_json(pid))? {
return Ok(true);
}
}
Ok(false)
}

#[cfg(not(any(target_os = "windows", any(target_os = "linux", target_os = "android"))))]
Expand Down
7 changes: 2 additions & 5 deletions src/watcher.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
use crate::Opt;
use crate::config::*;
use crate::search_regex::SearchRegex;
use crate::term_info::TermInfo;
use crate::util::{get_theme, has_regex_syntax};
use crate::view::View;
use anyhow::Error;
use chrono::offset::Local;
use getch::Getch;
use regex::RegexBuilder;
use std::collections::HashMap;
use std::sync::mpsc::{Receiver, Sender, channel};
use std::thread;
Expand Down Expand Up @@ -221,10 +221,7 @@ impl Watcher {
ConfigSearchCase::Insensitive => true,
ConfigSearchCase::Sensitive => false,
};
match RegexBuilder::new(&candidate)
.case_insensitive(ignore_case)
.build()
{
match SearchRegex::new(&candidate, ignore_case) {
Ok(_) => {
opt.keyword = vec![candidate];
regex_error = None;
Expand Down
Loading