-
Notifications
You must be signed in to change notification settings - Fork 38
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
e1e657a
commit 7f13c15
Showing
6 changed files
with
225 additions
and
62 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,157 @@ | ||
use devicons::FileIcon; | ||
use nucleo::{ | ||
pattern::{CaseMatching, Normalization}, | ||
Config, Nucleo, | ||
}; | ||
use std::sync::Arc; | ||
|
||
use tracing::info; | ||
|
||
use crate::components::{ | ||
finders::{Entry, Finder}, | ||
fuzzy::MATCHER, | ||
previewers::PreviewType, | ||
}; | ||
|
||
struct EnvVar { | ||
name: String, | ||
value: String, | ||
} | ||
|
||
pub struct NucleoEnvVarFinder { | ||
matcher: Nucleo<EnvVar>, | ||
last_pattern: String, | ||
file_icon: FileIcon, | ||
result_count: u32, | ||
total_count: u32, | ||
} | ||
|
||
const NUM_THREADS: usize = 1; | ||
const FILE_ICON_STR: &str = "config"; | ||
|
||
impl NucleoEnvVarFinder { | ||
pub fn new() -> Self { | ||
let matcher = Nucleo::new(Config::DEFAULT, Arc::new(|| {}), Some(NUM_THREADS), 2); | ||
let injector = matcher.injector(); | ||
for (name, value) in std::env::vars() { | ||
let _ = injector.push(EnvVar { name, value }, |e, cols| { | ||
cols[0] = e.name.clone().into(); | ||
cols[1] = e.value.clone().into(); | ||
}); | ||
} | ||
NucleoEnvVarFinder { | ||
matcher, | ||
last_pattern: String::new(), | ||
file_icon: FileIcon::from(FILE_ICON_STR), | ||
result_count: 0, | ||
total_count: 0, | ||
} | ||
} | ||
|
||
const MATCHER_TICK_TIMEOUT: u64 = 10; | ||
} | ||
|
||
impl Finder for NucleoEnvVarFinder { | ||
fn find(&mut self, pattern: &str) { | ||
if pattern != self.last_pattern { | ||
self.matcher.pattern.reparse( | ||
0, | ||
pattern, | ||
CaseMatching::Smart, | ||
Normalization::Smart, | ||
if pattern.starts_with(&self.last_pattern) { | ||
true | ||
} else { | ||
false | ||
}, | ||
); | ||
self.last_pattern = pattern.to_string(); | ||
} | ||
} | ||
|
||
fn result_count(&self) -> u32 { | ||
self.result_count | ||
} | ||
|
||
fn total_count(&self) -> u32 { | ||
self.total_count | ||
} | ||
|
||
// FIXME: find a trick to match name + value but not orthogonally like nucleo does | ||
fn results(&mut self, num_entries: u32, offset: u32) -> impl Iterator<Item = Entry> { | ||
let status = self.matcher.tick(Self::MATCHER_TICK_TIMEOUT); | ||
let snapshot = self.matcher.snapshot(); | ||
if status.changed { | ||
self.result_count = snapshot.matched_item_count(); | ||
self.total_count = snapshot.item_count(); | ||
} | ||
let mut name_indices = Vec::new(); | ||
let mut value_indices = Vec::new(); | ||
let mut matcher = MATCHER.lock(); | ||
let icon = self.file_icon.clone(); | ||
let mut should_add_name_indices = false; | ||
let mut should_add_value_indices = false; | ||
|
||
snapshot | ||
.matched_items(offset..(num_entries + offset).min(snapshot.matched_item_count())) | ||
.map(move |item| { | ||
snapshot.pattern().column_pattern(0).indices( | ||
item.matcher_columns[0].slice(..), | ||
&mut matcher, | ||
&mut name_indices, | ||
); | ||
name_indices.sort_unstable(); | ||
name_indices.dedup(); | ||
should_add_name_indices = !name_indices.is_empty(); | ||
let name_indices = name_indices.drain(..); | ||
|
||
snapshot.pattern().column_pattern(1).indices( | ||
item.matcher_columns[1].slice(..), | ||
&mut matcher, | ||
&mut value_indices, | ||
); | ||
value_indices.sort_unstable(); | ||
value_indices.dedup(); | ||
should_add_value_indices = !value_indices.is_empty(); | ||
info!("value_indices: {:?}", value_indices); | ||
let value_indices = value_indices.drain(..); | ||
|
||
Entry { | ||
name: item.matcher_columns[0].to_string(), | ||
display_name: None, | ||
preview: Some(item.matcher_columns[1].to_string()), | ||
name_match_ranges: if should_add_name_indices { | ||
Some(name_indices.map(|i| (i, i + 1)).collect()) | ||
} else { | ||
None | ||
}, | ||
preview_match_ranges: if should_add_value_indices { | ||
Some(value_indices.map(|i| (i, i + 1)).collect()) | ||
} else { | ||
None | ||
}, | ||
icon: Some(icon), | ||
line_number: None, | ||
preview_type: PreviewType::Files, | ||
} | ||
}) | ||
} | ||
|
||
fn get_result(&self, index: u32) -> Option<Entry> { | ||
let snapshot = self.matcher.snapshot(); | ||
snapshot.get_matched_item(index).and_then(|item| { | ||
let name = item.matcher_columns[0].to_string(); | ||
let value = item.matcher_columns[1].to_string(); | ||
Some(Entry { | ||
name, | ||
display_name: None, | ||
preview: Some(value), | ||
name_match_ranges: None, | ||
preview_match_ranges: None, | ||
icon: None, | ||
line_number: None, | ||
preview_type: PreviewType::EnvVar, | ||
}) | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters