summaryrefslogtreecommitdiff
path: root/mac/.config/yazi/plugins/smart-filter.yazi/main.lua
blob: 146e265c9d185b4e2191eb567464dc8f3c176660 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
--- @since 25.5.31

local hovered = ya.sync(function()
	local h = cx.active.current.hovered
	if not h then
		return {}
	end

	return {
		url = h.url,
		is_dir = h.cha.is_dir,
		unique = #cx.active.current.files == 1,
	}
end)

local function prompt()
	return ya.input {
		title = "Smart filter:",
		pos = { "center", w = 50 },
		position = { "center", w = 50 }, -- TODO: remove
		realtime = true,
		debounce = 0.1,
	}
end

local function entry()
	local input = prompt()

	while true do
		local value, event = input:recv()
		if event ~= 1 and event ~= 3 then
			ya.emit("escape", { filter = true })
			break
		end

		ya.emit("filter_do", { value, smart = true })

		local h = hovered()
		if h.unique and h.is_dir then
			ya.emit("escape", { filter = true })
			ya.emit("enter", {})
			input = prompt()
		elseif event == 1 then
			ya.emit("escape", { filter = true })
			ya.emit(h.is_dir and "enter" or "open", { h.url })
			break
		end
	end
end

return { entry = entry }