Skip to content

Commit

Permalink
wip: initial setup of reports tui
Browse files Browse the repository at this point in the history
  • Loading branch information
wllfaria committed Aug 21, 2024
1 parent 00ddb93 commit b4af3af
Show file tree
Hide file tree
Showing 5 changed files with 142 additions and 6 deletions.
15 changes: 13 additions & 2 deletions lua/ledger/commands.lua
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,17 @@ local M = {}

--- @class LedgerCommands
--- @field augroup integer
--- @field tui_augroup integer
--- @field output_augroup integer
local LedgerCommands = {}
LedgerCommands.__index = LedgerCommands

--- @class LedgerCommands
local instance

function LedgerCommands:create_augroups()
self.augroup = vim.api.nvim_create_augroup("Ledger", {})
self.tui_augroup = vim.api.nvim_create_augroup("LedgerTui", {})
self.output_augroup = vim.api.nvim_create_augroup("LedgerOutput", {})
end

Expand Down Expand Up @@ -108,9 +113,15 @@ function LedgerCommands:setup_autocommands()
})
end

function LedgerCommands:setup_tui_autocommands()
self.tui_augroup = tui
end

function M.setup()
local self = setmetatable({}, LedgerCommands)
return self
if not instance then
instance = setmetatable({}, LedgerCommands)
end
return instance
end

return M
24 changes: 23 additions & 1 deletion lua/ledger/config.lua
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
local M = {}

--- @class ledger.Tui
--- @field enabled boolean

--- @class ledger.SnippetKeymaps
--- @field new_account string[]
--- @field new_posting string[]
Expand All @@ -11,9 +14,14 @@ local M = {}
--- @field key string
--- @field command string

--- @class ledger.TuiKeymaps
--- @field initialize string[]
--- @field shutdown string[]

--- @class ledger.Keymaps
--- @field snippets ledger.SnippetKeymaps
--- @field reports ledger.ReportKeymap[]
--- @field tui ledger.TuiKeymaps

--- @class ledger.PartialKeymaps
--- @field snippets? ledger.SnippetKeymaps
Expand Down Expand Up @@ -51,6 +59,7 @@ local M = {}
--- @field snippets ledger.Snippet
--- @field keymaps ledger.Keymaps
--- @field diagnostics ledger.Diagnostics
--- @field tui ledger.Tui
local LedgerConfig = {}
LedgerConfig.__index = LedgerConfig

Expand Down Expand Up @@ -144,6 +153,13 @@ local function get_default_config()
new_commodity = { "cm" },
},
reports = {},
tui = {
initialize = { "<leader>tui" },
shutdown = { "<leader>tud" },
},
},
tui = {
enabled = true,
},
}

Expand All @@ -158,6 +174,13 @@ function LedgerConfig:set_keymaps()
commands:run_report(keymap.command)
end)
end

if not self.tui.enabled then
return
end

local tui = require("ledger.tui").get()
tui:set_keymaps()
end

--- Config is a singleton, allowing us to call `get` as many times as we
Expand All @@ -171,7 +194,6 @@ function M.setup(overrides)
local default = get_default_config()
local with_overrides = vim.tbl_deep_extend("force", default, overrides or {})
instance = setmetatable(with_overrides, LedgerConfig)
instance:set_keymaps()
end
return instance
end
Expand Down
6 changes: 6 additions & 0 deletions lua/ledger/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,12 @@ function M.setup(overrides)
require("ledger.diagnostics").get_diagnostics()
end

if config.tui.enabled then
require("ledger.tui").setup()
end

config:set_keymaps()

