From d7afe4cf1113f698db47a4f1470122d7f41808cf Mon Sep 17 00:00:00 2001 From: maaru Date: Fri, 17 Jan 2025 01:58:07 +0600 Subject: [PATCH 1/2] new func | Keymaps, ToggleGraph --- lua/gitgraph.lua | 19 +++++++++++ lua/gitgraph/config.lua | 12 ++++++- lua/gitgraph/core.lua | 75 +++++++++++++++++++++++++++++++++++++++++ 3 files changed, 105 insertions(+), 1 deletion(-) diff --git a/lua/gitgraph.lua b/lua/gitgraph.lua index 8476b2e..71db9fe 100644 --- a/lua/gitgraph.lua +++ b/lua/gitgraph.lua @@ -1,6 +1,7 @@ local log = require('gitgraph.log') local config = require('gitgraph.config') local highlights = require('gitgraph.highlights') +local core = require('gitgraph.core') local M = { config = config.defaults, @@ -19,6 +20,24 @@ function M.setup(user_config) math.randomseed(os.time()) log.set_level(M.config.log_level) + + -- Set the keymap for toggling GitGraph + vim.keymap.set('n', M.config.toggle_keymap, function() + core.toggleGitGraph(M.config) + end, { desc = 'Toggle GitGraph window' }) + + vim.keymap.set('n', M.config.close_key, function() + core.closeGitGraph(M.config) + end, { desc = 'quit gitgraph' }) + + vim.api.nvim_create_user_command('GitGraph', function() + core.toggleGitGraph(M.config) + end, { desc = 'Toggle GitGraph window' }) + + -- Register the CloseGitGraph command + vim.api.nvim_create_user_command('GitGraphClose', function() + core.closeGitGraph() + end, { desc = 'Close GitGraph window or buffer' }) end --- Draws the gitgraph in buffer diff --git a/lua/gitgraph/config.lua b/lua/gitgraph/config.lua index 6c13077..3d387b5 100644 --- a/lua/gitgraph/config.lua +++ b/lua/gitgraph/config.lua @@ -46,7 +46,15 @@ local M = {} ---@type I.GGConfig M.defaults = { + layout = 'floating', -- Options: "floating", "vertical", "horizontal" + floating_width = 80, -- Width for floating window (percentage of the screen) + floating_height = 80, -- Height for floating window (percentage of the screen) + border = 'single', -- Options: "single", "double", "rounded", "solid", "shadow" + toggle_keymap = 'gi', -- Default keymap for toggling GitGraph + key_close = 'q', -- Keymap to close GitGraph buffer + symbols = { + merge_commit = 'M', commit = '*', merge_commit_end = 'M', @@ -88,5 +96,7 @@ M.defaults = { }, log_level = vim.log.levels.ERROR, } - +function M.setup(user_config) + M.defaults = vim.tbl_deep_extend('force', M.defaults, user_config or {}) +end return M diff --git a/lua/gitgraph/core.lua b/lua/gitgraph/core.lua index e604368..814706f 100644 --- a/lua/gitgraph/core.lua +++ b/lua/gitgraph/core.lua @@ -1002,4 +1002,79 @@ function M._gitgraph(raw_commits, opt, sym, fields) return graph, lines, highlights, head_loc, found_bi_crossing end +local gitgraph_window_id = nil + +--- Toggles the GitGraph window +---@param config table +function M.toggleGitGraph(config) + if gitgraph_window_id and vim.api.nvim_win_is_valid(gitgraph_window_id) then + vim.api.nvim_win_close(gitgraph_window_id, true) + gitgraph_window_id = nil + return + end + + -- Create a new buffer + local buf = vim.api.nvim_create_buf(false, true) + if not buf then + print('Failed to create buffer for GitGraph') + return + end + + -- Open the window based on layout + if config.layout == 'floating' then + local width = math.floor(vim.o.columns * (config.floating_width / 100)) + local height = math.floor(vim.o.lines * (config.floating_height / 100)) + local row = math.floor((vim.o.lines - height) / 2) + local col = math.floor((vim.o.columns - width) / 2) + + gitgraph_window_id = vim.api.nvim_open_win(buf, true, { + relative = 'editor', + width = width, + height = height, + row = row, + col = col, + style = 'minimal', + border = config.border, + }) + elseif config.layout == 'vertical' then + vim.cmd('vsplit') + vim.api.nvim_win_set_buf(0, buf) + gitgraph_window_id = vim.api.nvim_get_current_win() + elseif config.layout == 'horizontal' then + vim.cmd('split') + vim.api.nvim_win_set_buf(0, buf) + gitgraph_window_id = vim.api.nvim_get_current_win() + else + error('Invalid layout option') + end + + -- Set a unique buffer name + vim.api.nvim_buf_set_name(buf, 'GitGraph_' .. tostring(buf)) + + -- Set buffer options + vim.api.nvim_buf_set_option(buf, 'buftype', 'nofile') + vim.api.nvim_buf_set_option(buf, 'swapfile', false) + vim.api.nvim_buf_set_option(buf, 'bufhidden', 'wipe') + + -- Validate and bind the close key + if type(config.key_close) == 'string' and #config.key_close > 0 then + vim.api.nvim_buf_set_keymap(buf, 'n', config.key_close, ':CloseGitGraph', { noremap = true, silent = true }) + else + error('Invalid key_close in configuration: Expected a non-empty string') + end + + -- Call the draw function from the plugin + require('gitgraph.draw').draw(config, { all = true }, { max_count = 100 }) +end + +--- Closes the GitGraph window or buffer +function M.closeGitGraph() + if gitgraph_window_id and vim.api.nvim_win_is_valid(gitgraph_window_id) then + vim.api.nvim_win_close(gitgraph_window_id, true) + gitgraph_window_id = nil + else + print('GitGraph is not open') + end +end + return M From cb91270c5134a487294976b2a4d8f047865dec49 Mon Sep 17 00:00:00 2001 From: maaru Date: Fri, 17 Jan 2025 02:11:34 +0600 Subject: [PATCH 2/2] new func | Keymaps, ToggleGraph --- lua/gitgraph.lua | 80 ++++++++++++++++++++++++------------------------ 1 file changed, 40 insertions(+), 40 deletions(-) diff --git a/lua/gitgraph.lua b/lua/gitgraph.lua index 71db9fe..625489f 100644 --- a/lua/gitgraph.lua +++ b/lua/gitgraph.lua @@ -1,43 +1,43 @@ -local log = require('gitgraph.log') -local config = require('gitgraph.config') -local highlights = require('gitgraph.highlights') -local core = require('gitgraph.core') +local log = require("gitgraph.log") +local config = require("gitgraph.config") +local highlights = require("gitgraph.highlights") +local core = require("gitgraph.core") local M = { - config = config.defaults, + config = config.defaults, - buf = nil, ---@type integer? - graph = {}, ---@type I.Row[] + buf = nil, ---@type integer? + graph = {}, ---@type I.Row[] } --- Setup ---@param user_config I.GGConfig function M.setup(user_config) - M.config = vim.tbl_deep_extend('force', M.config, user_config) + M.config = vim.tbl_deep_extend("force", M.config, user_config) - highlights.set_highlights() + highlights.set_highlights() - math.randomseed(os.time()) + math.randomseed(os.time()) - log.set_level(M.config.log_level) + log.set_level(M.config.log_level) - -- Set the keymap for toggling GitGraph - vim.keymap.set('n', M.config.toggle_keymap, function() - core.toggleGitGraph(M.config) - end, { desc = 'Toggle GitGraph window' }) + -- Set the keymap for toggling GitGraph + vim.keymap.set("n", M.config.toggle_keymap, function() + core.toggleGitGraph(M.config) + end, { desc = "Toggle GitGraph window" }) - vim.keymap.set('n', M.config.close_key, function() - core.closeGitGraph(M.config) - end, { desc = 'quit gitgraph' }) + vim.keymap.set("n", M.config.key_close, function() + core.closeGitGraph(M.config) + end, { desc = "quit gitgraph" }) - vim.api.nvim_create_user_command('GitGraph', function() - core.toggleGitGraph(M.config) - end, { desc = 'Toggle GitGraph window' }) + vim.api.nvim_create_user_command("GitGraph", function() + core.toggleGitGraph(M.config) + end, { desc = "Toggle GitGraph window" }) - -- Register the CloseGitGraph command - vim.api.nvim_create_user_command('GitGraphClose', function() - core.closeGitGraph() - end, { desc = 'Close GitGraph window or buffer' }) + -- Register the CloseGitGraph command + vim.api.nvim_create_user_command("GitGraphClose", function() + core.closeGitGraph() + end, { desc = "Close GitGraph window or buffer" }) end --- Draws the gitgraph in buffer @@ -45,35 +45,35 @@ end ---@param args I.GitLogArgs ---@return nil function M.draw(options, args) - return require('gitgraph.draw').draw(M.config, options, args) + return require("gitgraph.draw").draw(M.config, options, args) end --- Tests the gitgraph plugin function M.test() - local lines, _failure = require('gitgraph.tests').run_tests(M.config.symbols, M.config.format.fields) + local lines, _failure = require("gitgraph.tests").run_tests(M.config.symbols, M.config.format.fields) - local buf = vim.api.nvim_create_buf(false, true) - vim.api.nvim_win_set_buf(0, buf) + local buf = vim.api.nvim_create_buf(false, true) + vim.api.nvim_win_set_buf(0, buf) - vim.api.nvim_buf_set_lines(buf, 0, #lines, false, lines) + vim.api.nvim_buf_set_lines(buf, 0, #lines, false, lines) - local cursor_line = #lines - vim.api.nvim_win_set_cursor(0, { cursor_line, 0 }) - vim.api.nvim_set_option_value('modifiable', false, { buf = buf }) + local cursor_line = #lines + vim.api.nvim_win_set_cursor(0, { cursor_line, 0 }) + vim.api.nvim_set_option_value("modifiable", false, { buf = buf }) end --- Draws a random gitgraph function M.random() - local buf = vim.api.nvim_create_buf(false, true) - vim.api.nvim_win_set_buf(0, buf) + local buf = vim.api.nvim_create_buf(false, true) + vim.api.nvim_win_set_buf(0, buf) - local lines = require('gitgraph.tests').run_random(M.config.symbols, M.config.format.fields) + local lines = require("gitgraph.tests").run_random(M.config.symbols, M.config.format.fields) - vim.api.nvim_buf_set_lines(buf, 0, #lines, false, lines) + vim.api.nvim_buf_set_lines(buf, 0, #lines, false, lines) - local cursor_line = 1 - vim.api.nvim_win_set_cursor(0, { cursor_line, 0 }) - vim.api.nvim_set_option_value('modifiable', false, { buf = buf }) + local cursor_line = 1 + vim.api.nvim_win_set_cursor(0, { cursor_line, 0 }) + vim.api.nvim_set_option_value("modifiable", false, { buf = buf }) end return M