Regenerate nvim config
This commit is contained in:
@ -0,0 +1,127 @@
|
||||
local fn = vim.fn
|
||||
|
||||
-- store all git repositories visited in this session
|
||||
local lazygit_visited_git_repos = {}
|
||||
|
||||
-- TODO:check if the repo isa git repo
|
||||
local function append_git_repo_path(repo_path)
|
||||
if repo_path == nil or not fn.isdirectory(repo_path) then
|
||||
return
|
||||
end
|
||||
|
||||
for _, path in ipairs(lazygit_visited_git_repos) do
|
||||
if path == repo_path then
|
||||
return
|
||||
end
|
||||
end
|
||||
|
||||
table.insert(lazygit_visited_git_repos, tostring(repo_path))
|
||||
end
|
||||
|
||||
|
||||
--- Strip leading and lagging whitespace
|
||||
local function trim(str)
|
||||
return str:gsub('^%s+', ''):gsub('%s+$', '')
|
||||
end
|
||||
|
||||
|
||||
local function get_root(cwd)
|
||||
local status, job = pcall(require, 'plenary.job')
|
||||
if not status then
|
||||
return fn.system('git rev-parse --show-toplevel')
|
||||
end
|
||||
|
||||
local gitroot_job = job:new({
|
||||
'git',
|
||||
'rev-parse',
|
||||
'--show-toplevel',
|
||||
cwd=cwd
|
||||
})
|
||||
|
||||
local path, code = gitroot_job:sync()
|
||||
if (code ~= 0) then
|
||||
return nil
|
||||
end
|
||||
|
||||
return table.concat(path, "")
|
||||
end
|
||||
|
||||
--- Get project_root_dir for git repository
|
||||
local function project_root_dir()
|
||||
-- always use bash on Unix based systems.
|
||||
local oldshell = vim.o.shell
|
||||
if vim.fn.has('win32') == 0 then
|
||||
vim.o.shell = 'bash'
|
||||
end
|
||||
|
||||
local cwd = vim.loop.cwd()
|
||||
local root = get_root(cwd)
|
||||
if root == nil then
|
||||
vim.o.shell = oldshell
|
||||
return nil
|
||||
end
|
||||
|
||||
local cmd = string.format('cd "%s" && git rev-parse --show-toplevel', fn.fnamemodify(fn.resolve(fn.expand('%:p')), ':h'), root)
|
||||
-- try symlinked file location instead
|
||||
local gitdir = fn.system(cmd)
|
||||
local isgitdir = fn.matchstr(gitdir, '^fatal:.*') == ''
|
||||
|
||||
if isgitdir then
|
||||
vim.o.shell = oldshell
|
||||
append_git_repo_path(gitdir)
|
||||
return trim(gitdir)
|
||||
end
|
||||
|
||||
-- revert to old shell
|
||||
vim.o.shell = oldshell
|
||||
|
||||
local repo_path = fn.getcwd(0, 0)
|
||||
append_git_repo_path(repo_path)
|
||||
|
||||
-- just return current working directory
|
||||
return repo_path
|
||||
end
|
||||
|
||||
--- Check if lazygit is available
|
||||
local function is_lazygit_available()
|
||||
return fn.executable('lazygit') == 1
|
||||
end
|
||||
|
||||
local function is_symlink()
|
||||
local resolved = fn.resolve(fn.expand('%:p'))
|
||||
return resolved ~= fn.expand('%:p')
|
||||
end
|
||||
|
||||
local function open_or_create_config(path)
|
||||
if fn.empty(fn.glob(path)) == 1 then
|
||||
-- file does not exist
|
||||
-- check if user wants to create it
|
||||
local answer = fn.confirm(
|
||||
"File "
|
||||
.. path
|
||||
.. " does not exist.\nDo you want to create the file and populate it with the default configuration?",
|
||||
"&Yes\n&No"
|
||||
)
|
||||
if answer == 2 then
|
||||
return nil
|
||||
end
|
||||
if fn.isdirectory(fn.fnamemodify(path, ":h")) == false then
|
||||
-- directory does not exist
|
||||
fn.mkdir(fn.fnamemodify(path, ":h"), "p")
|
||||
end
|
||||
vim.cmd("edit " .. path)
|
||||
vim.cmd([[execute "silent! 0read !lazygit -c"]])
|
||||
vim.cmd([[execute "normal 1G"]])
|
||||
else
|
||||
vim.cmd("edit " .. path)
|
||||
end
|
||||
end
|
||||
|
||||
return {
|
||||
get_root = get_root,
|
||||
project_root_dir = project_root_dir,
|
||||
lazygit_visited_git_repos = lazygit_visited_git_repos,
|
||||
is_lazygit_available = is_lazygit_available,
|
||||
is_symlink = is_symlink,
|
||||
open_or_create_config = open_or_create_config,
|
||||
}
|
||||
@ -0,0 +1,89 @@
|
||||
local api = vim.api
|
||||
|
||||
--- open floating window with nice borders
|
||||
local function open_floating_window()
|
||||
local floating_window_scaling_factor = vim.g.lazygit_floating_window_scaling_factor
|
||||
|
||||
-- Why is this required?
|
||||
-- vim.g.lazygit_floating_window_scaling_factor returns different types if the value is an integer or float
|
||||
if type(floating_window_scaling_factor) == 'table' then
|
||||
floating_window_scaling_factor = floating_window_scaling_factor[false]
|
||||
end
|
||||
|
||||
local status, plenary = pcall(require, 'plenary.window.float')
|
||||
if status and vim.g.lazygit_floating_window_use_plenary and vim.g.lazygit_floating_window_use_plenary ~= 0 then
|
||||
local ret = plenary.percentage_range_window(floating_window_scaling_factor, floating_window_scaling_factor, {winblend=vim.g.lazygit_floating_window_winblend})
|
||||
return ret.win_id, ret.bufnr
|
||||
end
|
||||
|
||||
local height = math.ceil(vim.o.lines * floating_window_scaling_factor) - 1
|
||||
local width = math.ceil(vim.o.columns * floating_window_scaling_factor)
|
||||
|
||||
local row = math.ceil(vim.o.lines - height) / 2
|
||||
local col = math.ceil(vim.o.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 topleft, top, topright, right, botright, bot, botleft, left
|
||||
local window_chars = vim.g.lazygit_floating_window_border_chars
|
||||
if type(window_chars) == 'table' and #window_chars == 8 then
|
||||
topleft, top, topright, right, botright, bot, botleft, left = unpack(window_chars)
|
||||
else
|
||||
topleft, top, topright, right, botright, bot, botleft, left = '╭','─', '╮', '│', '╯','─', '╰', '│'
|
||||
end
|
||||
|
||||
local border_lines = { topleft .. string.rep(top, width) .. topright }
|
||||
local middle_line = left .. string.rep(' ', width) .. right
|
||||
for _ = 1, height do
|
||||
table.insert(border_lines, middle_line)
|
||||
end
|
||||
table.insert(border_lines, botleft .. string.rep(bot, width) .. botright)
|
||||
|
||||
-- create a unlisted scratch buffer for the border
|
||||
local border_buffer = api.nvim_create_buf(false, true)
|
||||
|
||||
-- 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, true, border_lines)
|
||||
-- create border window
|
||||
local border_window = api.nvim_open_win(border_buffer, true, border_opts)
|
||||
vim.api.nvim_set_hl(0, "LazyGitBorder", { link = "Normal", default = true })
|
||||
vim.cmd('set winhl=NormalFloat:LazyGitBorder')
|
||||
|
||||
-- create a unlisted scratch buffer
|
||||
if LAZYGIT_BUFFER == nil or vim.fn.bufwinnr(LAZYGIT_BUFFER) == -1 then
|
||||
LAZYGIT_BUFFER = api.nvim_create_buf(false, true)
|
||||
else
|
||||
LAZYGIT_LOADED = true
|
||||
end
|
||||
-- create file window, enter the window, and use the options defined in opts
|
||||
local win = api.nvim_open_win(LAZYGIT_BUFFER, true, opts)
|
||||
|
||||
vim.bo[LAZYGIT_BUFFER].filetype = 'lazygit'
|
||||
|
||||
vim.cmd('setlocal bufhidden=hide')
|
||||
vim.cmd('setlocal nocursorcolumn')
|
||||
vim.api.nvim_set_hl(0, "LazyGitFloat", { link = "Normal", default = true })
|
||||
vim.cmd('setlocal winhl=NormalFloat:LazyGitFloat')
|
||||
vim.cmd('set winblend=' .. vim.g.lazygit_floating_window_winblend)
|
||||
|
||||
-- use autocommand to ensure that the border_buffer closes at the same time as the main buffer
|
||||
local cmd = [[autocmd WinLeave <buffer> silent! execute 'hide']]
|
||||
vim.cmd(cmd)
|
||||
cmd = [[autocmd WinLeave <buffer> silent! execute 'silent bdelete! %s']]
|
||||
vim.cmd(cmd:format(border_buffer))
|
||||
|
||||
return win, LAZYGIT_BUFFER
|
||||
end
|
||||
|
||||
return {
|
||||
open_floating_window = open_floating_window,
|
||||
}
|
||||
Reference in New Issue
Block a user