Skip to content

Commit e1149cb

Browse files
KKrampisjohnseth97
andauthored
mod for side panel mode and buffer copy (#12)
Co-authored-by: Konstantinos Krampis <KKrampis> Co-authored-by: EJ <17620345+johnseth97@users.noreply.github.com>
1 parent 910084b commit e1149cb

2 files changed

Lines changed: 64 additions & 15 deletions

File tree

README.md

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
> Latest version: ![GitHub tag (latest SemVer)](https://img.shields.io/github/v/tag/johnseth97/codex.nvim?sort=semver)
66
77
### Features:
8-
- ✅ Toggle Codex floating window with `:CodexToggle`
8+
- ✅ Toggle Codex window or side-panel with `:CodexToggle`
99
- ✅ Optional keymap mapping via `setup` call
1010
- ✅ Background running when window hidden
1111
- ✅ Statusline integration via `require('codex').status()`
@@ -29,14 +29,14 @@ export OPENAI_API_KEY=your_api_key
2929

3030
```lua
3131
return {
32-
'johnseth97/codex.nvim',
32+
'kkrampis/codex.nvim',
3333
lazy = true,
3434
cmd = { 'Codex', 'CodexToggle' }, -- Optional: Load only on command execution
3535
keys = {
3636
{
3737
'<leader>cc', -- Change this to your preferred keybinding
3838
function() require('codex').toggle() end,
39-
desc = 'Toggle Codex popup',
39+
desc = 'Toggle Codex popup or side-panel',
4040
mode = { 'n', 't' }
4141
},
4242
},
@@ -50,12 +50,16 @@ return {
5050
height = 0.8, -- Height of the floating window (0.0 to 1.0)
5151
model = nil, -- Optional: pass a string to use a specific model (e.g., 'o3-mini')
5252
autoinstall = true, -- Automatically install the Codex CLI if not found
53+
panel = false, -- Open Codex in a side-panel (vertical split) instead of floating window
54+
use_buffer = false, -- Capture Codex stdout into a normal buffer instead of a terminal buffer
5355
},
5456
}```
5557

5658
### Usage:
57-
- Call `:Codex` (or `:CodexToggle`) to open or close the Codex popup.
58-
-- Map your own keybindings via the `keymaps.toggle` setting.
59+
- Call `:Codex` (or `:CodexToggle`) to open or close the Codex popup or side-panel.
60+
- Map your own keybindings via the `keymaps.toggle` setting.
61+
- To choose floating popup vs side-panel, set `panel = false` (popup) or `panel = true` (panel) in your setup options.
62+
- To capture Codex output in an editable buffer instead of a terminal, set `use_buffer = true` (or `false` to keep terminal) in your setup options.
5963
- Add the following code to show backgrounded Codex window in lualine:
6064

6165
```lua
@@ -66,4 +70,3 @@ require('codex').status() -- drop in to your lualine sections
6670
- All plugin configurations can be seen in the `opts` table of the plugin setup, as shown in the installation section.
6771

6872
- **For deeper customization, please refer to the [Codex CLI documentation](https://github.com/openai/codex?tab=readme-ov-file#full-configuration-example) full configuration example. These features change quickly as Codex CLI is in active beta development.*
69-

lua/codex/init.lua

Lines changed: 55 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ local config = {
1515
cmd = 'codex',
1616
model = nil, -- Default to the latest model
1717
autoinstall = true,
18+
panel = false, -- if true, open Codex in a side-panel instead of floating window
19+
use_buffer = false, -- if true, capture Codex stdout into a normal buffer instead of a terminal
1820
}
1921

2022
function M.setup(user_config)
@@ -86,6 +88,18 @@ local function open_window()
8688
})
8789
end
8890

91+
--- Open Codex in a side-panel (vertical split) instead of floating window
92+
local function open_panel()
93+
-- Create a vertical split on the right and show the buffer
94+
vim.cmd('vertical rightbelow vsplit')
95+
local win = vim.api.nvim_get_current_win()
96+
vim.api.nvim_win_set_buf(win, state.buf)
97+
-- Adjust width according to config (percentage of total columns)
98+
local width = math.floor(vim.o.columns * config.width)
99+
vim.api.nvim_win_set_width(win, width)
100+
state.win = win
101+
end
102+
89103
function M.open()
90104
local function create_clean_buf()
91105
local buf = vim.api.nvim_create_buf(false, false)
@@ -128,7 +142,7 @@ function M.open()
128142
'You can install manually with:',
129143
' npm install -g @openai/codex',
130144
})
131-
open_window()
145+
if config.panel then open_panel() else open_window() end
132146
end
133147
end)
134148
return
@@ -145,7 +159,7 @@ function M.open()
145159
'',
146160
'Or enable autoinstall in setup: require("codex").setup{ autoinstall = true }',
147161
})
148-
open_window()
162+
if config.panel then open_panel() else open_window() end
149163
return
150164
end
151165
end
@@ -158,21 +172,53 @@ function M.open()
158172
state.buf = create_clean_buf()
159173
end
160174

161-
open_window()
175+
if config.panel then open_panel() else open_window() end
162176

163177
if not state.job then
178+
-- assemble command
164179
local cmd_args = type(config.cmd) == 'string' and { config.cmd } or vim.deepcopy(config.cmd)
165180
if config.model then
166181
table.insert(cmd_args, '-m')
167182
table.insert(cmd_args, config.model)
168183
end
169184

170-
state.job = vim.fn.termopen(cmd_args, {
171-
cwd = vim.loop.cwd(),
172-
on_exit = function()
173-
state.job = nil
174-
end,
175-
})
185+
if config.use_buffer then
186+
-- capture stdout/stderr into normal buffer
187+
state.job = vim.fn.jobstart(cmd_args, {
188+
cwd = vim.loop.cwd(),
189+
stdout_buffered = true,
190+
on_stdout = function(_, data)
191+
if not data then return end
192+
for _, line in ipairs(data) do
193+
if line ~= '' then
194+
vim.api.nvim_buf_set_lines(state.buf, -1, -1, false, { line })
195+
end
196+
end
197+
end,
198+
on_stderr = function(_, data)
199+
if not data then return end
200+
for _, line in ipairs(data) do
201+
if line ~= '' then
202+
vim.api.nvim_buf_set_lines(state.buf, -1, -1, false, { '[ERR] ' .. line })
203+
end
204+
end
205+
end,
206+
on_exit = function(_, code)
207+
state.job = nil
208+
vim.api.nvim_buf_set_lines(state.buf, -1, -1, false, {
209+
('[Codex exit: %d]'):format(code),
210+
})
211+
end,
212+
})
213+
else
214+
-- use a terminal buffer
215+
state.job = vim.fn.termopen(cmd_args, {
216+
cwd = vim.loop.cwd(),
217+
on_exit = function()
218+
state.job = nil
219+
end,
220+
})
221+
end
176222
end
177223
end
178224

0 commit comments

Comments
 (0)