Skip to content

Commit e942ac9

Browse files
feat: add configurable terminal persistence for native provider
Adds preserve_on_close option to terminal configuration that allows users to preserve terminal processes when windows are manually closed. Defaults to false for backward compatibility. - Add preserve_on_close config option (default: false) - Update native provider to use configurable buffer behavior - Fix hide_terminal to preserve process during programmatic hiding - Update README with new configuration option and documentation This provides the same functionality as the snacks provider fix but makes it configurable so users can choose their preferred behavior.
1 parent 3bfaf39 commit e942ac9

File tree

3 files changed

+13
-3
lines changed

3 files changed

+13
-3
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -324,6 +324,7 @@ For most users, the default configuration is sufficient:
324324
split_width_percentage = 0.30, -- Width as percentage (0.0 to 1.0)
325325
provider = "auto", -- "auto", "snacks", or "native"
326326
show_native_term_exit_tip = true, -- Show exit tip for native terminal
327+
preserve_on_close = false, -- Preserve terminal process when window is closed (native provider only)
327328
auto_close = true, -- Auto-close terminal after command completion
328329
},
329330

@@ -369,6 +370,7 @@ For most users, the default configuration is sufficient:
369370
- `"snacks"`: Force snacks.nvim (requires folke/snacks.nvim)
370371
- `"native"`: Use built-in Neovim terminal
371372
- **`show_native_term_exit_tip`**: Show help text for exiting native terminal
373+
- **`preserve_on_close`**: When `true`, preserves the terminal process if the window is manually closed (native provider only, default: `false`)
372374
- **`auto_close`**: Automatically close terminal when commands finish
373375

374376
#### Diff Options

lua/claudecode/terminal.lua

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ local config = {
2222
split_width_percentage = 0.30,
2323
provider = "auto",
2424
show_native_term_exit_tip = true,
25+
preserve_on_close = false, -- When true, preserves terminal process when window is closed
2526
terminal_cmd = nil,
2627
auto_close = true,
2728
}
@@ -179,6 +180,7 @@ end
179180
-- @field user_term_config.split_width_percentage number Percentage of screen width (0.0 to 1.0, default: 0.30).
180181
-- @field user_term_config.provider string 'snacks' or 'native' (default: 'snacks').
181182
-- @field user_term_config.show_native_term_exit_tip boolean Show tip for exiting native terminal (default: true).
183+
-- @field user_term_config.preserve_on_close boolean Preserve terminal process when window is closed (default: false).
182184
-- @param p_terminal_cmd string|nil The command to run in the terminal (from main config).
183185
function M.setup(user_term_config, p_terminal_cmd)
184186
if user_term_config == nil then -- Allow nil, default to empty table silently
@@ -208,6 +210,8 @@ function M.setup(user_term_config, p_terminal_cmd)
208210
config[k] = v
209211
elseif k == "show_native_term_exit_tip" and type(v) == "boolean" then
210212
config[k] = v
213+
elseif k == "preserve_on_close" and type(v) == "boolean" then
214+
config[k] = v
211215
elseif k == "auto_close" and type(v) == "boolean" then
212216
config[k] = v
213217
else

lua/claudecode/terminal/native.lua

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,9 @@ local function open_terminal(cmd_string, env_table, effective_config, focus)
125125

126126
winid = new_winid
127127
bufnr = vim.api.nvim_get_current_buf()
128-
vim.bo[bufnr].bufhidden = "wipe" -- Wipe buffer when hidden (e.g., window closed)
128+
-- Configure buffer behavior when window is closed
129+
local bufhidden_value = config.preserve_on_close and "hide" or "wipe"
130+
vim.bo[bufnr].bufhidden = bufhidden_value
129131
-- buftype=terminal is set by termopen
130132

131133
if focus then
@@ -185,8 +187,10 @@ end
185187
local function hide_terminal()
186188
-- Hide the terminal window but keep the buffer and job alive
187189
if bufnr and vim.api.nvim_buf_is_valid(bufnr) and winid and vim.api.nvim_win_is_valid(winid) then
188-
-- Set buffer to hide instead of being wiped when window closes
189-
vim.api.nvim_buf_set_option(bufnr, "bufhidden", "hide")
190+
-- If preserve_on_close is false (wipe mode), temporarily set to hide to preserve process
191+
if not config.preserve_on_close then
192+
vim.api.nvim_buf_set_option(bufnr, "bufhidden", "hide")
193+
end
190194

191195
-- Close the window - this preserves the buffer and job
192196
vim.api.nvim_win_close(winid, false)

0 commit comments

Comments
 (0)