From 14453034114cd6e14d93ca18013d20da02106106 Mon Sep 17 00:00:00 2001 From: Asger Gitz-Johansen Date: Thu, 11 Jun 2026 22:15:15 +0200 Subject: [PATCH] feat: limit the terminal width Also add the possibility of configuring it. --- README.md | 18 +++++++++++++++--- lua/opencode.lua | 37 ++++++++++++++++++++++++++++++++++--- 2 files changed, 49 insertions(+), 6 deletions(-) diff --git a/README.md b/README.md index f4168c2..cf92482 100644 --- a/README.md +++ b/README.md @@ -10,6 +10,18 @@ Run: :OpenCodeToggle ``` -The command opens `opencode` in a right-side terminal window. Running the command -again closes only the window; the terminal buffer and `opencode` session stay -alive. Run `:OpenCodeToggle` again to show the same session. +The command opens `opencode` in a right-side terminal window sized to 40% of the +screen width. Running the command again closes only the window; the terminal +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`. diff --git a/lua/opencode.lua b/lua/opencode.lua index d781fd6..67e9f1f 100644 --- a/lua/opencode.lua +++ b/lua/opencode.lua @@ -6,6 +6,10 @@ local state = { job = nil, } +local config = { + width = 0.4, +} + local function is_valid_window(win) return win and vim.api.nvim_win_is_valid(win) end @@ -14,8 +18,18 @@ local function is_valid_buffer(buf) return buf and vim.api.nvim_buf_is_valid(buf) 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() - vim.cmd("botright vertical 80new") + vim.cmd("botright vertical " .. terminal_width() .. "new") state.win = vim.api.nvim_get_current_win() end @@ -51,10 +65,27 @@ function M.toggle() vim.cmd("startinsert") 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() 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 return M