@@ -14,6 +14,7 @@ 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
1718--- @field reports_buf integer
1819--- @field hint_buf integer
1920--- @field help_buf integer
@@ -50,21 +51,33 @@ function LedgerTuiLayout.set_buffer_options()
5051 vim .bo .swapfile = false
5152end
5253
54+ --- @param buffer integer
55+ function LedgerTuiLayout :close_buffer (buffer )
56+ if buffer == nil then
57+ return
58+ end
59+
60+ if vim .api .nvim_buf_is_valid (buffer ) then
61+ vim .api .nvim_buf_delete (buffer , { force = true })
62+ end
63+ end
64+
5365--- sets up all the windows for the tui layout, which consists
5466--- of three buffers, one for the reports, one for the output
5567--- and one for working with filters, which is supposed to look
5668--- something like this:
5769---
5870--- ┌───┬───────────┬───┐
59- --- │ │ │ │
60- --- │ R │ O │ F │
61- --- │ │ │ │
71+ --- │ │ │ F │
72+ --- │ R │ O ├───┤
73+ --- │ │ │ A │
6274--- ├───┴───────────┴───┤
6375--- │ HINT │
6476--- └───────────────────┘
6577--- R: Reports pane
6678--- O: Outputs pane
6779--- F: Filters pane
80+ --- A: Applied Filters pane
6881function LedgerTuiLayout :setup_windows ()
6982 vim .cmd (" split" )
7083 local hint_buf = vim .api .nvim_create_buf (false , true )
@@ -92,6 +105,12 @@ function LedgerTuiLayout:setup_windows()
92105 local filter_width = math.ceil (vim .o .columns * 0.15 )
93106
94107 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 )
95114 vim .cmd (" wincmd h" )
96115 vim .cmd (" wincmd h" )
97116
@@ -105,6 +124,7 @@ function LedgerTuiLayout:setup_windows()
105124 vim .defer_fn (function ()
106125 vim .api .nvim_set_option_value (" modifiable" , false , { buf = output_buf })
107126 vim .api .nvim_set_option_value (" modifiable" , false , { buf = filters_buf })
127+ vim .api .nvim_set_option_value (" modifiable" , false , { buf = applied_filters_buf })
108128 vim .api .nvim_set_option_value (" modifiable" , false , { buf = reports_buf })
109129 end , 100 )
110130
@@ -113,6 +133,7 @@ function LedgerTuiLayout:setup_windows()
113133 self .output_buf = output_buf
114134 self .filters_buf = filters_buf
115135 self .reports_buf = reports_buf
136+ self .applied_filters_buf = applied_filters_buf
116137 self :setup_keymaps ()
117138end
118139
@@ -373,6 +394,57 @@ local function open_filter_input_popup()
373394
374395 vim .api .nvim_open_win (hint_buf , false , win_config (center_y + 3 + 1 , center_x , 4 , " Summary" ))
375396 vim .api .nvim_open_win (input_buf , true , win_config (center_y , center_x , 1 ))
397+
398+ vim .api .nvim_create_autocmd ({ " BufLeave" }, {
399+ group = layout .augroup ,
400+ buffer = input_buf ,
401+ callback = function ()
402+ if vim .api .nvim_buf_is_valid (hint_buf ) then
403+ vim .api .nvim_buf_delete (hint_buf , { force = true })
404+ end
405+ if vim .api .nvim_buf_is_valid (input_buf ) then
406+ vim .api .nvim_buf_delete (input_buf , { force = true })
407+ end
408+ end ,
409+ })
410+
411+ vim .api .nvim_buf_set_option (input_buf , " buftype" , " acwrite" )
412+ vim .api .nvim_buf_set_option (input_buf , " bufhidden" , " wipe" )
413+ vim .api .nvim_buf_set_option (input_buf , " swapfile" , false )
414+ vim .api .nvim_buf_set_name (input_buf , " InputBuffer" )
415+
416+ vim .api .nvim_create_autocmd ({ " BufWriteCmd" }, {
417+ buffer = input_buf ,
418+ callback = function ()
419+ local lines = vim .api .nvim_buf_get_lines (input_buf , 0 , - 1 , false )
420+
421+ if # lines > 0 then
422+ local reports = require (" ledger.tui.reports" ).get ()
423+ local layout = require (" ledger.tui.layout" ).get ()
424+
425+ local input = lines [1 ]
426+ if reports .filters [report_name ] == nil then
427+ reports .filters [report_name ] = { active = {} }
428+ end
429+ reports .filters [report_name ].active [filter_name ] = {
430+ flag = filter .flag ,
431+ value = input ,
432+ }
433+ logger :info (" applying filter '" .. filter_name .. " ' with input '" .. input .. " '" )
434+ layout .focus_reports ()
435+ reports :maybe_run_command ()
436+ reports :populate_filters ()
437+ layout :update_hint ()
438+ end
439+
440+ if vim .api .nvim_buf_is_valid (input_buf ) then
441+ vim .api .nvim_buf_delete (input_buf , { force = true })
442+ end
443+ if vim .api .nvim_buf_is_valid (hint_buf ) then
444+ vim .api .nvim_buf_delete (hint_buf , { force = true })
445+ end
446+ end ,
447+ })
376448end
377449
378450local function dismiss_hint ()
@@ -382,8 +454,10 @@ local function dismiss_hint()
382454 vim .api .nvim_set_current_buf (layout .reports_buf )
383455 end
384456
385- vim .api .nvim_buf_delete (layout .hint_buf , {})
386- layout .hint_buf = - 1
457+ if vim .api .nvim_buf_is_valid (layout .hint_buf ) then
458+ vim .api .nvim_buf_delete (layout .hint_buf , {})
459+ layout .hint_buf = - 1
460+ end
387461end
388462
389463local function focus_reports ()
@@ -406,6 +480,10 @@ local function focus_reports()
406480 end
407481end
408482
483+ function LedgerTuiLayout :focus_reports ()
484+ focus_reports ()
485+ end
486+
409487local function focus_filters ()
410488 local layout = require (" ledger.tui.layout" ).get ()
411489
@@ -445,6 +523,7 @@ function LedgerTuiLayout:setup_keymaps()
445523 set_buffer_maps (self .output_buf )
446524 set_buffer_maps (self .filters_buf )
447525 set_buffer_maps (self .hint_buf )
526+ -- set_buffer_maps(self.applied_filters_buf)
448527
449528 vim .api .nvim_buf_create_user_command (self .filters_buf , " OpenFilterInput" , open_filter_input_popup , {})
450529 vim .api .nvim_buf_set_keymap (self .filters_buf , " n" , " <CR>" , " :OpenFilterInput<CR>" , {})
0 commit comments