From 98ec6d7fdb5e3b42f0cdeb5e04e76f7c3c0b92eb Mon Sep 17 00:00:00 2001 From: Chen Asraf Date: Fri, 20 Dec 2024 16:55:08 +0200 Subject: [PATCH] feat: lazygit new plugin wip --- .config/nvim/lua/casraf/lib/lazygit.lua | 71 +++++++++++++++++++++ .config/nvim/lua/casraf/plugins/lazygit.lua | 28 ++++---- .config/nvim/lua/casraf/plugins/remap.lua | 2 + 3 files changed, 90 insertions(+), 11 deletions(-) create mode 100644 .config/nvim/lua/casraf/lib/lazygit.lua diff --git a/.config/nvim/lua/casraf/lib/lazygit.lua b/.config/nvim/lua/casraf/lib/lazygit.lua new file mode 100644 index 00000000..f1da2a30 --- /dev/null +++ b/.config/nvim/lua/casraf/lib/lazygit.lua @@ -0,0 +1,71 @@ +local state = { + floating = { + buf = -1, + win = -1, + } +} + +local function create_floating_window(opts) + opts = opts or {} + local width = opts.width or math.floor(vim.o.columns * 0.8) + local height = opts.height or math.floor(vim.o.lines * 0.8) + + local col = math.floor((vim.o.columns - width) / 2) + local row = math.floor((vim.o.lines - height) / 2) + + local buf = nil + if vim.api.nvim_buf_is_valid(opts.buf) then + buf = opts.buf + else + buf = vim.api.nvim_create_buf(false, true) -- No file, scratch buffer + end + + local win_config = { + relative = 'editor', + width = width, + height = height, + col = col, + row = row, + style = 'minimal', + border = 'rounded', + } + + local win = vim.api.nvim_open_win(buf, true, win_config) + + return { buf = buf, win = win } +end + +local function toggle_lazygit() + if not vim.api.nvim_win_is_valid(state.floating.win) then + state.floating = create_floating_window({ buf = state.floating.buf }) + if vim.bo[state.floating.buf].buftype ~= "terminal" then + vim.cmd.terminal("lazygit") + vim.keymap.set({ 'n', 'i', 't' }, 'q', toggle_lazygit, { buffer = state.floating.buf }) + end + vim.defer_fn(function() + vim.cmd('startinsert') + end, 100) + else + vim.api.nvim_win_hide(state.floating.win) + end +end + +local function setup(opts) + opts = opts or {} + vim.api.nvim_create_user_command('LazyGit', toggle_lazygit, { force = true }) + vim.api.nvim_create_autocmd('WinResized', { + callback = function() + if not vim.api.nvim_win_is_valid(state.floating.win) then + return + end + + vim.api.nvim_win_hide(state.floating.win) + toggle_lazygit() + end, + }) +end + +local M = {} +M.toggle_lazygit = toggle_lazygit +M.setup = setup +return M diff --git a/.config/nvim/lua/casraf/plugins/lazygit.lua b/.config/nvim/lua/casraf/plugins/lazygit.lua index affddbf6..739b4091 100644 --- a/.config/nvim/lua/casraf/plugins/lazygit.lua +++ b/.config/nvim/lua/casraf/plugins/lazygit.lua @@ -1,16 +1,22 @@ --- local home = os.getenv('HOME') +-- local lazygit = require('casraf.lib.lazygit') +-- +-- lazygit.setup({}) + return { - "chenasraf/lazygit.nvim", - branch = "feat/resize", + { + "chenasraf/lazygit.nvim", + branch = "feat/resize", + -- enabled = false, - -- dir = home .. "/Dev/lazygit.nvim", + -- dir = home .. "/Dev/lazygit.nvim", - -- "kdheepak/lazygit.nvim", - -- optional for floating window border decoration - dependencies = { - "nvim-lua/plenary.nvim", + -- "kdheepak/lazygit.nvim", + -- optional for floating window border decoration + dependencies = { + "nvim-lua/plenary.nvim", + }, + config = function() + vim.keymap.set('n', 'gs', 'LazyGit', { desc = 'Lazy[G]it', silent = true }) + end, }, - config = function() - vim.keymap.set('n', 'gs', 'LazyGit', { desc = 'Lazy[G]it', silent = true }) - end, } diff --git a/.config/nvim/lua/casraf/plugins/remap.lua b/.config/nvim/lua/casraf/plugins/remap.lua index 3efd2bc1..5187825b 100644 --- a/.config/nvim/lua/casraf/plugins/remap.lua +++ b/.config/nvim/lua/casraf/plugins/remap.lua @@ -85,6 +85,8 @@ vim.keymap.set("v", "r", [["hy:%s/h/h/gI]], --- NOTE general utils/cmds --- +vim.keymap.set("t", "", "", { desc = "Exit terminal mode" }) + --- serve working directory as web server & open in browser vim.keymap.set("n", "Srv", function() local cmd = "http-server -p 5500"