diff --git a/yazi-plugin/preset/plugins/mime.lua b/yazi-plugin/preset/plugins/mime.lua index bfebcd34..2dcc9efd 100644 --- a/yazi-plugin/preset/plugins/mime.lua +++ b/yazi-plugin/preset/plugins/mime.lua @@ -1,5 +1,12 @@ local M = {} +local function match_mimetype(s) + local type, subtype = s:match("([-a-z]+/)([+-.a-zA-Z0-9]+)%s*$") + if string.find("application/audio/biosig/chemical/font/image/inode/message/model/rinex/text/vector/video/x-epoc/", type, 1, true) then + return type .. subtype + end +end + function M:preload() local urls = {} for _, file in ipairs(self.files) do @@ -14,7 +21,7 @@ function M:preload() local mimes, last = {}, ya.time() local flush = function(force) - if not force and ya.time() - last < 0.1 then + if not force and ya.time() - last < 0.3 then return end if next(mimes) then @@ -23,9 +30,9 @@ function M:preload() end end - local i, j = 1, 0 + local i, j, mime = 1, 0, nil repeat - local next, event = child:read_line_with { timeout = 300 } + local line, event = child:read_line_with { timeout = 300 } if event == 3 then flush(true) goto continue @@ -33,9 +40,11 @@ function M:preload() break end - next = next:gsub("[\r\n]+$", "") - if ya.mime_valid(next) then - j, mimes[urls[i]] = j + 1, next + mime = match_mimetype(line) + if mime and string.find(line, mime, 1, true) ~= 1 then + goto continue + elseif mime then + j, mimes[urls[i]] = j + 1, mime flush(false) end diff --git a/yazi-plugin/src/utils/text.rs b/yazi-plugin/src/utils/text.rs index a914e501..55b49738 100644 --- a/yazi-plugin/src/utils/text.rs +++ b/yazi-plugin/src/utils/text.rs @@ -2,7 +2,6 @@ use std::ops::ControlFlow; use mlua::{Lua, Table}; use unicode_width::{UnicodeWidthChar, UnicodeWidthStr}; -use yazi_shared::mime_valid; use super::Utils; @@ -41,11 +40,6 @@ impl Utils { })?, )?; - ya.set( - "mime_valid", - lua.create_function(|_, mime: mlua::String| Ok(mime_valid(mime.as_bytes())))?, - )?; - Ok(()) } } diff --git a/yazi-shared/src/chars.rs b/yazi-shared/src/chars.rs index 4478d8ec..83fa989b 100644 --- a/yazi-shared/src/chars.rs +++ b/yazi-shared/src/chars.rs @@ -1,3 +1,5 @@ +pub const MIME_DIR: &str = "inode/directory"; + #[derive(Clone, Copy, PartialEq, Eq)] pub enum CharKind { Space, diff --git a/yazi-shared/src/lib.rs b/yazi-shared/src/lib.rs index e655e865..f0d3fa07 100644 --- a/yazi-shared/src/lib.rs +++ b/yazi-shared/src/lib.rs @@ -9,7 +9,6 @@ mod errors; pub mod event; pub mod fs; mod layer; -mod mime; mod natsort; mod number; mod os; @@ -25,7 +24,6 @@ pub use defer::*; pub use env::*; pub use errors::*; pub use layer::*; -pub use mime::*; pub use natsort::*; pub use number::*; pub use os::*; diff --git a/yazi-shared/src/mime.rs b/yazi-shared/src/mime.rs deleted file mode 100644 index 022c1d3e..00000000 --- a/yazi-shared/src/mime.rs +++ /dev/null @@ -1,23 +0,0 @@ -pub const MIME_DIR: &str = "inode/directory"; - -pub fn mime_valid(b: &[u8]) -> bool { - let parts = b.split(|&b| b == b'/').collect::>(); - if parts.len() != 2 || parts[1].is_empty() { - return false; - } - - matches!( - parts[0], - b"application" - | b"audio" - | b"example" - | b"font" - | b"image" - | b"inode" - | b"message" - | b"model" - | b"multipart" - | b"text" - | b"video" - ) -}