forked from nickjvandyke/opencode.nvim
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathterminal.lua
More file actions
89 lines (78 loc) · 2.45 KB
/
Copy pathterminal.lua
File metadata and controls
89 lines (78 loc) · 2.45 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
---Provide an embedded `opencode` via a [Neovim terminal](https://neovim.io/doc/user/terminal.html) buffer.
---@class opencode.provider.Terminal : opencode.Provider
---
---@field opts opencode.provider.terminal.Opts
---
---@field bufnr? integer
---@field winid? integer
local Terminal = {}
Terminal.__index = Terminal
Terminal.name = "terminal"
---@class opencode.provider.terminal.Opts : vim.api.keyset.win_config
function Terminal.new(opts)
local self = setmetatable({}, Terminal)
self.opts = opts or {}
self.winid = nil
self.bufnr = nil
return self
end
function Terminal.health()
return true
end
---Start if not running, else hide/show the window.
function Terminal:toggle()
if self.bufnr == nil then
self:start()
else
if self.winid ~= nil and vim.api.nvim_win_is_valid(self.winid) then
-- Hide the window
vim.api.nvim_win_hide(self.winid)
self.winid = nil
elseif self.bufnr ~= nil and vim.api.nvim_buf_is_valid(self.bufnr) then
-- Show the window
local previous_win = vim.api.nvim_get_current_win()
self.winid = vim.api.nvim_open_win(self.bufnr, true, self.opts)
vim.api.nvim_set_current_win(previous_win)
end
end
end
---Open a window with a terminal buffer.
function Terminal:start()
if self.bufnr == nil then
local previous_win = vim.api.nvim_get_current_win()
self.bufnr = vim.api.nvim_create_buf(true, false)
self.winid = vim.api.nvim_open_win(self.bufnr, true, self.opts)
-- Redraw terminal buffer on initial render.
-- Fixes empty columns on the right side.
local auid
auid = vim.api.nvim_create_autocmd("TermRequest", {
buffer = self.bufnr,
callback = function(ev)
if ev.data.cursor[1] > 1 then
vim.api.nvim_del_autocmd(auid)
vim.api.nvim_set_current_win(self.winid)
vim.cmd([[startinsert | call feedkeys("\<C-\>\<C-n>\<C-w>p", "n")]])
end
end,
})
vim.fn.jobstart(self.cmd, {
term = true,
on_exit = function()
self.winid = nil
self.bufnr = nil
end,
})
vim.api.nvim_set_current_win(previous_win)
end
end
---Close the window, delete the buffer.
function Terminal:stop()
if self.winid ~= nil and vim.api.nvim_win_is_valid(self.winid) then
vim.api.nvim_win_close(self.winid, true)
self.winid = nil
end
if self.bufnr ~= nil and vim.api.nvim_buf_is_valid(self.bufnr) then
vim.api.nvim_buf_delete(self.bufnr, { force = true })
end
end
return Terminal