Skip to content

Commit

Permalink
feat: added new options to the `find' command for smart-case/case-ins…
Browse files Browse the repository at this point in the history
…ensitive finding (#240)
  • Loading branch information
ndtoan96 authored Oct 4, 2023
1 parent 77988d4 commit 6da04c7
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 11 deletions.
10 changes: 8 additions & 2 deletions app/src/executor.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use core::{emit, files::FilesSorter, input::InputMode};
use core::{emit, files::FilesSorter, input::InputMode, manager::FinderCase};

use config::{keymap::{Control, Exec, Key, KeymapLayer}, manager::SortBy, KEYMAP};
use shared::{optional_bool, Url};
Expand Down Expand Up @@ -154,7 +154,13 @@ impl Executor {
"find" => {
let query = exec.args.get(0).map(|s| s.as_str());
let prev = exec.named.contains_key("previous");
cx.manager.active_mut().find(query, prev)
let case = match (exec.named.contains_key("smart"), exec.named.contains_key("insensitive"))
{
(true, _) => FinderCase::Smart,
(_, false) => FinderCase::Sensitive,
(_, true) => FinderCase::Insensitive,
};
cx.manager.active_mut().find(query, prev, case)
}
"find_arrow" => cx.manager.active_mut().find_arrow(exec.named.contains_key("previous")),

Expand Down
4 changes: 2 additions & 2 deletions config/preset/keymap.toml
Original file line number Diff line number Diff line change
Expand Up @@ -73,8 +73,8 @@ keymap = [
{ on = [ "c", "n" ], exec = "copy name_without_ext", desc = "Copy the name of the file without the extension" },

# Find
{ on = [ "/" ], exec = "find" },
{ on = [ "?" ], exec = "find --previous" },
{ on = [ "/" ], exec = "find --smart" },
{ on = [ "?" ], exec = "find --previous --smart" },
{ on = [ "n" ], exec = "find_arrow" },
{ on = [ "N" ], exec = "find_arrow --previous" },

Expand Down
21 changes: 18 additions & 3 deletions core/src/manager/finder.rs
Original file line number Diff line number Diff line change
@@ -1,20 +1,35 @@
use std::{collections::BTreeMap, ffi::OsStr};

use anyhow::Result;
use regex::bytes::Regex;
use regex::bytes::{Regex, RegexBuilder};
use shared::Url;

use crate::files::Files;

#[derive(PartialEq, Eq)]
pub enum FinderCase {
Smart,
Sensitive,
Insensitive,
}

pub struct Finder {
query: Regex,
matched: BTreeMap<Url, u8>,
version: u64,
}

impl Finder {
pub(super) fn new(s: &str) -> Result<Self> {
Ok(Self { query: Regex::new(s)?, matched: Default::default(), version: 0 })
pub(super) fn new(s: &str, case: FinderCase) -> Result<Self> {
let query = match case {
FinderCase::Smart => {
let uppercase = s.chars().any(|c| c.is_uppercase());
RegexBuilder::new(s).case_insensitive(!uppercase).build()?
}
FinderCase::Sensitive => Regex::new(s)?,
FinderCase::Insensitive => RegexBuilder::new(s).case_insensitive(true).build()?,
};
Ok(Self { query, matched: Default::default(), version: 0 })
}

pub(super) fn prev(&self, files: &Files, cursor: usize, include: bool) -> Option<isize> {
Expand Down
12 changes: 8 additions & 4 deletions core/src/manager/tab.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use shared::{Debounce, Defer, InputError, Url};
use tokio::{pin, task::JoinHandle};
use tokio_stream::{wrappers::UnboundedReceiverStream, StreamExt};

use super::{Backstack, Finder, Folder, Mode, Preview, PreviewLock};
use super::{Backstack, Finder, FinderCase, Folder, Mode, Preview, PreviewLock};
use crate::{emit, external::{self, FzfOpt, ZoxideOpt}, files::{File, FilesOp, FilesSorter}, input::InputOpt, Event, Step, BLOCKER};

pub struct Tab {
Expand Down Expand Up @@ -267,9 +267,9 @@ impl Tab {
false
}

pub fn find(&mut self, query: Option<&str>, prev: bool) -> bool {
pub fn find(&mut self, query: Option<&str>, prev: bool, case: FinderCase) -> bool {
if let Some(query) = query {
let Ok(finder) = Finder::new(query) else {
let Ok(finder) = Finder::new(query, case) else {
return false;
};

Expand Down Expand Up @@ -297,7 +297,11 @@ impl Tab {

while let Some(Ok(s)) | Some(Err(InputError::Typed(s))) = rx.next().await {
emit!(Call(
Exec::call("find", vec![s]).with_bool("previous", prev).vec(),
Exec::call("find", vec![s])
.with_bool("previous", prev)
.with_bool("smart", case == FinderCase::Smart)
.with_bool("insensitive", case == FinderCase::Insensitive)
.vec(),
KeymapLayer::Manager
));
}
Expand Down

0 comments on commit 6da04c7

Please sign in to comment.