Compare commits
10 Commits
fd6ffc9645
...
43a7196cf8
Author | SHA1 | Date | |
---|---|---|---|
43a7196cf8 | |||
39e1fea36f | |||
c2dfd020bf | |||
37ae70bc56 | |||
95fa4128ca | |||
55fff9612e | |||
102af558f5 | |||
ff8103c805 | |||
6773122076 | |||
1520374a39 |
25
Cargo.lock
generated
25
Cargo.lock
generated
@ -492,6 +492,7 @@ dependencies = [
|
||||
"dirs",
|
||||
"dotenv",
|
||||
"gitea-rs",
|
||||
"nucleo-matcher",
|
||||
"octocrab",
|
||||
"pretty_assertions",
|
||||
"prost",
|
||||
@ -905,6 +906,16 @@ dependencies = [
|
||||
"winapi",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "nucleo-matcher"
|
||||
version = "0.3.1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "bf33f538733d1a5a3494b836ba913207f14d9d4a1d3cd67030c5061bdd2cac85"
|
||||
dependencies = [
|
||||
"memchr",
|
||||
"unicode-segmentation",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "num-bigint"
|
||||
version = "0.4.6"
|
||||
@ -1134,9 +1145,9 @@ checksum = "439ee305def115ba05938db6eb1644ff94165c5ab5e9420d1c1bcedbba909391"
|
||||
|
||||
[[package]]
|
||||
name = "pretty_assertions"
|
||||
version = "1.4.0"
|
||||
version = "1.4.1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "af7cee1a6c8a5b9208b3cb1061f10c0cb689087b3d8ce85fb9d2dd7a29b6ba66"
|
||||
checksum = "3ae130e2f271fbc2ac3a40fb1d07180839cdbbe443c7a27e1e3c13c5cac0116d"
|
||||
dependencies = [
|
||||
"diff",
|
||||
"yansi",
|
||||
@ -1965,6 +1976,12 @@ dependencies = [
|
||||
"tinyvec",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "unicode-segmentation"
|
||||
version = "1.12.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "f6ccf251212114b54433ec949fd6a7841275f9ada20dddd2f29e9ceea4501493"
|
||||
|
||||
[[package]]
|
||||
name = "untrusted"
|
||||
version = "0.9.0"
|
||||
@ -2329,9 +2346,9 @@ dependencies = [
|
||||
|
||||
[[package]]
|
||||
name = "yansi"
|
||||
version = "0.5.1"
|
||||
version = "1.0.1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "09041cd90cf85f7f8b2df60c646f853b7f535ce68f85244eb6731cf89fa498ec"
|
||||
checksum = "cfe53a6657fd280eaa890a3bc59152892ffa3e30101319d168b781ed6529b049"
|
||||
|
||||
[[package]]
|
||||
name = "zeroize"
|
||||
|
@ -24,6 +24,7 @@ dirs = "5.0.1"
|
||||
prost = "0.13.2"
|
||||
prost-types = "0.13.2"
|
||||
bytes = "1.7.1"
|
||||
nucleo-matcher = "0.3.1"
|
||||
|
||||
[dev-dependencies]
|
||||
pretty_assertions = "1.4.0"
|
||||
|
@ -1,4 +1,12 @@
|
||||
use crate::{app::App, cache::CacheApp, projects_list::ProjectsListApp};
|
||||
use std::collections::BTreeMap;
|
||||
|
||||
use crate::{
|
||||
app::App,
|
||||
cache::CacheApp,
|
||||
fuzzy_matcher::{FuzzyMatcher, FuzzyMatcherApp},
|
||||
git_provider::Repository,
|
||||
projects_list::ProjectsListApp,
|
||||
};
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct RootCommand {
|
||||
@ -10,8 +18,7 @@ impl RootCommand {
|
||||
Self { app }
|
||||
}
|
||||
|
||||
#[tracing::instrument(skip(self))]
|
||||
pub async fn execute(&mut self) -> anyhow::Result<()> {
|
||||
pub async fn execute(&mut self, search: Option<impl Into<String>>) -> anyhow::Result<()> {
|
||||
tracing::debug!("executing");
|
||||
|
||||
let repositories = match self.app.cache().get().await? {
|
||||
@ -25,9 +32,19 @@ impl RootCommand {
|
||||
repositories
|
||||
}
|
||||
};
|
||||
let needle = match search {
|
||||
Some(needle) => needle.into(),
|
||||
None => todo!(),
|
||||
};
|
||||
|
||||
for repo in &repositories {
|
||||
//tracing::info!("repo: {}", repo.to_rel_path().display());
|
||||
let matched_repos = self
|
||||
.app
|
||||
.fuzzy_matcher()
|
||||
.match_repositories(&needle, &repositories);
|
||||
let res = matched_repos.iter().take(10).rev().collect::<Vec<_>>();
|
||||
|
||||
for repo in res {
|
||||
tracing::debug!("repo: {:?}", repo);
|
||||
}
|
||||
|
||||
tracing::info!("amount of repos fetched {}", repositories.len());
|
||||
@ -35,3 +52,43 @@ impl RootCommand {
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
trait StringExt {
|
||||
fn as_str_vec(&self) -> Vec<&str>;
|
||||
}
|
||||
|
||||
impl StringExt for Vec<String> {
|
||||
fn as_str_vec(&self) -> Vec<&str> {
|
||||
self.iter().map(|r| r.as_ref()).collect()
|
||||
}
|
||||
}
|
||||
|
||||
impl StringExt for Vec<&String> {
|
||||
fn as_str_vec(&self) -> Vec<&str> {
|
||||
self.iter().map(|r| r.as_ref()).collect()
|
||||
}
|
||||
}
|
||||
|
||||
trait RepositoryMatcher {
|
||||
fn match_repositories(&self, pattern: &str, repositories: &[Repository]) -> Vec<Repository>;
|
||||
}
|
||||
|
||||
impl RepositoryMatcher for FuzzyMatcher {
|
||||
fn match_repositories(&self, pattern: &str, repositories: &[Repository]) -> Vec<Repository> {
|
||||
let haystack = repositories
|
||||
.iter()
|
||||
.map(|r| (r.to_rel_path().display().to_string(), r))
|
||||
.collect::<BTreeMap<_, _>>();
|
||||
let haystack_keys = haystack.keys().collect::<Vec<_>>();
|
||||
let haystack_keys = haystack_keys.as_str_vec();
|
||||
|
||||
let res = self.match_pattern(pattern, &haystack_keys);
|
||||
|
||||
let matched_repos = res
|
||||
.into_iter()
|
||||
.filter_map(|repo_key| haystack.get(repo_key).map(|r| (*r).to_owned()))
|
||||
.collect::<Vec<_>>();
|
||||
|
||||
matched_repos
|
||||
}
|
||||
}
|
||||
|
34
crates/gitnow/src/fuzzy_matcher.rs
Normal file
34
crates/gitnow/src/fuzzy_matcher.rs
Normal file
@ -0,0 +1,34 @@
|
||||
use nucleo_matcher::{pattern::Pattern, Matcher};
|
||||
|
||||
use crate::app::App;
|
||||
|
||||
pub struct FuzzyMatcher {}
|
||||
|
||||
impl FuzzyMatcher {
|
||||
pub fn new() -> Self {
|
||||
Self {}
|
||||
}
|
||||
|
||||
pub fn match_pattern<'a>(&self, pattern: &'a str, items: &'a [&'a str]) -> Vec<&'a str> {
|
||||
let pat = Pattern::new(
|
||||
pattern,
|
||||
nucleo_matcher::pattern::CaseMatching::Ignore,
|
||||
nucleo_matcher::pattern::Normalization::Smart,
|
||||
nucleo_matcher::pattern::AtomKind::Fuzzy,
|
||||
);
|
||||
let mut matcher = Matcher::new(nucleo_matcher::Config::DEFAULT);
|
||||
let res = pat.match_list(items, &mut matcher);
|
||||
|
||||
res.into_iter().map(|(item, _)| *item).collect::<Vec<_>>()
|
||||
}
|
||||
}
|
||||
|
||||
pub trait FuzzyMatcherApp {
|
||||
fn fuzzy_matcher(&self) -> FuzzyMatcher;
|
||||
}
|
||||
|
||||
impl FuzzyMatcherApp for &'static App {
|
||||
fn fuzzy_matcher(&self) -> FuzzyMatcher {
|
||||
FuzzyMatcher::new()
|
||||
}
|
||||
}
|
@ -1,10 +1,4 @@
|
||||
use anyhow::Context;
|
||||
use octocrab::{
|
||||
auth::Auth,
|
||||
models::{hooks::Config, Repository},
|
||||
params::repos::Sort,
|
||||
NoSvc, Octocrab, Page,
|
||||
};
|
||||
use octocrab::{models::Repository, params::repos::Sort, Octocrab, Page};
|
||||
|
||||
use crate::{app::App, config::GitHubAccessToken};
|
||||
|
||||
@ -159,7 +153,7 @@ impl GitHubProvider {
|
||||
|
||||
fn get_client(
|
||||
&self,
|
||||
url: Option<&String>,
|
||||
_url: Option<&String>,
|
||||
access_token: &GitHubAccessToken,
|
||||
) -> anyhow::Result<Octocrab> {
|
||||
let client = octocrab::Octocrab::builder()
|
||||
|
@ -12,6 +12,7 @@ mod cache;
|
||||
mod cache_codec;
|
||||
mod commands;
|
||||
mod config;
|
||||
mod fuzzy_matcher;
|
||||
mod git_provider;
|
||||
mod projects_list;
|
||||
|
||||
@ -20,6 +21,9 @@ mod projects_list;
|
||||
struct Command {
|
||||
#[command(subcommand)]
|
||||
command: Option<Commands>,
|
||||
|
||||
#[arg()]
|
||||
search: Option<String>,
|
||||
}
|
||||
|
||||
#[derive(Subcommand)]
|
||||
@ -51,7 +55,7 @@ async fn main() -> anyhow::Result<()> {
|
||||
match cli.command {
|
||||
Some(_) => todo!(),
|
||||
None => {
|
||||
RootCommand::new(app).execute().await?;
|
||||
RootCommand::new(app).execute(cli.search.as_ref()).await?;
|
||||
}
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user