From 8be63811a440cb10aa0e7823f7bf4ff505663d79 Mon Sep 17 00:00:00 2001 From: sxyazi Date: Sat, 22 Jun 2024 18:55:18 +0800 Subject: [PATCH] Refactor the code --- yazi-core/src/folder/filter.rs | 12 ++++++------ yazi-fm/src/lives/filter.rs | 4 ++-- yazi-plugin/preset/components/header.lua | 24 +++++++++++++++--------- 3 files changed, 23 insertions(+), 17 deletions(-) diff --git a/yazi-core/src/folder/filter.rs b/yazi-core/src/folder/filter.rs index 4dd4e912..290b0067 100644 --- a/yazi-core/src/folder/filter.rs +++ b/yazi-core/src/folder/filter.rs @@ -1,4 +1,4 @@ -use std::{ffi::OsStr, fmt::{Display, Error}, ops::Range}; +use std::{ffi::OsStr, fmt::Display, ops::Range}; use anyhow::Result; use regex::bytes::{Regex, RegexBuilder}; @@ -9,10 +9,6 @@ pub struct Filter { regex: Regex, } -impl PartialEq for Filter { - fn eq(&self, other: &Self) -> bool { self.raw == other.raw } -} - impl Filter { pub fn new(s: &str, case: FilterCase) -> Result { let regex = match case { @@ -35,8 +31,12 @@ impl Filter { } } +impl PartialEq for Filter { + fn eq(&self, other: &Self) -> bool { self.raw == other.raw } +} + impl Display for Filter { - fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> Result<(), Error> { f.write_str(&self.raw) } + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { f.write_str(&self.raw) } } #[derive(Default, PartialEq, Eq)] diff --git a/yazi-fm/src/lives/filter.rs b/yazi-fm/src/lives/filter.rs index 9e1163ce..79c9119c 100644 --- a/yazi-fm/src/lives/filter.rs +++ b/yazi-fm/src/lives/filter.rs @@ -1,6 +1,6 @@ use std::ops::Deref; -use mlua::{AnyUserData, Lua, UserDataFields}; +use mlua::{AnyUserData, Lua, MetaMethod, UserDataMethods}; use super::SCOPE; @@ -22,7 +22,7 @@ impl Filter { pub(super) fn register(lua: &Lua) -> mlua::Result<()> { lua.register_userdata_type::(|reg| { - reg.add_field_method_get("to_string", |_, me| Ok(me.to_string())); + reg.add_meta_method(MetaMethod::ToString, |_, me, ()| Ok(me.to_string())); }) } } diff --git a/yazi-plugin/preset/components/header.lua b/yazi-plugin/preset/components/header.lua index c317da0f..90a60def 100644 --- a/yazi-plugin/preset/components/header.lua +++ b/yazi-plugin/preset/components/header.lua @@ -3,18 +3,24 @@ Header = { } function Header:cwd(max) - local cwd = cx.active.current.cwd - local readable = ya.readable_path(tostring(cwd)) - local files = cx.active.current.files - - local search = cwd.is_search and string.format(" (search: %s)", cwd:frag()) or "" - local filter = files.filter and string.format(" (filter: %s)", files.filter.to_string) or "" - - local text = string.format("%s%s%s", readable, search, filter) - + local text = ya.readable_path(tostring(cx.active.current.cwd)) .. self:flags() return ui.Span(ya.truncate(text, { max = max, rtl = true })):style(THEME.manager.cwd) end +function Header:flags() + local cwd = cx.active.current.cwd + local filter = cx.active.current.files.filter + + local s = cwd.is_search and string.format(" (search: %s", cwd:frag()) or "" + if not filter then + return s == "" and s or s .. ")" + elseif s == "" then + return string.format(" (filter: %s)", tostring(filter)) + else + return string.format("%s, filter: %s)", s, tostring(filter)) + end +end + function Header:count() local yanked = #cx.yanked