feat: limit the terminal width
Also add the possibility of configuring it.
This commit is contained in:
@@ -10,6 +10,18 @@ Run:
|
|||||||
:OpenCodeToggle
|
:OpenCodeToggle
|
||||||
```
|
```
|
||||||
|
|
||||||
The command opens `opencode` in a right-side terminal window. Running the command
|
The command opens `opencode` in a right-side terminal window sized to 40% of the
|
||||||
again closes only the window; the terminal buffer and `opencode` session stay
|
screen width. Running the command again closes only the window; the terminal
|
||||||
alive. Run `:OpenCodeToggle` again to show the same session.
|
buffer and `opencode` session stay alive. Run `:OpenCodeToggle` again to show the
|
||||||
|
same session.
|
||||||
|
|
||||||
|
## Configuration
|
||||||
|
|
||||||
|
```lua
|
||||||
|
require("opencode").setup({
|
||||||
|
width = 0.4,
|
||||||
|
})
|
||||||
|
```
|
||||||
|
|
||||||
|
`width` is the fraction of the screen width used by the terminal window. The
|
||||||
|
default is `0.4`.
|
||||||
|
|||||||
+34
-3
@@ -6,6 +6,10 @@ local state = {
|
|||||||
job = nil,
|
job = nil,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
local config = {
|
||||||
|
width = 0.4,
|
||||||
|
}
|
||||||
|
|
||||||
local function is_valid_window(win)
|
local function is_valid_window(win)
|
||||||
return win and vim.api.nvim_win_is_valid(win)
|
return win and vim.api.nvim_win_is_valid(win)
|
||||||
end
|
end
|
||||||
@@ -14,8 +18,18 @@ local function is_valid_buffer(buf)
|
|||||||
return buf and vim.api.nvim_buf_is_valid(buf)
|
return buf and vim.api.nvim_buf_is_valid(buf)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
local function terminal_width()
|
||||||
|
return math.max(1, math.floor(vim.o.columns * config.width))
|
||||||
|
end
|
||||||
|
|
||||||
|
local function resize_side_window()
|
||||||
|
if is_valid_window(state.win) then
|
||||||
|
vim.api.nvim_win_set_width(state.win, terminal_width())
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
local function open_side_window()
|
local function open_side_window()
|
||||||
vim.cmd("botright vertical 80new")
|
vim.cmd("botright vertical " .. terminal_width() .. "new")
|
||||||
state.win = vim.api.nvim_get_current_win()
|
state.win = vim.api.nvim_get_current_win()
|
||||||
end
|
end
|
||||||
|
|
||||||
@@ -51,10 +65,27 @@ function M.toggle()
|
|||||||
vim.cmd("startinsert")
|
vim.cmd("startinsert")
|
||||||
end
|
end
|
||||||
|
|
||||||
function M.setup()
|
function M.setup(opts)
|
||||||
|
opts = opts or {}
|
||||||
|
|
||||||
|
if opts.width ~= nil then
|
||||||
|
assert(
|
||||||
|
type(opts.width) == "number" and opts.width > 0 and opts.width <= 1,
|
||||||
|
"opencode.nvim: width must be a number between 0 and 1"
|
||||||
|
)
|
||||||
|
|
||||||
|
config.width = opts.width
|
||||||
|
resize_side_window()
|
||||||
|
end
|
||||||
|
|
||||||
vim.api.nvim_create_user_command("OpenCodeToggle", function()
|
vim.api.nvim_create_user_command("OpenCodeToggle", function()
|
||||||
M.toggle()
|
M.toggle()
|
||||||
end, {})
|
end, { force = true })
|
||||||
|
|
||||||
|
vim.api.nvim_create_autocmd("VimResized", {
|
||||||
|
group = vim.api.nvim_create_augroup("opencode_terminal_width", { clear = true }),
|
||||||
|
callback = resize_side_window,
|
||||||
|
})
|
||||||
end
|
end
|
||||||
|
|
||||||
return M
|
return M
|
||||||
|
|||||||
Reference in New Issue
Block a user