-
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
f56ba58
commit f38c8d9
Showing
22 changed files
with
1,490 additions
and
40 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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
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,17 @@ | ||
/// This module contains abstract display logic for the application. | ||
/// A group of styles that can be applied to a string. | ||
pub enum StyleGroup { | ||
Default, | ||
Entry, | ||
EntryLineNumber, | ||
EntryContent, | ||
} | ||
|
||
/// A styled string with a text and a style group for that string. | ||
/// This is used to produce styled output in a way that can then be translated to the actual | ||
/// frontend implementation. | ||
pub struct StyledString { | ||
pub text: String, | ||
pub style: StyleGroup, | ||
} |
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,25 @@ | ||
mod env; | ||
|
||
// finder types | ||
pub use env::EnvVarFinder; | ||
use rust_devicons::FileIcon; | ||
|
||
#[derive(Clone, Debug)] | ||
pub struct Entry { | ||
pub name: String, | ||
pub preview: Option<String>, | ||
pub score: i64, | ||
pub name_match_ranges: Option<Vec<(usize, usize)>>, | ||
pub preview_match_ranges: Option<Vec<(usize, usize)>>, | ||
pub icon: Option<FileIcon>, | ||
pub line_number: Option<usize>, | ||
} | ||
|
||
/// A trait for a finder that can find entries based on a pattern. | ||
/// The `find` method takes a pattern and returns an iterator over the entries found. | ||
/// | ||
/// # Methods | ||
/// - `find`: Find entries based on a pattern. | ||
pub trait Finder { | ||
fn find(&self, pattern: &str) -> impl Iterator<Item = Entry>; | ||
} |
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,81 @@ | ||
use std::{env::vars, path::Path}; | ||
|
||
use fuzzy_matcher::skim::SkimMatcherV2; | ||
use rust_devicons::{icon_for_file, File, FileIcon}; | ||
|
||
use crate::components::finders::{Entry, Finder}; | ||
|
||
struct EnvVar { | ||
name: String, | ||
value: String, | ||
} | ||
|
||
pub struct EnvVarFinder { | ||
env_vars: Vec<EnvVar>, | ||
matcher: SkimMatcherV2, | ||
file_icon: FileIcon, | ||
} | ||
|
||
impl EnvVarFinder { | ||
pub fn new() -> Self { | ||
let mut env_vars = Vec::new(); | ||
for (name, value) in vars() { | ||
env_vars.push(EnvVar { name, value }); | ||
} | ||
let file_icon = icon_for_file(&File::new(&Path::new("config")), None); | ||
EnvVarFinder { | ||
env_vars, | ||
matcher: SkimMatcherV2::default(), | ||
file_icon, | ||
} | ||
} | ||
} | ||
|
||
impl Finder for EnvVarFinder { | ||
fn find(&self, pattern: &str) -> impl Iterator<Item = Entry> { | ||
let mut results = Vec::new(); | ||
for env_var in &self.env_vars { | ||
if pattern.is_empty() { | ||
results.push(Entry { | ||
name: env_var.name.clone(), | ||
preview: Some(env_var.value.clone()), | ||
score: 0, | ||
name_match_ranges: None, | ||
preview_match_ranges: None, | ||
icon: Some(self.file_icon.clone()), | ||
line_number: None, | ||
}); | ||
} else { | ||
let mut total_score = 0; | ||
let preview_match_ranges = if let Some((score, indices)) = | ||
self.matcher.fuzzy(&env_var.value, pattern, true) | ||
{ | ||
total_score += score; | ||
Some(indices.iter().map(|i| (*i, *i + 1)).collect()) | ||
} else { | ||
None | ||
}; | ||
let name_match_ranges = if let Some((score, indices)) = | ||
self.matcher.fuzzy(&env_var.name, pattern, true) | ||
{ | ||
total_score += score; | ||
Some(indices.iter().map(|i| (*i, *i + 1)).collect()) | ||
} else { | ||
None | ||
}; | ||
if preview_match_ranges.is_some() || name_match_ranges.is_some() { | ||
results.push(Entry { | ||
name: env_var.name.clone(), | ||
preview: Some(env_var.value.clone()), | ||
score: total_score, | ||
name_match_ranges, | ||
preview_match_ranges, | ||
icon: Some(self.file_icon.clone()), | ||
line_number: None, | ||
}); | ||
} | ||
} | ||
} | ||
results.into_iter() | ||
} | ||
} |
Oops, something went wrong.