-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathgitui.lua
More file actions
97 lines (86 loc) · 2.14 KB
/
gitui.lua
File metadata and controls
97 lines (86 loc) · 2.14 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
local M = {}
local state = {
buf = nil,
win = nil,
}
local function win_opts()
local columns = vim.o.columns
local lines = vim.o.lines
local width = math.floor(columns * 0.9)
local height = math.floor(lines * 0.9)
return {
relative = "editor",
style = "minimal",
row = math.floor((lines - height) * 0.5),
col = math.floor((columns - width) * 0.5),
width = width,
height = height,
focusable = true,
border = "rounded",
title = "GitUI",
title_pos = "center",
}
end
local function close_window(force)
if state.win and vim.api.nvim_win_is_valid(state.win) then
pcall(vim.api.nvim_win_close, state.win, force or false)
end
state.win = nil
end
local function clear_buffer()
if state.buf and vim.api.nvim_buf_is_valid(state.buf) then
vim.api.nvim_buf_delete(state.buf, { force = true })
end
state.buf = nil
end
local function reset_state()
close_window(true)
clear_buffer()
state.job = nil
end
function M.gitui()
if vim.api.nvim_get_mode().mode == "i" then
vim.cmd("stopinsert")
end
if state.buf ~= nil then
if state.win ~= nil then
close_window(true)
return
end
state.win = vim.api.nvim_open_win(state.buf, true, win_opts())
return
end
state.buf = vim.api.nvim_create_buf(false, true)
state.win = vim.api.nvim_open_win(state.buf, true, win_opts())
vim.bo[state.buf].modified = false
vim.b[state.buf].q_close = false
vim.api.nvim_create_autocmd("WinLeave", {
buffer = state.buf,
callback = function()
close_window(false)
end,
})
vim.api.nvim_create_autocmd({ "TermOpen", "BufEnter" }, {
buffer = state.buf,
command = "startinsert!",
})
local job_opts = {
term = true,
on_exit = function()
reset_state()
end,
}
local ok, job_or_err = pcall(vim.fn.jobstart, { "gitui" }, job_opts)
if not ok then
reset_state()
vim.notify(("gitui failed to start: %s"):format(job_or_err), vim.log.levels.ERROR)
return
end
if job_or_err <= 0 then
reset_state()
vim.notify("gitui failed to start: invalid job id", vim.log.levels.ERROR)
return
end
state.job = job_or_err
end
return M