Skip to content

Commit

Permalink
pulling my hair out
Browse files Browse the repository at this point in the history
  • Loading branch information
alexpasmantier committed Sep 26, 2024
1 parent a580b6f commit 4e4f397
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 8 deletions.
2 changes: 2 additions & 0 deletions src/action.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,4 +39,6 @@ pub enum Action {
Help,
Error(String),
NoOp,
// channel actions
SyncFinderResults,
}
9 changes: 9 additions & 0 deletions src/components/channels.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
use color_eyre::Result;
use tokio::sync::mpsc::UnboundedSender;

use crate::action::Action;
use crate::cli::UnitTvChannel;
use crate::components::finders::Entry;
use crate::components::pickers::{self, Picker};
Expand All @@ -11,6 +13,13 @@ pub enum TvChannel {
}

impl TvChannel {
pub fn register_action_handler(&mut self, tx: UnboundedSender<Action>) -> Result<()> {
match self {
TvChannel::Files(picker) => picker.register_action_handler(tx),
_ => Ok(()),
}
}

pub async fn load_entries(&mut self, pattern: &str) -> Result<()> {
match self {
TvChannel::Env(picker) => picker.load_entries(pattern).await,
Expand Down
27 changes: 20 additions & 7 deletions src/components/finders/files.rs
Original file line number Diff line number Diff line change
@@ -1,41 +1,54 @@
use color_eyre::Result;
use futures::{stream, StreamExt};
use nucleo::{Config, Nucleo};
use nucleo::{Config, Item, Nucleo};
use std::{
collections::HashMap,
path::{Path, PathBuf},
sync::Arc,
};

use ignore::{types::TypesBuilder, WalkBuilder};
use tokio::sync::mpsc;
use tokio::sync::mpsc::{self, UnboundedSender};
use tracing::info;

use crate::{
action::Action,
components::finders::{Entry, Finder},
config::default_num_threads,
};

pub struct FileFinder {
current_directory: PathBuf,
files: Vec<PathBuf>,
matcher: Nucleo,
matcher: Option<Nucleo<_>>,
cache: HashMap<String, Vec<Entry>>,
}

struct MatchItem {
path: PathBuf,
}

impl FileFinder {
pub async fn new() -> Self {
let matcher_config = Config::DEFAULT.match_paths();
let files = load_files(&std::env::current_dir().unwrap()).await;
FileFinder {
current_directory: std::env::current_dir().unwrap(),
files,
matcher: Nucleo::new(matcher_config, notify, num_threads, columns),
matcher: None,
cache: HashMap::new(),
}
}

pub fn register_action_handler(&mut self, tx: UnboundedSender<Action>) -> Result<()> {
let matcher_config = Config::DEFAULT.match_paths();
let notify = || {
tx.send(Action::SyncFinderResults).unwrap();
};
self.matcher = Some(Nucleo::new(matcher_config, Arc::new(notify), None, 1));
Ok(())
}
}

/// TODO: we might need to tweak this a bit -> read the paper associated to the matcher to get
/// a sense of what the threshold should be
const FUZZY_THRESHOLD: i64 = 2;

impl Finder for FileFinder {
Expand Down
10 changes: 9 additions & 1 deletion src/components/pickers/files.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
use color_eyre::Result;
use tokio::sync::mpsc::UnboundedSender;
use tokio_stream::StreamExt;

use crate::action::Action;
use crate::components::finders::{self, Finder};
use crate::components::pickers::Picker;
use crate::components::previewers::{self, Previewer};
Expand All @@ -18,14 +21,19 @@ impl FilePicker {
previewer: previewers::FilePreviewer::new(),
}
}

pub fn register_action_handler(&mut self, tx: UnboundedSender<Action>) -> Result<()> {
self.finder.register_action_handler(tx)
}
}

impl Picker for FilePicker {
type F = finders::FileFinder;
type P = previewers::FilePreviewer;

async fn load_entries(&mut self, pattern: &str) -> Result<()> {
self.entries = self.finder.find(pattern).collect::<Vec<finders::Entry>>();
// this is probably not the best way to do this
self.entries = self.finder.find(pattern).await.collect().await;
self.entries.sort_by_key(|e| -e.score);
Ok(())
}
Expand Down
4 changes: 4 additions & 0 deletions src/components/television.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ pub struct Television {
action_tx: Option<UnboundedSender<Action>>,
config: Config,
channel: TvChannel,
channel_rx: Option<tokio::sync::mpsc::UnboundedReceiver<>
current_pattern: String,
current_pane: Pane,
input: Input,
Expand Down Expand Up @@ -276,6 +277,9 @@ impl Component for Television {
_ => {}
}
}
Action::SyncFinderResults => {
self.sync_channel(&self.current_pattern).await?;
}
Action::SelectNextEntry => self.select_next_entry(),
Action::SelectPrevEntry => self.select_prev_entry(),
_ => {}
Expand Down

0 comments on commit 4e4f397

Please sign in to comment.