From 1d62d6ee6376ddca073f3b8e20a652497b89c4d5 Mon Sep 17 00:00:00 2001 From: Dheepak Krishnamurthy Date: Wed, 8 Apr 2020 10:58:34 -0600 Subject: [PATCH] Reorder functions --- lua/lazygit.lua | 39 ++++++++++++++++++++++++--------------- 1 file changed, 24 insertions(+), 15 deletions(-) diff --git a/lua/lazygit.lua b/lua/lazygit.lua index faa1cfd..4512305 100644 --- a/lua/lazygit.lua +++ b/lua/lazygit.lua @@ -1,6 +1,8 @@ local api = vim.api local fn = vim.fn +local file_buffer = nil + local OPTIONS = { lazygit_floating_window_scaling_factor = 0.9, lazygit_floating_window_winblend = 0, @@ -15,6 +17,22 @@ local function is_lazygit_available() return fn.executable("lazygit") == 1 end +local function project_root_dir() + return fn.system('cd ' .. fn.expand('%:p:h') .. ' && git rev-parse --show-toplevel 2> /dev/null') +end + +local function exec_lazygit_command() + local current_dir = fn.getcwd() + -- TODO: ensure that it is a valid git directory + local root_dir = project_root_dir() + local cmd = "lazygit " .. "-p " .. root_dir + -- ensure that the buffer is closed on exit + execute([[ + call termopen('%s', {'on_exit': {job_id, code, event-> luaeval("require('lazygit').on_exit(" . job_id . "," . code . "," . event . ")")}}) + ]], cmd) + vim.cmd "startinsert" +end + local function open_floating_window() -- create a unlisted scratch buffer local file_buffer = api.nvim_create_buf(false, true) @@ -64,11 +82,13 @@ local function open_floating_window() vim.cmd('set winblend=' .. OPTIONS.lazygit_floating_window_winblend) -- use autocommand to ensure that the border_buffer closes at the same time as the main buffer - vim.cmd('au BufWipeout silent! execute "silent bwipeout!"' .. border_buffer) + vim.cmd('autocmd BufWipeout silent! execute "silent bwipeout!"' .. border_buffer) end -local function project_root_dir() - return fn.system('cd ' .. fn.expand('%:p:h') .. ' && git rev-parse --show-toplevel 2> /dev/null') +local function on_buf_leave() + file_buffer = fn.bufnr("%") + vim.cmd("hide") + vim.cmd("hide") end local function on_exit(job_id, code, event) @@ -77,18 +97,6 @@ local function on_exit(job_id, code, event) end end -local function exec_lazygit_command() - local current_dir = fn.getcwd() - -- TODO: ensure that it is a valid git directory - local root_dir = project_root_dir() - local cmd = "lazygit " .. "-p " .. root_dir - -- ensure that the buffer is closed on exit - execute([[ - call termopen('%s', {'on_exit': {job_id, code, event-> luaeval("require('lazygit').on_exit(" . job_id . "," . code . "," . event . ")")}}) - ]], cmd) - vim.cmd "startinsert" -end - local function lazygit() if is_lazygit_available() ~= true then print("Please install lazygit. Check documentation for more information") @@ -109,4 +117,5 @@ return { setup = setup, lazygit = lazygit, on_exit = on_exit, + on_buf_leave = on_buf_leave, }