feat: suggest keywords in the header if a finder is active (#1847)

This commit is contained in:
三咲雅 · Misaki Masa 2024-10-28 17:14:41 +08:00 committed by GitHub
parent 4dd24f779c
commit e3507205fb
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 28 additions and 20 deletions

View File

@ -1,6 +1,6 @@
use std::ops::Deref; use std::ops::Deref;
use mlua::{AnyUserData, Lua}; use mlua::{AnyUserData, Lua, MetaMethod, UserDataMethods};
use super::SCOPE; use super::SCOPE;
@ -21,6 +21,8 @@ impl Finder {
} }
pub(super) fn register(lua: &Lua) -> mlua::Result<()> { pub(super) fn register(lua: &Lua) -> mlua::Result<()> {
lua.register_userdata_type::<Self>(|_| {}) lua.register_userdata_type::<Self>(|reg| {
reg.add_meta_method(MetaMethod::ToString, |_, me, ()| Ok(me.filter.to_string()));
})
} }
} }

View File

@ -17,6 +17,7 @@ function Header:new(area, tab)
return setmetatable({ return setmetatable({
_area = area, _area = area,
_tab = tab, _tab = tab,
_current = tab.current,
}, { __index = self }) }, { __index = self })
end end
@ -26,22 +27,26 @@ function Header:cwd()
return ui.Span("") return ui.Span("")
end end
local s = ya.readable_path(tostring(self._tab.current.cwd)) .. self:flags() local s = ya.readable_path(tostring(self._current.cwd)) .. self:flags()
return ui.Span(ya.truncate(s, { max = max, rtl = true })):style(THEME.manager.cwd) return ui.Span(ya.truncate(s, { max = max, rtl = true })):style(THEME.manager.cwd)
end end
function Header:flags() function Header:flags()
local cwd = self._tab.current.cwd local cwd = self._current.cwd
local filter = self._tab.current.files.filter local filter = self._current.files.filter
local finder = self._tab.finder
local s = cwd.is_search and string.format(" (search: %s", cwd:frag()) or "" local t = {}
if not filter then if cwd.is_search then
return s == "" and s or s .. ")" t[#t + 1] = string.format("search: %s", cwd:frag())
elseif s == "" then
return string.format(" (filter: %s)", tostring(filter))
else
return string.format("%s, filter: %s)", s, tostring(filter))
end end
if filter then
t[#t + 1] = string.format("filter: %s", filter)
end
if finder then
t[#t + 1] = string.format("find: %s", finder)
end
return #t == 0 and "" or " (" .. table.concat(t, ", ") .. ")"
end end
function Header:count() function Header:count()
@ -65,7 +70,7 @@ function Header:count()
return ui.Line { return ui.Line {
ui.Span(string.format(" %d ", count)):style(style), ui.Span(string.format(" %d ", count)):style(style),
ui.Span(" "), " ",
} }
end end

View File

@ -20,6 +20,7 @@ function Status:new(area, tab)
return setmetatable({ return setmetatable({
_area = area, _area = area,
_tab = tab, _tab = tab,
_current = tab.current,
}, { __index = self }) }, { __index = self })
end end
@ -45,7 +46,7 @@ function Status:mode()
end end
function Status:size() function Status:size()
local h = self._tab.current.hovered local h = self._current.hovered
if not h then if not h then
return ui.Line {} return ui.Line {}
end end
@ -58,7 +59,7 @@ function Status:size()
end end
function Status:name() function Status:name()
local h = self._tab.current.hovered local h = self._current.hovered
if not h then if not h then
return ui.Line {} return ui.Line {}
end end
@ -67,7 +68,7 @@ function Status:name()
end end
function Status:permissions() function Status:permissions()
local h = self._tab.current.hovered local h = self._current.hovered
if not h then if not h then
return ui.Line {} return ui.Line {}
end end
@ -97,8 +98,8 @@ end
function Status:percentage() function Status:percentage()
local percent = 0 local percent = 0
local cursor = self._tab.current.cursor local cursor = self._current.cursor
local length = #self._tab.current.files local length = #self._current.files
if cursor ~= 0 and length ~= 0 then if cursor ~= 0 and length ~= 0 then
percent = math.floor((cursor + 1) * 100 / length) percent = math.floor((cursor + 1) * 100 / length)
end end
@ -119,8 +120,8 @@ function Status:percentage()
end end
function Status:position() function Status:position()
local cursor = self._tab.current.cursor local cursor = self._current.cursor
local length = #self._tab.current.files local length = #self._current.files
local style = self:style() local style = self:style()
return ui.Line { return ui.Line {