--- @type ledger.Main
local default = {
context = context,
Expand Down
6 changes: 3 additions & 3 deletions lua/ledger/parser.lua
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ local ts_utils = require("nvim-treesitter.ts_utils")
--- @class ledger.parser
--- @field get_parser fun(source: string): TSSource
--- @field find_current_scope fun(): Scope
--- @field query_iter fun(query: vim.treesitter.Query, node: TSNode, source: string):
--- @field query_iter fun(query: vim.treesitter.Query, node: TSNode, source: string): fun(end_line: integer|nil):integer, TSNode, vim.treesitter.query.TSMetadata, TSQueryMatch
--- @field get_account_names_from_source fun(node: TSNode, source: string, filename: string, ctx: ledger.Context)
--- @field get_commodities_from_source fun(node: TSNode, source: string, filename: string, ctx: ledger.Context)
local M = {}
Expand Down Expand Up @@ -142,13 +142,13 @@ function M.get_postings_from_source(node, source, filename, ctx)

if child_count > 0 then
--- @type TSNode
local account, amount = unpack(match:named_children(0))
local account, amount = unpack(match:named_children())
local account_text = vim.treesitter.get_node_text(account, source)
posting.account = { text = account_text, range = M.get_node_range(account) }

if amount then
--- @type TSNode, TSNode
local commodity, quantity = unpack(amount:named_children(0))
local commodity, quantity = unpack(amount:named_children())
local commodity_text = vim.treesitter.get_node_text(commodity, source)
posting.commodity = { text = commodity_text, range = M.get_node_range(commodity) }

Expand Down
97 changes: 97 additions & 0 deletions lua/ledger/tui/init.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
local M = {}

--- @class LedgerTui
local LedgerTui = {}
LedgerTui.__index = LedgerTui

--- @class LedgerTui
local instance

function LedgerTui:set_autocmds() end

--- @param buffer integer
function LedgerTui:set_buffer_options(buffer)
vim.bo.buftype = "nofile"
vim.bo.bufhidden = "wipe"
vim.bo.swapfile = false
vim.api.nvim_set_option_value("number", false, {})
vim.api.nvim_set_option_value("relativenumber", false, {})
vim.api.nvim_set_option_value("signcolumn", "no", {})
end

function LedgerTui:setup()
print("initializing")
-- Create a vertical split (left: information buffer, right: output buffer)
vim.cmd("vsplit")

-- Create an empty buffer for the output in the right window
local output_buf = vim.api.nvim_create_buf(false, true)
vim.api.nvim_set_current_buf(output_buf)

-- Set buffer options for output buffer
self:set_buffer_options(output_buf)
local output_width = math.ceil(vim.o.columns * 0.75)
vim.cmd("vertical resize " .. output_width)

-- Create a horizontal split below the output buffer (filters buffer)
vim.cmd("split")

-- Create an empty buffer for the filters in the bottom window
local filters_buf = vim.api.nvim_create_buf(false, true)
vim.api.nvim_set_current_buf(filters_buf)

-- Set buffer options for filters buffer
self:set_buffer_options(filters_buf)
local filter_height = math.ceil(vim.o.lines * 0.15)
vim.cmd("horizontal resize " .. filter_height)

-- Move back to the original left split (information buffer)
vim.cmd("wincmd h")

-- Create an empty buffer for the information in the left window
local info_buf = vim.api.nvim_create_buf(false, true)
vim.api.nvim_set_current_buf(info_buf)

-- Set buffer options for information buffer
self:set_buffer_options(info_buf)

-- Optionally set keymaps to close each buffer/window
vim.api.nvim_buf_set_keymap(output_buf, "n", "q", ":q<CR>", { noremap = true, silent = true })
vim.api.nvim_buf_set_keymap(filters_buf, "n", "q", ":q<CR>", { noremap = true, silent = true })
vim.api.nvim_buf_set_keymap(info_buf, "n", "q", ":q<CR>", { noremap = true, silent = true })
end

function LedgerTui:shutdown()
print("shutting down")
end

function LedgerTui:set_keymaps()
local config = require("ledger.config").get()
local tui_keymaps = config.keymaps.tui

--- @param commands string[]
--- @param callback function
local set_keymap = function(commands, callback)
for _, trigger in pairs(commands) do
vim.keymap.set("n", trigger, function()
callback(self)
end)
end
end

set_keymap(tui_keymaps.initialize, self.setup)
set_keymap(tui_keymaps.shutdown, self.shutdown)
end

function M.setup()
if not instance then
instance = setmetatable({}, LedgerTui)
end
return instance
end

function M.get()
return instance
end

return M

0 comments on commit b4af3af

Please sign in to comment.