From 13c1ea476857d1184194cb15aeb7c26b950f3d84 Mon Sep 17 00:00:00 2001 From: sxyazi Date: Wed, 21 Aug 2024 00:43:08 +0800 Subject: [PATCH] feat: add `ext()` method to `Url` userdata --- Cargo.lock | 83 ++++++++++++++------------ Cargo.toml | 4 +- yazi-boot/Cargo.toml | 2 +- yazi-cli/Cargo.toml | 2 +- yazi-config/src/pattern.rs | 4 +- yazi-config/src/plugin/fetcher.rs | 6 +- yazi-config/src/plugin/plugin.rs | 4 +- yazi-config/src/plugin/preloader.rs | 6 +- yazi-config/src/theme/filetype.rs | 4 +- yazi-core/src/manager/commands/peek.rs | 7 +-- yazi-core/src/tasks/preload.rs | 8 ++- yazi-fm/src/lives/file.rs | 7 ++- yazi-plugin/src/url/url.rs | 3 + 13 files changed, 76 insertions(+), 64 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index ef4a093db..de50e36b4 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -153,7 +153,7 @@ checksum = "0ae92a5119aa49cdbcf6b9f893fe4e1d98b04ccbf82ee0584ad948a44a734dea" dependencies = [ "proc-macro2", "quote", - "syn 2.0.72", + "syn 2.0.74", ] [[package]] @@ -324,9 +324,9 @@ checksum = "8f1fe948ff07f4bd06c30984e69f5b4899c516a3ef74f34df92a2df2ab535495" [[package]] name = "bytes" -version = "1.6.1" +version = "1.7.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "a12916984aab3fa6e39d655a33e09c0071eb36d6ab3aea5c2d78551f1df6d952" +checksum = "8318a53db07bb3f8dca91a600466bdb3f2eaadeedfdbcf02e1accbad9271ba50" [[package]] name = "cassowary" @@ -345,12 +345,13 @@ dependencies = [ [[package]] name = "cc" -version = "1.1.7" +version = "1.1.13" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "26a5c3fd7bfa1ce3897a3a3501d362b2d87b7f2583ebcb4a949ec25911025cbc" +checksum = "72db2f7947ecee9b03b510377e8bb9077afa27176fdbff55c51027e976fdcc48" dependencies = [ "jobserver", "libc", + "shlex", ] [[package]] @@ -405,9 +406,9 @@ dependencies = [ [[package]] name = "clap_complete" -version = "4.5.16" +version = "4.5.19" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "9c677cd0126f3026d8b093fa29eae5d812fde5c05bc66dbb29d0374eea95113a" +checksum = "7eddf1c00919f37952199f7dbc834789cd33356ed10278ee40c8572b8fb88cf2" dependencies = [ "clap", ] @@ -441,7 +442,7 @@ dependencies = [ "heck", "proc-macro2", "quote", - "syn 2.0.72", + "syn 2.0.74", ] [[package]] @@ -618,7 +619,7 @@ dependencies = [ "proc-macro2", "quote", "strsim", - "syn 2.0.72", + "syn 2.0.74", ] [[package]] @@ -629,7 +630,7 @@ checksum = "733cabb43482b1a1b53eee8583c2b9e8684d592215ea83efd305dd31bc2f0178" dependencies = [ "darling_core", "quote", - "syn 2.0.72", + "syn 2.0.74", ] [[package]] @@ -659,7 +660,7 @@ dependencies = [ "darling", "proc-macro2", "quote", - "syn 2.0.72", + "syn 2.0.74", ] [[package]] @@ -669,7 +670,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "206868b8242f27cecce124c19fd88157fbd0dd334df2587f36417bafbc85097b" dependencies = [ "derive_builder_core", - "syn 2.0.72", + "syn 2.0.74", ] [[package]] @@ -915,7 +916,7 @@ checksum = "87750cf4b7a4c0625b1529e4c543c2182106e4dedc60a2a6455e00d212c489ac" dependencies = [ "proc-macro2", "quote", - "syn 2.0.72", + "syn 2.0.74", ] [[package]] @@ -1182,7 +1183,7 @@ checksum = "c34819042dc3d3971c46c2190835914dfbe0c3c13f61449b2997f4e9722dfa60" dependencies = [ "proc-macro2", "quote", - "syn 2.0.72", + "syn 2.0.74", ] [[package]] @@ -1282,9 +1283,9 @@ checksum = "03087c2bad5e1034e8cace5926dec053fb3790248370865f5117a7d0213354c8" [[package]] name = "libc" -version = "0.2.156" +version = "0.2.158" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "a5f43f184355eefb8d17fc948dbecf6c13be3c141f20d834ae842193a448c72a" +checksum = "d8adc4bb1803a324070e64a98ae98f38934d91957a99cfb3a43dcbc01bc56439" [[package]] name = "libfuzzer-sys" @@ -1427,9 +1428,9 @@ dependencies = [ [[package]] name = "mio" -version = "1.0.1" +version = "1.0.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "4569e456d394deccd22ce1c1913e6ea0e54519f577285001215d33557431afe4" +checksum = "80e04d1dcff3aae0704555fe5fee3bcfaf3d1fdf8a7e521d5b9d2b42acb52cec" dependencies = [ "hermit-abi", "libc", @@ -1480,7 +1481,7 @@ dependencies = [ "proc-macro2", "quote", "regex", - "syn 2.0.72", + "syn 2.0.74", ] [[package]] @@ -1573,7 +1574,7 @@ checksum = "ed3955f1a9c7c0c15e092f9c887db08b1fc683305fdf6eb6684f22555355e202" dependencies = [ "proc-macro2", "quote", - "syn 2.0.72", + "syn 2.0.74", ] [[package]] @@ -1853,7 +1854,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "8021cf59c8ec9c432cfc2526ac6b8aa508ecaf29cd415f271b8406c1b851c3fd" dependencies = [ "quote", - "syn 2.0.72", + "syn 2.0.74", ] [[package]] @@ -2155,7 +2156,7 @@ checksum = "24008e81ff7613ed8e5ba0cfaf24e2c2f1e5b8a0495711e44fcd4882fca62bcf" dependencies = [ "proc-macro2", "quote", - "syn 2.0.72", + "syn 2.0.74", ] [[package]] @@ -2194,6 +2195,12 @@ version = "1.1.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "24188a676b6ae68c3b2cb3a01be17fbf7240ce009799bb56d5b1409051e78fde" +[[package]] +name = "shlex" +version = "1.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0fda2ff0d084019ba4d7c6f371c95d8fd75ce3524c3cb8fb653a3023f6323e64" + [[package]] name = "signal-hook" version = "0.3.17" @@ -2298,7 +2305,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "d904e7009df136af5297832a3ace3370cd14ff1546a232f4f185036c2736fcac" dependencies = [ "quote", - "syn 2.0.72", + "syn 2.0.74", ] [[package]] @@ -2332,7 +2339,7 @@ dependencies = [ "proc-macro2", "quote", "rustversion", - "syn 2.0.72", + "syn 2.0.74", ] [[package]] @@ -2348,9 +2355,9 @@ dependencies = [ [[package]] name = "syn" -version = "2.0.72" +version = "2.0.74" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "dc4b9b9bf2add8093d3f2c0204471e951b2285580335de42f9d2534f3ae7a8af" +checksum = "1fceb41e3d546d0bd83421d3409b1460cc7444cd389341a4c880fe7a042cb3d7" dependencies = [ "proc-macro2", "quote", @@ -2414,7 +2421,7 @@ checksum = "a4558b58466b9ad7ca0f102865eccc95938dca1a74a856f2b57b6629050da261" dependencies = [ "proc-macro2", "quote", - "syn 2.0.72", + "syn 2.0.74", ] [[package]] @@ -2508,14 +2515,14 @@ checksum = "1f3ccbac311fea05f86f61904b462b55fb3df8837a366dfc601a0161d0532f20" [[package]] name = "tokio" -version = "1.39.2" +version = "1.39.3" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "daa4fb1bc778bd6f04cbfc4bb2d06a7396a8f299dc33ea1900cedaa316f467b1" +checksum = "9babc99b9923bfa4804bd74722ff02c0381021eafa4db9949217e3be8e84fff5" dependencies = [ "backtrace", "bytes", "libc", - "mio 1.0.1", + "mio 1.0.2", "parking_lot", "pin-project-lite", "signal-hook-registry", @@ -2532,7 +2539,7 @@ checksum = "693d596312e88961bc67d7f1f97af8a70227d9f90c31bba5806eec004978d752" dependencies = [ "proc-macro2", "quote", - "syn 2.0.72", + "syn 2.0.74", ] [[package]] @@ -2625,7 +2632,7 @@ checksum = "34704c8d6ebcbc939824180af020566b01a7c01f80641264eba0999f6c2b6be7" dependencies = [ "proc-macro2", "quote", - "syn 2.0.72", + "syn 2.0.74", ] [[package]] @@ -2807,7 +2814,7 @@ dependencies = [ "proc-macro-error", "proc-macro2", "quote", - "syn 2.0.72", + "syn 2.0.74", ] [[package]] @@ -2904,7 +2911,7 @@ dependencies = [ "once_cell", "proc-macro2", "quote", - "syn 2.0.72", + "syn 2.0.74", "wasm-bindgen-shared", ] @@ -2926,7 +2933,7 @@ checksum = "e94f17b526d0a461a191c78ea52bbce64071ed5c04c9ffe424dcb38f74171bb7" dependencies = [ "proc-macro2", "quote", - "syn 2.0.72", + "syn 2.0.74", "wasm-bindgen-backend", "wasm-bindgen-shared", ] @@ -3025,7 +3032,7 @@ checksum = "f6fc35f58ecd95a9b71c4f2329b911016e6bec66b3f2e6a4aad86bd2e99e2f9b" dependencies = [ "proc-macro2", "quote", - "syn 2.0.72", + "syn 2.0.74", ] [[package]] @@ -3036,7 +3043,7 @@ checksum = "08990546bf4edef8f431fa6326e032865f27138718c587dc21bc0265bbcb57cc" dependencies = [ "proc-macro2", "quote", - "syn 2.0.72", + "syn 2.0.74", ] [[package]] @@ -3490,7 +3497,7 @@ checksum = "fa4f8080344d4671fb4e831a13ad1e68092748387dfc4f55e356242fae12ce3e" dependencies = [ "proc-macro2", "quote", - "syn 2.0.72", + "syn 2.0.74", ] [[package]] diff --git a/Cargo.toml b/Cargo.toml index 010bc51ef..39ac725bd 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -20,7 +20,7 @@ crossterm = { version = "0.27.0", features = [ "event-stream" ] } dirs = "5.0.1" futures = "0.3.30" globset = "0.4.14" -libc = "0.2.156" +libc = "0.2.158" md-5 = "0.10.6" mlua = { version = "0.9.9", features = [ "lua54", "serialize", "macros", "async" ] } parking_lot = "0.12.3" @@ -30,7 +30,7 @@ scopeguard = "1.2.0" serde = { version = "1.0.208", features = [ "derive" ] } serde_json = "1.0.125" shell-words = "1.1.0" -tokio = { version = "1.39.2", features = [ "full" ] } +tokio = { version = "1.39.3", features = [ "full" ] } tokio-stream = "0.1.15" tokio-util = "0.7.11" tracing = { version = "0.1.40", features = [ "max_level_debug", "release_max_level_warn" ] } diff --git a/yazi-boot/Cargo.toml b/yazi-boot/Cargo.toml index d48886d82..85e4f402f 100644 --- a/yazi-boot/Cargo.toml +++ b/yazi-boot/Cargo.toml @@ -20,7 +20,7 @@ serde = { workspace = true } [build-dependencies] clap = { workspace = true } -clap_complete = "4.5.16" +clap_complete = "4.5.19" clap_complete_fig = "4.5.2" clap_complete_nushell = "4.5.3" vergen-gitcl = { version = "1.0.0", features = [ "build" ] } diff --git a/yazi-cli/Cargo.toml b/yazi-cli/Cargo.toml index dccf0eafb..dbaab9f8a 100644 --- a/yazi-cli/Cargo.toml +++ b/yazi-cli/Cargo.toml @@ -28,7 +28,7 @@ yazi-shared = { path = "../yazi-shared", version = "0.3.1" } # External build dependencies anyhow = { workspace = true } clap = { workspace = true } -clap_complete = "4.5.16" +clap_complete = "4.5.19" clap_complete_fig = "4.5.2" clap_complete_nushell = "4.5.3" serde_json = { workspace = true } diff --git a/yazi-config/src/pattern.rs b/yazi-config/src/pattern.rs index d92c6ee65..3ca5823a2 100644 --- a/yazi-config/src/pattern.rs +++ b/yazi-config/src/pattern.rs @@ -13,8 +13,8 @@ pub struct Pattern { impl Pattern { #[inline] - pub fn match_mime(&self, str: impl AsRef) -> bool { - self.is_star || self.inner.is_match(str.as_ref()) + pub fn match_mime(&self, mime: impl AsRef) -> bool { + self.is_star || (!mime.as_ref().is_empty() && self.inner.is_match(mime.as_ref())) } #[inline] diff --git a/yazi-config/src/plugin/fetcher.rs b/yazi-config/src/plugin/fetcher.rs index 3890a5ca1..57bf3bc99 100644 --- a/yazi-config/src/plugin/fetcher.rs +++ b/yazi-config/src/plugin/fetcher.rs @@ -22,10 +22,10 @@ pub struct Fetcher { impl Fetcher { #[inline] - pub fn matches(&self, path: &Path, mime: Option<&str>, f: impl Fn(&str) -> bool + Copy) -> bool { + pub fn matches(&self, path: &Path, mime: &str, f: impl Fn(&str) -> bool + Copy) -> bool { self.if_.as_ref().and_then(|c| c.eval(f)) != Some(false) - && (self.mime.as_ref().zip(mime).map_or(false, |(p, m)| p.match_mime(m)) - || self.name.as_ref().is_some_and(|p| p.match_path(path, mime == Some(MIME_DIR)))) + && (self.mime.as_ref().is_some_and(|p| p.match_mime(mime)) + || self.name.as_ref().is_some_and(|p| p.match_path(path, mime == MIME_DIR))) } } diff --git a/yazi-config/src/plugin/plugin.rs b/yazi-config/src/plugin/plugin.rs index 0c1d97d34..5ab3572c9 100644 --- a/yazi-config/src/plugin/plugin.rs +++ b/yazi-config/src/plugin/plugin.rs @@ -16,7 +16,7 @@ impl Plugin { pub fn fetchers<'a>( &'a self, path: &'a Path, - mime: Option<&'a str>, + mime: &'a str, factor: impl Fn(&str) -> bool + Copy, ) -> impl Iterator { let mut seen = HashSet::new(); @@ -32,7 +32,7 @@ impl Plugin { pub fn preloaders<'a>( &'a self, path: &'a Path, - mime: Option<&'a str>, + mime: &'a str, ) -> impl Iterator { let mut next = true; self.preloaders.iter().filter(move |&p| { diff --git a/yazi-config/src/plugin/preloader.rs b/yazi-config/src/plugin/preloader.rs index dcfe4d326..8453cdc46 100644 --- a/yazi-config/src/plugin/preloader.rs +++ b/yazi-config/src/plugin/preloader.rs @@ -21,9 +21,9 @@ pub struct Preloader { impl Preloader { #[inline] - pub fn matches(&self, path: &Path, mime: Option<&str>) -> bool { - self.mime.as_ref().zip(mime).map_or(false, |(p, m)| p.match_mime(m)) - || self.name.as_ref().is_some_and(|p| p.match_path(path, mime == Some(MIME_DIR))) + pub fn matches(&self, path: &Path, mime: &str) -> bool { + self.mime.as_ref().is_some_and(|p| p.match_mime(mime)) + || self.name.as_ref().is_some_and(|p| p.match_path(path, mime == MIME_DIR)) } } diff --git a/yazi-config/src/theme/filetype.rs b/yazi-config/src/theme/filetype.rs index 31528caf5..184e30a6b 100644 --- a/yazi-config/src/theme/filetype.rs +++ b/yazi-config/src/theme/filetype.rs @@ -12,12 +12,12 @@ pub struct Filetype { } impl Filetype { - pub fn matches(&self, file: &File, mime: Option<&str>) -> bool { + pub fn matches(&self, file: &File, mime: &str) -> bool { if !self.is.check(&file.cha) { return false; } - self.mime.as_ref().zip(mime).map_or(false, |(p, m)| p.match_mime(m)) + self.mime.as_ref().is_some_and(|p| p.match_mime(mime)) || self.name.as_ref().is_some_and(|n| n.match_path(&file.url, file.is_dir())) } } diff --git a/yazi-core/src/manager/commands/peek.rs b/yazi-core/src/manager/commands/peek.rs index daf62492a..0d8b71e21 100644 --- a/yazi-core/src/manager/commands/peek.rs +++ b/yazi-core/src/manager/commands/peek.rs @@ -55,10 +55,7 @@ impl Manager { return; } - if let Some(mime) = self.mimetype.get(&hovered.url).cloned() { - self.active_mut().preview.go(hovered, &mime, opt.force); - } else { - render!(self.active_mut().preview.reset()); - } + let mime = self.mimetype.get(&hovered.url).cloned().unwrap_or_default(); + self.active_mut().preview.go(hovered, &mime, opt.force); } } diff --git a/yazi-core/src/tasks/preload.rs b/yazi-core/src/tasks/preload.rs index c430719b4..d7d020c5f 100644 --- a/yazi-core/src/tasks/preload.rs +++ b/yazi-core/src/tasks/preload.rs @@ -11,9 +11,10 @@ impl Tasks { let mut loaded = self.scheduler.prework.loaded.lock(); let mut tasks: [Vec<_>; MAX_PREWORKERS as usize] = Default::default(); for f in paged { - let mime = if f.is_dir() { Some(MIME_DIR) } else { mimetype.get(&f.url).map(|s| &**s) }; + let mime = + if f.is_dir() { MIME_DIR } else { mimetype.get(&f.url).map(|s| &**s).unwrap_or_default() }; let factors = |s: &str| match s { - "mime" => mime.is_some(), + "mime" => !mime.is_empty(), _ => false, }; @@ -38,7 +39,8 @@ impl Tasks { pub fn preload_paged(&self, paged: &[File], mimetype: &HashMap) { let mut loaded = self.scheduler.prework.loaded.lock(); for f in paged { - let mime = if f.is_dir() { Some(MIME_DIR) } else { mimetype.get(&f.url).map(|s| &**s) }; + let mime = + if f.is_dir() { MIME_DIR } else { mimetype.get(&f.url).map(|s| &**s).unwrap_or_default() }; for p in PLUGIN.preloaders(&f.url, mime) { match loaded.get_mut(&f.url) { Some(n) if *n & (1 << p.idx) != 0 => continue, diff --git a/yazi-fm/src/lives/file.rs b/yazi-fm/src/lives/file.rs index 5a69b84fb..9962f8fa2 100644 --- a/yazi-fm/src/lives/file.rs +++ b/yazi-fm/src/lives/file.rs @@ -56,8 +56,11 @@ impl File { }); reg.add_method("style", |lua, me, ()| { let cx = lua.named_registry_value::("cx")?; - let mime = - if me.is_dir() { Some(MIME_DIR) } else { cx.manager.mimetype.get(&me.url).map(|x| &**x) }; + let mime = if me.is_dir() { + MIME_DIR + } else { + cx.manager.mimetype.get(&me.url).map(|x| &**x).unwrap_or_default() + }; Ok(THEME.filetypes.iter().find(|&x| x.matches(me, mime)).map(|x| Style::from(x.style))) }); diff --git a/yazi-plugin/src/url/url.rs b/yazi-plugin/src/url/url.rs index b0749c1e4..9fde4a553 100644 --- a/yazi-plugin/src/url/url.rs +++ b/yazi-plugin/src/url/url.rs @@ -20,6 +20,9 @@ impl Url { reg.add_method("stem", |lua, me, ()| { me.file_stem().map(|s| lua.create_string(s.as_encoded_bytes())).transpose() }); + reg.add_method("ext", |lua, me, ()| { + me.extension().map(|s| lua.create_string(s.as_encoded_bytes())).transpose() + }); reg.add_method("join", |lua, me, other: Value| { Ok(match other { Value::String(s) => Self::cast(lua, me.join(s.to_str()?)),