forked from johnseth97/codex.nvim
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinit.lua
More file actions
265 lines (236 loc) · 7.45 KB
/
init.lua
File metadata and controls
265 lines (236 loc) · 7.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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
local vim = vim
local installer = require 'codex.installer'
local state = require 'codex.state'
local M = {}
local config = {
keymaps = {
toggle = nil,
quit = '<C-q>', -- Default: Ctrl+q to quit
},
border = 'single',
width = 0.8,
height = 0.8,
cmd = 'codex',
model = nil, -- Default to the latest model
autoinstall = true,
panel = false, -- if true, open Codex in a side-panel instead of floating window
use_buffer = false, -- if true, capture Codex stdout into a normal buffer instead of a terminal
}
function M.setup(user_config)
config = vim.tbl_deep_extend('force', config, user_config or {})
vim.api.nvim_create_user_command('Codex', function()
M.toggle()
end, { desc = 'Toggle Codex popup' })
vim.api.nvim_create_user_command('CodexToggle', function()
M.toggle()
end, { desc = 'Toggle Codex popup (alias)' })
if config.keymaps.toggle then
vim.api.nvim_set_keymap('n', config.keymaps.toggle, '<cmd>CodexToggle<CR>', { noremap = true, silent = true })
end
end
local function open_window()
local width = math.floor(vim.o.columns * config.width)
local height = math.floor(vim.o.lines * config.height)
local row = math.floor((vim.o.lines - height) / 2)
local col = math.floor((vim.o.columns - width) / 2)
local styles = {
single = {
{ '┌', 'FloatBorder' },
{ '─', 'FloatBorder' },
{ '┐', 'FloatBorder' },
{ '│', 'FloatBorder' },
{ '┘', 'FloatBorder' },
{ '─', 'FloatBorder' },
{ '└', 'FloatBorder' },
{ '│', 'FloatBorder' },
},
double = {
{ '╔', 'FloatBorder' },
{ '═', 'FloatBorder' },
{ '╗', 'FloatBorder' },
{ '║', 'FloatBorder' },
{ '╝', 'FloatBorder' },
{ '═', 'FloatBorder' },
{ '╚', 'FloatBorder' },
{ '║', 'FloatBorder' },
},
rounded = {
{ '╭', 'FloatBorder' },
{ '─', 'FloatBorder' },
{ '╮', 'FloatBorder' },
{ '│', 'FloatBorder' },
{ '╯', 'FloatBorder' },
{ '─', 'FloatBorder' },
{ '╰', 'FloatBorder' },
{ '│', 'FloatBorder' },
},
none = nil,
}
local border = type(config.border) == 'string' and styles[config.border] or config.border
state.win = vim.api.nvim_open_win(state.buf, true, {
relative = 'editor',
width = width,
height = height,
row = row,
col = col,
style = 'minimal',
border = border,
})
end
--- Open Codex in a side-panel (vertical split) instead of floating window
local function open_panel()
-- Create a vertical split on the right and show the buffer
vim.cmd('vertical rightbelow vsplit')
local win = vim.api.nvim_get_current_win()
vim.api.nvim_win_set_buf(win, state.buf)
-- Adjust width according to config (percentage of total columns)
local width = math.floor(vim.o.columns * config.width)
vim.api.nvim_win_set_width(win, width)
state.win = win
end
function M.open()
local function create_clean_buf()
local buf = vim.api.nvim_create_buf(false, false)
vim.api.nvim_buf_set_option(buf, 'bufhidden', 'hide')
vim.api.nvim_buf_set_option(buf, 'swapfile', false)
vim.api.nvim_buf_set_option(buf, 'filetype', 'codex')
-- Apply configured quit keybinding
if config.keymaps.quit then
local quit_cmd = [[<cmd>lua require('codex').close()<CR>]]
vim.api.nvim_buf_set_keymap(buf, 't', config.keymaps.quit, [[<C-\><C-n>]] .. quit_cmd, { noremap = true, silent = true })
vim.api.nvim_buf_set_keymap(buf, 'n', config.keymaps.quit, quit_cmd, { noremap = true, silent = true })
end
return buf
end
if state.win and vim.api.nvim_win_is_valid(state.win) then
vim.api.nvim_set_current_win(state.win)
return
end
local check_cmd = type(config.cmd) == 'string' and not config.cmd:find '%s' and config.cmd or (type(config.cmd) == 'table' and config.cmd[1]) or nil
if check_cmd and vim.fn.executable(check_cmd) == 0 then
if config.autoinstall then
installer.prompt_autoinstall(function(success)
if success then
M.open() -- Try again after installing
else
-- Show failure message *after* buffer is created
if not state.buf or not vim.api.nvim_buf_is_valid(state.buf) then
state.buf = create_clean_buf()
end
vim.api.nvim_buf_set_lines(state.buf, 0, -1, false, {
'Autoinstall cancelled or failed.',
'',
'You can install manually with:',
' npm install -g @openai/codex',
})
if config.panel then open_panel() else open_window() end
end
end)
return
else
-- Show fallback message
if not state.buf or not vim.api.nvim_buf_is_valid(state.buf) then
state.buf = vim.api.nvim_create_buf(false, false)
end
vim.api.nvim_buf_set_lines(state.buf, 0, -1, false, {
'Codex CLI not found, autoinstall disabled.',
'',
'Install with:',
' npm install -g @openai/codex',
'',
'Or enable autoinstall in setup: require("codex").setup{ autoinstall = true }',
})
if config.panel then open_panel() else open_window() end
return
end
end
local function is_buf_reusable(buf)
return type(buf) == 'number' and vim.api.nvim_buf_is_valid(buf)
end
if not is_buf_reusable(state.buf) then
state.buf = create_clean_buf()
end
if config.panel then open_panel() else open_window() end
if not state.job then
-- assemble command
local cmd_args = type(config.cmd) == 'string' and { config.cmd } or vim.deepcopy(config.cmd)
if config.model then
table.insert(cmd_args, '-m')
table.insert(cmd_args, config.model)
end
if config.use_buffer then
-- capture stdout/stderr into normal buffer
state.job = vim.fn.jobstart(cmd_args, {
cwd = vim.loop.cwd(),
stdout_buffered = true,
on_stdout = function(_, data)
if not data then return end
for _, line in ipairs(data) do
if line ~= '' then
vim.api.nvim_buf_set_lines(state.buf, -1, -1, false, { line })
end
end
end,
on_stderr = function(_, data)
if not data then return end
for _, line in ipairs(data) do
if line ~= '' then
vim.api.nvim_buf_set_lines(state.buf, -1, -1, false, { '[ERR] ' .. line })
end
end
end,
on_exit = function(_, code)
state.job = nil
vim.api.nvim_buf_set_lines(state.buf, -1, -1, false, {
('[Codex exit: %d]'):format(code),
})
end,
})
else
-- use a terminal buffer
state.job = vim.fn.termopen(cmd_args, {
cwd = vim.loop.cwd(),
on_exit = function()
state.job = nil
end,
})
end
end
end
function M.close()
if state.win and vim.api.nvim_win_is_valid(state.win) then
vim.api.nvim_win_close(state.win, true)
end
state.win = nil
end
function M.toggle()
if state.win and vim.api.nvim_win_is_valid(state.win) then
M.close()
else
M.open()
end
end
function M.statusline()
if state.job and not (state.win and vim.api.nvim_win_is_valid(state.win)) then
return '[Codex]'
end
return ''
end
function M.status()
return {
function()
return M.statusline()
end,
cond = function()
return M.statusline() ~= ''
end,
icon = '',
color = { fg = '#51afef' },
}
end
return setmetatable(M, {
__call = function(_, opts)
M.setup(opts)
return M
end,
})