Skip to content

Commit

Permalink
Merge pull request #387 from siketyan/feat/search-cmd
Browse files Browse the repository at this point in the history
feat: Introduce search command to perform a fuzzy search
  • Loading branch information
siketyan authored Jun 29, 2024
2 parents eeb9b77 + 09ef495 commit 0cca9c4
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 7 deletions.
17 changes: 17 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 @@ -25,6 +25,7 @@ git2 = "0.18.3"
itertools = "0.13.0"
indicatif = "0.17.8"
lazy_static = "1.5"
nucleo-matcher = "0.3.1"
regex = "1.10"
serde = { version = "1.0", features = ["derive"] }
serde_regex = "1.1"
Expand Down
18 changes: 11 additions & 7 deletions src/cmd/mod.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
use std::io::stderr;

use anyhow::Result;
use clap::{Parser, Subcommand};
use tracing_subscriber::filter::LevelFilter;
use tracing_subscriber::EnvFilter;

mod add;
mod browse;
mod cd;
Expand All @@ -8,17 +15,11 @@ mod list;
mod open;
mod path;
mod profile;
mod search;
mod shell;
mod sync;
mod version;

use std::io::stderr;

use anyhow::Result;
use clap::{Parser, Subcommand};
use tracing_subscriber::filter::LevelFilter;
use tracing_subscriber::EnvFilter;

#[derive(Debug, Subcommand)]
pub enum Action {
/// Add an existing repository into the ghr managed directory.
Expand All @@ -41,6 +42,8 @@ pub enum Action {
Path(path::Cmd),
/// Manages profiles to use in repositories.
Profile(profile::Cmd),
/// Perform a fuzzy search on the repositories list.
Search(search::Cmd),
/// Writes a shell script to extend ghr features.
Shell(shell::Cmd),
/// Sync repositories between your devices.
Expand Down Expand Up @@ -93,6 +96,7 @@ impl Cli {
Browse(cmd) => cmd.run().await,
Path(cmd) => cmd.run(),
Profile(cmd) => cmd.run(),
Search(cmd) => cmd.run(),
Shell(cmd) => cmd.run(),
Sync(cmd) => cmd.run().await,
Version(cmd) => cmd.run(),
Expand Down
42 changes: 42 additions & 0 deletions src/cmd/search.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
use anyhow::Result;
use clap::Parser;
use itertools::Itertools;
use nucleo_matcher::{Config, Matcher, Utf32Str};

use crate::repository::Repositories;
use crate::root::Root;

const MIN_SCORE: u16 = 50;

#[derive(Debug, Parser)]
pub struct Cmd {
query: String,
}

impl Cmd {
pub fn run(self) -> Result<()> {
let root = Root::find()?;

let mut matcher = Matcher::new(Config::DEFAULT);

Repositories::try_collect(&root)?
.into_iter()
.map(|(path, _)| path.to_string())
.filter_map(|path| {
let mut haystack_buf = Vec::new();
let mut needle_buf = Vec::new();

matcher
.fuzzy_match(
Utf32Str::new(&path, &mut haystack_buf),
Utf32Str::new(&self.query, &mut needle_buf),
)
.map(|score| (path, score))
})
.filter(|(_, score)| *score > MIN_SCORE)
.sorted_by_key(|(_, score)| -i32::from(*score))
.for_each(|(path, _)| println!("{}", path));

Ok(())
}
}

0 comments on commit 0cca9c4

Please sign in to comment.