Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: true custom mime fetcher support - using user rules even during open or in watcher #1976

Merged
merged 1 commit into from
Nov 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions yazi-config/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ ratatui = { workspace = true }
regex = { workspace = true }
serde = { workspace = true }
toml = { version = "0.8.19", features = [ "preserve_order" ] }
tracing = { workspace = true }
validator = { version = "0.19.0", features = [ "derive" ] }

[target.'cfg(target_os = "macos")'.dependencies]
Expand Down
4 changes: 2 additions & 2 deletions yazi-config/preset/keymap.toml
Original file line number Diff line number Diff line change
Expand Up @@ -79,8 +79,8 @@ keymap = [
{ on = ";", run = "shell --interactive", desc = "Run a shell command" },
{ on = ":", run = "shell --block --interactive", desc = "Run a shell command (block until finishes)" },
{ on = ".", run = "hidden toggle", desc = "Toggle the visibility of hidden files" },
{ on = "s", run = "search fd", desc = "Search files by name via fd" },
{ on = "S", run = "search rg", desc = "Search files by content via ripgrep" },
{ on = "s", run = "search --via=fd", desc = "Search files by name via fd" },
{ on = "S", run = "search --via=rg", desc = "Search files by content via ripgrep" },
{ on = "<C-s>", run = "escape --search", desc = "Cancel the ongoing search" },
{ on = "z", run = "plugin zoxide", desc = "Jump to a directory via zoxide" },
{ on = "Z", run = "plugin fzf", desc = "Jump to a file/directory via fzf" },
Expand Down
23 changes: 23 additions & 0 deletions yazi-config/src/plugin/plugin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ use std::{collections::HashSet, path::Path, str::FromStr};

use anyhow::Context;
use serde::{Deserialize, Deserializer};
use tracing::warn;
use yazi_shared::fs::File;

use super::{Fetcher, Preloader, Previewer, Spotter};
use crate::{Preset, plugin::MAX_PREWORKERS};
Expand Down Expand Up @@ -30,6 +32,27 @@ impl Plugin {
})
}

pub fn mime_fetchers(&self, files: Vec<File>) -> impl Iterator<Item = (&Fetcher, Vec<File>)> {
let mut tasks: [Vec<_>; MAX_PREWORKERS as usize] = Default::default();
for f in files {
let factors = |s: &str| match s {
"dummy" => f.cha.is_dummy(),
_ => false,
};

let found = self.fetchers.iter().find(|&g| g.id == "mime" && g.matches(&f.url, "", factors));
if let Some(g) = found {
tasks[g.idx as usize].push(f);
} else {
warn!("No mime fetcher for {f:?}");
}
}

tasks.into_iter().enumerate().filter_map(|(i, tasks)| {
if tasks.is_empty() { None } else { Some((&self.fetchers[i], tasks)) }
})
}

#[inline]
pub fn fetchers_mask(&self) -> u32 {
self.fetchers.iter().fold(0, |n, f| if f.mime.is_some() { n } else { n | 1 << f.idx as u32 })
Expand Down
10 changes: 6 additions & 4 deletions yazi-core/src/manager/commands/open.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@ use std::{borrow::Cow, ffi::OsString};

use tracing::error;
use yazi_boot::ARGS;
use yazi_config::{OPEN, popup::PickCfg};
use yazi_config::{OPEN, PLUGIN, popup::PickCfg};
use yazi_fs::Folder;
use yazi_macro::emit;
use yazi_plugin::isolate;
use yazi_proxy::{ManagerProxy, TasksProxy, options::OpenDoOpt};
use yazi_shared::{MIME_DIR, event::{Cmd, CmdCow, EventQuit}, fs::{File, Url}};
use yazi_shared::{MIME_DIR, event::{CmdCow, EventQuit}, fs::{File, Url}};

use crate::{manager::Manager, tasks::Tasks};

Expand Down Expand Up @@ -65,8 +65,10 @@ impl Manager {
}

done.extend(files.iter().map(|f| (f.url_owned(), String::new())));
if let Err(e) = isolate::fetch(Cmd::new("mime").into(), files).await {
error!("Fetch `mime` failed in opening: {e}");
for (fetcher, files) in PLUGIN.mime_fetchers(files) {
if let Err(e) = isolate::fetch(CmdCow::from(&fetcher.run), files).await {
error!("Fetch mime failed on opening: {e}");
}
}

ManagerProxy::open_do(OpenDoOpt { hovered, targets: done, interactive: opt.interactive });
Expand Down
9 changes: 6 additions & 3 deletions yazi-core/src/manager/watcher.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,11 @@ use parking_lot::RwLock;
use tokio::{fs, pin, sync::{mpsc::{self, UnboundedReceiver}, watch}};
use tokio_stream::{StreamExt, wrappers::UnboundedReceiverStream};
use tracing::error;
use yazi_config::PLUGIN;
use yazi_fs::{Files, Folder};
use yazi_plugin::isolate;
use yazi_proxy::WATCHER;
use yazi_shared::{RoCell, event::Cmd, fs::{Cha, File, FilesOp, Url, realname_unchecked}};
use yazi_shared::{RoCell, event::CmdCow, fs::{Cha, File, FilesOp, Url, realname_unchecked}};

use super::Linked;

Expand Down Expand Up @@ -151,8 +152,10 @@ impl Watcher {
}

FilesOp::mutate(ops);
if let Err(e) = isolate::fetch(Cmd::new("mime").into(), reload).await {
error!("Fetch `mime` failed in watcher: {e}");
for (fetcher, files) in PLUGIN.mime_fetchers(reload) {
if let Err(e) = isolate::fetch(CmdCow::from(&fetcher.run), files).await {
error!("Fetch mime failed in watcher: {e}");
}
}
}
}
Expand Down
10 changes: 5 additions & 5 deletions yazi-core/src/tasks/preload.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,13 @@ impl Tasks {
_ => false,
};

for p in PLUGIN.fetchers(&f.url, mime, factors) {
for g in PLUGIN.fetchers(&f.url, mime, factors) {
match loaded.get_mut(&f.url) {
Some(n) if *n & (1 << p.idx) != 0 => continue,
Some(n) => *n |= 1 << p.idx,
None => _ = loaded.insert(f.url_owned(), 1 << p.idx),
Some(n) if *n & (1 << g.idx) != 0 => continue,
Some(n) => *n |= 1 << g.idx,
None => _ = loaded.insert(f.url_owned(), 1 << g.idx),
}
tasks[p.idx as usize].push(f.clone());
tasks[g.idx as usize].push(f.clone());
}
}

Expand Down
Loading