Skip to content

Commit

Permalink
Implement --match-type option
Browse files Browse the repository at this point in the history
  • Loading branch information
lusingander committed Dec 13, 2024
1 parent 293889b commit fa3a5d7
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 9 deletions.
26 changes: 25 additions & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
mod cargo;
mod matcher;
mod tui;
mod util;

Expand All @@ -18,6 +19,8 @@ use ratatui::{
};
use tui::{Ret, Tui};

use crate::matcher::Matcher;

#[derive(Debug, Parser)]
#[command(name = "cargo", bin_name = "cargo")]
enum Cli {
Expand All @@ -38,6 +41,10 @@ struct SelectorArgs {
/// Target kind
#[arg(short, long, value_name = "NAME")]
kind: Option<TargetKind>,

/// Match type
#[arg(short = 't', long, default_value = "substring", value_name = "TYPE")]
match_type: MatchType,
}

#[derive(Debug, Clone)]
Expand All @@ -54,6 +61,21 @@ pub enum TargetKind {
Example,
}

#[derive(Debug, Clone, Copy, PartialEq, Eq, ValueEnum)]
pub enum MatchType {
Substring,
Fuzzy,
}

impl MatchType {
fn matcher(self) -> Matcher {
match self {
MatchType::Substring => Matcher::substring(),
MatchType::Fuzzy => Matcher::fuzzy(),
}
}
}

#[derive(Default, Clone, Copy)]
pub enum Action {
#[default]
Expand Down Expand Up @@ -101,6 +123,7 @@ fn main() -> std::io::Result<()> {
inline,
inline_list_size,
kind,
match_type,
} = args;

let mut targets = cargo::get_all_targets();
Expand All @@ -111,7 +134,8 @@ fn main() -> std::io::Result<()> {
initialize_panic_handler(inline);
let mut terminal = setup(inline, inline_list_size)?;
let term_size = terminal.get_frame().area();
let ret = Tui::new(targets, term_size).run(&mut terminal);
let matcher = match_type.matcher();
let ret = Tui::new(targets, term_size, matcher).run(&mut terminal);
shutdown(inline)?;

if inline {
Expand Down
18 changes: 10 additions & 8 deletions src/tui.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use ratatui::{
};
use tui_input::{backend::crossterm::EventHandler, Input};

use crate::{util::digits, Action, Target, TargetKind};
use crate::{matcher::Matcher, util::digits, Action, Target, TargetKind};

#[derive(Default)]
pub struct Tui {
Expand All @@ -23,6 +23,8 @@ pub struct Tui {

list_height: usize,
list_offset: usize,

matcher: Matcher,
}

struct FilteredTarget {
Expand All @@ -37,10 +39,11 @@ pub enum Ret {
}

impl Tui {
pub fn new(targets: Vec<Target>, term_size: Rect) -> Tui {
pub fn new(targets: Vec<Target>, term_size: Rect, matcher: Matcher) -> Tui {
let mut tui = Tui {
targets,
list_height: Tui::calc_list_height(term_size.height),
matcher,
..Default::default()
};
tui.update_filter();
Expand Down Expand Up @@ -128,13 +131,12 @@ impl Tui {
.iter()
.enumerate()
.filter_map(|(i, t)| {
t.name.find(s).map(|pos| {
let match_indices = (pos..pos + s.len()).collect();
FilteredTarget {
self.matcher
.match_indices(&t.name, s)
.map(|indices| FilteredTarget {
index: i,
match_indices,
}
})
match_indices: indices,
})
})
.collect();
self.cursor = 0;
Expand Down

0 comments on commit fa3a5d7

Please sign in to comment.