commit 3759d6ab85b221dac8a95e2c7a3b007e2f961853 Author: Dheepak Krishnamurthy Date: Fri Apr 3 16:41:47 2020 -0600 Initial commit diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..15af027 --- /dev/null +++ b/LICENSE @@ -0,0 +1,9 @@ +MIT License + +Copyright (c) 2020 Dheepak Krishnamurthy + +Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. diff --git a/README.md b/README.md new file mode 100644 index 0000000..76757ed --- /dev/null +++ b/README.md @@ -0,0 +1,32 @@ +# lazygit.nvim + +Plugin for calling lazygit from within nvim. + +### Install + +Use any plugin manager: + +**[vim-plug](https://github.com/junegunn/vim-plug)** + +```vim +Plug 'kdheepak/lazygit.nvim' +``` + +**[dein.vim](https://github.com/Shougo/dein.vim)** + +```vim +call dein#add('kdheepak/lazygit.nvim') +``` + +**[Vundle.vim](https://github.com/junegunn/vim-plug)** + +```vim +Plugin 'kdheepak/lazygit.nvim' +``` + +### Usage + +```vim +" lazygit +nnoremap lg :LazyGit +``` diff --git a/lua/lazygit.lua b/lua/lazygit.lua new file mode 100644 index 0000000..5cb032e --- /dev/null +++ b/lua/lazygit.lua @@ -0,0 +1,98 @@ +local api = vim.api +local fn = vim.fn + +local function echom(message) + api.nvim_command('echom "' .. tostring(message) .. '"') +end + +local function is_lazygit_available() + return fn.executable("lazygit") == 1 +end + +function open_floating_window() + + -- create a unlisted scratch buffer + local buffer = api.nvim_create_buf(false, true) + -- create a unlisted scratch buffer for the border + local border_buffer = api.nvim_create_buf(false, true) + + api.nvim_buf_set_option(buffer, 'bufhidden', 'wipe') + api.nvim_buf_set_option(buffer, 'filetype', 'lazygit') + + local columns = api.nvim_get_option("columns") + local lines = api.nvim_get_option("lines") + + local height = math.ceil(lines * 0.8 - 4) + local width = math.ceil(columns * 0.8) + + local row = math.ceil(lines - height) / 2 - 1 + local col = math.ceil(columns - width) / 2 + + local border_opts = { + style = "minimal", + relative = "editor", + row = row - 1, + col = col - 1, + width = width + 2, + height = height + 2, + } + + local opts = { + style = "minimal", + relative = "editor", + row = row, + col = col, + width = width, + height = height, + } + + local border_lines = {'╭' .. string.rep('─', width) .. '╮'} + local middle_line = '│' .. string.rep(' ', width) .. '│' + for i = 1, height do + table.insert(border_lines, middle_line) + end + table.insert(border_lines, '╰' .. string.rep('─', width) .. '╯') + -- set border_lines in the border buffer from start 0 to end -1 and strict_indexing false + api.nvim_buf_set_lines(border_buffer, 0, -1, false, border_lines) + + local border_window = api.nvim_open_win(border_buffer, true, border_opts) + api.nvim_command('set winhl=Normal:Floating') + local window = api.nvim_open_win(buffer, true, opts) + api.nvim_command('set winhl=Normal:Floating') + -- use autocommand to ensure that the border_buffer closes at the same time as the main buffer + api.nvim_command('au BufWipeout exe "silent bwipeout!"' .. border_buffer) + return window +end + +local function project_root_dir() + return fn.finddir('.git/..', fn.expand('%:p:h') .. ';') +end + +local function execute(cmd, ...) + cmd = cmd:format(...) + vim.api.nvim_command(cmd) +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': {-> execute(':q')}}) + ]], cmd) +end + +function lazygit() + if is_lazygit_available() ~= true then + print("Please install lazygit. Check documentation for more information") + return + end + local window = open_floating_window() + exec_lazygit_command() +end + +return { + lazygit = lazygit, +} diff --git a/plugin/lazygit.vim b/plugin/lazygit.vim new file mode 100644 index 0000000..ca206ae --- /dev/null +++ b/plugin/lazygit.vim @@ -0,0 +1,22 @@ +scriptencoding utf-8 + +if exists('g:loaded_lazygit_vim') | finish | endif + +let s:save_cpo = &cpoptions +set cpoptions&vim + +"""""""""""""""""""""""""""""""""""""""""""""""""""""" + +let s:lazygit_lua_loc = expand(':h:r') . '/../lua/' + +exe "lua package.path = package.path .. ';". s:lazygit_lua_loc."?/init.lua'" +exe "lua package.path = package.path .. ';". s:lazygit_lua_loc."?.lua'" + +command! LazyGit lua require'lazygit'.lazygit() + +"""""""""""""""""""""""""""""""""""""""""""""""""""""" + +let &cpoptions = s:save_cpo +unlet s:save_cpo + +let g:loaded_lazygit_vim = 1