Skip to content

Commit 9e15e03

Browse files
committed
feat: toggling filters
1 parent 8a2d6ce commit 9e15e03

File tree

2 files changed

+83
-27
lines changed

2 files changed

+83
-27
lines changed

lua/ledger/tui/layout.lua

Lines changed: 60 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ local HL_GROUPS = {
1414
--- @field previous_signcolumn "no" | "yes"
1515
--- @field output_buf integer
1616
--- @field filters_buf integer
17-
--- @field applied_filters_buf integer
1817
--- @field reports_buf integer
1918
--- @field hint_buf integer
2019
--- @field help_buf integer
@@ -105,12 +104,6 @@ function LedgerTuiLayout:setup_windows()
105104
local filter_width = math.ceil(vim.o.columns * 0.15)
106105

107106
vim.cmd("vertical resize " .. filter_width)
108-
109-
local applied_filters_buf = vim.api.nvim_create_buf(false, true)
110-
vim.api.nvim_set_current_buf(applied_filters_buf)
111-
self.set_buffer_options()
112-
self:set_window_options()
113-
vim.cmd("horizontal resize " .. filter_width)
114107
vim.cmd("wincmd h")
115108
vim.cmd("wincmd h")
116109

@@ -124,7 +117,6 @@ function LedgerTuiLayout:setup_windows()
124117
vim.defer_fn(function()
125118
vim.api.nvim_set_option_value("modifiable", false, { buf = output_buf })
126119
vim.api.nvim_set_option_value("modifiable", false, { buf = filters_buf })
127-
vim.api.nvim_set_option_value("modifiable", false, { buf = applied_filters_buf })
128120
vim.api.nvim_set_option_value("modifiable", false, { buf = reports_buf })
129121
end, 100)
130122

@@ -133,7 +125,6 @@ function LedgerTuiLayout:setup_windows()
133125
self.output_buf = output_buf
134126
self.filters_buf = filters_buf
135127
self.reports_buf = reports_buf
136-
self.applied_filters_buf = applied_filters_buf
137128
self:setup_keymaps()
138129
end
139130

@@ -275,7 +266,7 @@ local function toggle_help_popup()
275266
" [f] - select filters buffer",
276267
" [r] - select reports buffer",
277268
" [h] - hides hint buffer",
278-
"",
269+
" [t] - toggle selected filter",
279270
"",
280271
"",
281272
close_message,
@@ -309,7 +300,13 @@ local function open_filter_input_popup()
309300
if #selected_filter == 0 then
310301
return
311302
end
303+
312304
local filter_name = selected_filter[1]
305+
if #filter_name == 0 then
306+
return
307+
end
308+
309+
filter_name = filter_name:gsub("", "")
313310

314311
--- @type integer | nil
315312
local reports_win = nil
@@ -504,6 +501,57 @@ local function focus_filters()
504501
end
505502
end
506503

504+
local function toggle_filter()
505+
local layout = require("ledger.tui.layout").get()
506+
local reports = require("ledger.tui.reports").get()
507+
508+
--- @type integer | nil
509+
local reports_win = nil
510+
511+
for _, win in ipairs(vim.api.nvim_list_wins()) do
512+
if vim.api.nvim_win_get_buf(win) == layout.reports_buf then
513+
reports_win = win
514+
break
515+
end
516+
end
517+
518+
if not reports_win then
519+
return
520+
end
521+
522+
local row, _ = unpack(vim.api.nvim_win_get_cursor(reports_win))
523+
local selected_report = vim.api.nvim_buf_get_lines(layout.reports_buf, row - 1, row, false)
524+
if #selected_report == 0 then
525+
return
526+
end
527+
local report_name = selected_report[1]
528+
if #report_name == 0 then
529+
return
530+
end
531+
532+
local row, _ = unpack(vim.api.nvim_win_get_cursor(0))
533+
local selected_filter = vim.api.nvim_buf_get_lines(layout.filters_buf, row - 1, row, false)
534+
if #selected_filter == 0 then
535+
return
536+
end
537+
538+
local filter_name = selected_filter[1]
539+
if #filter_name == 0 then
540+
return
541+
end
542+
543+
if not filter_name:gmatch("") then
544+
return
545+
end
546+
547+
local filter_name = filter_name:gsub("", "")
548+
reports.filters[report_name].active[filter_name] = nil
549+
layout.focus_reports()
550+
reports:populate_filters(report_name)
551+
reports:maybe_run_command()
552+
layout:update_hint()
553+
end
554+
507555
function LedgerTuiLayout:setup_keymaps()
508556
--- @param buffer integer
509557
local function set_buffer_maps(buffer)
@@ -523,8 +571,9 @@ function LedgerTuiLayout:setup_keymaps()
523571
set_buffer_maps(self.output_buf)
524572
set_buffer_maps(self.filters_buf)
525573
set_buffer_maps(self.hint_buf)
526-
-- set_buffer_maps(self.applied_filters_buf)
527574

575+
vim.api.nvim_buf_create_user_command(self.filters_buf, "ToggleFilter", toggle_filter, {})
576+
vim.api.nvim_buf_set_keymap(self.filters_buf, "n", "t", ":ToggleFilter<CR>", {})
528577
vim.api.nvim_buf_create_user_command(self.filters_buf, "OpenFilterInput", open_filter_input_popup, {})
529578
vim.api.nvim_buf_set_keymap(self.filters_buf, "n", "<CR>", ":OpenFilterInput<CR>", {})
530579
end

lua/ledger/tui/reports.lua

Lines changed: 23 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -19,36 +19,43 @@ LedgerReports.__index = LedgerReports
1919
--- @class ledger.TuiReports
2020
local instance
2121

22-
function LedgerReports:populate_filters()
23-
local ok, current_report = self:maybe_get_current_report()
24-
if not ok then
25-
return
22+
--- @param report string | nil
23+
function LedgerReports:populate_filters(report)
24+
--- @type string
25+
local current_report
26+
27+
if report ~= nil then
28+
current_report = report
29+
end
30+
31+
if report == nil then
32+
local ok, active_report = self:maybe_get_current_report()
33+
if not ok then
34+
return
35+
end
36+
current_report = active_report
2637
end
2738

2839
for name, command in pairs(config.tui.sections) do
2940
if name == current_report then
3041
local content = {}
42+
3143
for name, _ in pairs(command.filters) do
32-
table.insert(content, name)
44+
local formatted = name
45+
if self.filters[current_report] ~= nil and self.filters[current_report].active[name] ~= nil then
46+
formatted = "" .. formatted
47+
end
48+
table.insert(content, formatted)
3349
end
50+
3451
table.sort(content, function(a, b)
3552
return a < b and true or false
3653
end)
54+
3755
self.layout.set_buffer_content(self.layout.filters_buf, content)
3856
return
3957
end
4058
end
41-
42-
if self.filters[current_report] ~= nil then
43-
local filters = {}
44-
45-
for name, filter in pairs(self.filters[current_report].active) do
46-
local formatted = name .. " (" .. filter.flag .. ") " .. filter.value
47-
table.insert(filters, formatted)
48-
end
49-
50-
-- self.layout.set_buffer_content(self.layout.applied_filters_buf, filters)
51-
end
5259
end
5360

5461
function LedgerReports:populate_reports()

0 commit comments

Comments
 (0)