Refactor the code

This commit is contained in:
sxyazi 2024-06-22 18:55:18 +08:00
parent ed8b30b81d
commit 8be63811a4
No known key found for this signature in database
3 changed files with 23 additions and 17 deletions

View file

@ -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<Self> {
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)]

View file

@ -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::<Self>(|reg| {
reg.add_field_method_get("to_string", |_, me| Ok(me.to_string()));
reg.add_meta_method(MetaMethod::ToString, |_, me, ()| Ok(me.to_string()));
})
}
}

View file

@ -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