Skip to content

Commit ce58369

Browse files
authored
feat: add terminal_enabled config flag for the built in nvim terminal
1 parent 91357d8 commit ce58369

File tree

6 files changed

+322
-56
lines changed

6 files changed

+322
-56
lines changed

lua/claudecode/config.lua

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ M.defaults = {
66
port_range = { min = 10000, max = 65535 },
77
auto_start = true,
88
terminal_cmd = nil,
9+
enable_terminal = true, -- Enable built-in terminal integration (set to false to use external terminal like tmux)
910
log_level = "info",
1011
track_selection = true,
1112
visual_demotion_delay_ms = 50, -- Milliseconds to wait before demoting a visual selection
@@ -51,6 +52,8 @@ function M.validate(config)
5152

5253
assert(type(config.track_selection) == "boolean", "track_selection must be a boolean")
5354

55+
assert(type(config.enable_terminal) == "boolean", "enable_terminal must be a boolean")
56+
5457
assert(
5558
type(config.visual_demotion_delay_ms) == "number" and config.visual_demotion_delay_ms >= 0,
5659
"visual_demotion_delay_ms must be a non-negative number"

lua/claudecode/init.lua

Lines changed: 62 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -260,7 +260,7 @@ function M.send_at_mention(file_path, start_line, end_line, context)
260260
if M.is_claude_connected() then
261261
-- Claude is connected, send immediately and ensure terminal is visible
262262
local success, error_msg = M._broadcast_at_mention(file_path, start_line, end_line)
263-
if success then
263+
if success and M.state.config.enable_terminal then
264264
local terminal = require("claudecode.terminal")
265265
terminal.ensure_visible()
266266
end
@@ -276,9 +276,11 @@ function M.send_at_mention(file_path, start_line, end_line, context)
276276

277277
queue_at_mention(mention_data)
278278

279-
-- Launch terminal with Claude Code
280-
local terminal = require("claudecode.terminal")
281-
terminal.open()
279+
-- Launch terminal with Claude Code if enabled
280+
if M.state.config.enable_terminal then
281+
local terminal = require("claudecode.terminal")
282+
terminal.open()
283+
end
282284

283285
logger.debug(context, "Queued @ mention and launched Claude Code: " .. file_path)
284286

@@ -307,15 +309,17 @@ function M.setup(opts)
307309

308310
-- Setup terminal module: always try to call setup to pass terminal_cmd,
309311
-- even if terminal_opts (for split_side etc.) are not provided.
310-
local terminal_setup_ok, terminal_module = pcall(require, "claudecode.terminal")
311-
if terminal_setup_ok then
312-
-- Guard in case tests or user replace the module with a minimal stub without `setup`.
313-
if type(terminal_module.setup) == "function" then
314-
-- terminal_opts might be nil, which the setup function should handle gracefully.
315-
terminal_module.setup(terminal_opts, M.state.config.terminal_cmd)
312+
if M.state.config.enable_terminal then
313+
local terminal_setup_ok, terminal_module = pcall(require, "claudecode.terminal")
314+
if terminal_setup_ok then
315+
-- Guard in case tests or user replace the module with a minimal stub without `setup`.
316+
if type(terminal_module.setup) == "function" then
317+
-- terminal_opts might be nil, which the setup function should handle gracefully.
318+
terminal_module.setup(terminal_opts, M.state.config.terminal_cmd)
319+
end
320+
else
321+
logger.error("init", "Failed to load claudecode.terminal module for setup.")
316322
end
317-
else
318-
logger.error("init", "Failed to load claudecode.terminal module for setup.")
319323
end
320324

321325
local diff = require("claudecode.diff")
@@ -882,50 +886,52 @@ function M._create_commands()
882886
desc = "Add specified file or directory to Claude Code context with optional line range",
883887
})
884888

885-
local terminal_ok, terminal = pcall(require, "claudecode.terminal")
886-
if terminal_ok then
887-
vim.api.nvim_create_user_command("ClaudeCode", function(opts)
888-
local current_mode = vim.fn.mode()
889-
if current_mode == "v" or current_mode == "V" or current_mode == "\22" then
890-
vim.api.nvim_feedkeys(vim.api.nvim_replace_termcodes("<Esc>", true, false, true), "n", false)
891-
end
892-
local cmd_args = opts.args and opts.args ~= "" and opts.args or nil
893-
terminal.simple_toggle({}, cmd_args)
894-
end, {
895-
nargs = "*",
896-
desc = "Toggle the Claude Code terminal window (simple show/hide) with optional arguments",
897-
})
898-
899-
vim.api.nvim_create_user_command("ClaudeCodeFocus", function(opts)
900-
local current_mode = vim.fn.mode()
901-
if current_mode == "v" or current_mode == "V" or current_mode == "\22" then
902-
vim.api.nvim_feedkeys(vim.api.nvim_replace_termcodes("<Esc>", true, false, true), "n", false)
903-
end
904-
local cmd_args = opts.args and opts.args ~= "" and opts.args or nil
905-
terminal.focus_toggle({}, cmd_args)
906-
end, {
907-
nargs = "*",
908-
desc = "Smart focus/toggle Claude Code terminal (switches to terminal if not focused, hides if focused)",
909-
})
910-
911-
vim.api.nvim_create_user_command("ClaudeCodeOpen", function(opts)
912-
local cmd_args = opts.args and opts.args ~= "" and opts.args or nil
913-
terminal.open({}, cmd_args)
914-
end, {
915-
nargs = "*",
916-
desc = "Open the Claude Code terminal window with optional arguments",
917-
})
918-
919-
vim.api.nvim_create_user_command("ClaudeCodeClose", function()
920-
terminal.close()
921-
end, {
922-
desc = "Close the Claude Code terminal window",
923-
})
924-
else
925-
logger.error(
926-
"init",
927-
"Terminal module not found. Terminal commands (ClaudeCode, ClaudeCodeOpen, ClaudeCodeClose) not registered."
928-
)
889+
if M.state.config.enable_terminal then
890+
local terminal_ok, terminal = pcall(require, "claudecode.terminal")
891+
if terminal_ok then
892+
vim.api.nvim_create_user_command("ClaudeCode", function(opts)
893+
local current_mode = vim.fn.mode()
894+
if current_mode == "v" or current_mode == "V" or current_mode == "\22" then
895+
vim.api.nvim_feedkeys(vim.api.nvim_replace_termcodes("<Esc>", true, false, true), "n", false)
896+
end
897+
local cmd_args = opts.args and opts.args ~= "" and opts.args or nil
898+
terminal.simple_toggle({}, cmd_args)
899+
end, {
900+
nargs = "*",
901+
desc = "Toggle the Claude Code terminal window (simple show/hide) with optional arguments",
902+
})
903+
904+
vim.api.nvim_create_user_command("ClaudeCodeFocus", function(opts)
905+
local current_mode = vim.fn.mode()
906+
if current_mode == "v" or current_mode == "V" or current_mode == "\22" then
907+
vim.api.nvim_feedkeys(vim.api.nvim_replace_termcodes("<Esc>", true, false, true), "n", false)
908+
end
909+
local cmd_args = opts.args and opts.args ~= "" and opts.args or nil
910+
terminal.focus_toggle({}, cmd_args)
911+
end, {
912+
nargs = "*",
913+
desc = "Smart focus/toggle Claude Code terminal (switches to terminal if not focused, hides if focused)",
914+
})
915+
916+
vim.api.nvim_create_user_command("ClaudeCodeOpen", function(opts)
917+
local cmd_args = opts.args and opts.args ~= "" and opts.args or nil
918+
terminal.open({}, cmd_args)
919+
end, {
920+
nargs = "*",
921+
desc = "Open the Claude Code terminal window with optional arguments",
922+
})
923+
924+
vim.api.nvim_create_user_command("ClaudeCodeClose", function()
925+
terminal.close()
926+
end, {
927+
desc = "Close the Claude Code terminal window",
928+
})
929+
else
930+
logger.error(
931+
"init",
932+
"Terminal module not found. Terminal commands (ClaudeCode, ClaudeCodeOpen, ClaudeCodeClose) not registered."
933+
)
934+
end
929935
end
930936

931937
-- Diff management commands

tests/config_test.lua

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -180,6 +180,7 @@ describe("Config module", function()
180180
port_range = { min = 10000, max = 65535 },
181181
auto_start = true,
182182
terminal_cmd = "toggleterm",
183+
enable_terminal = true,
183184
log_level = "debug",
184185
track_selection = false,
185186
visual_demotion_delay_ms = 50,

tests/integration/command_args_spec.lua

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -149,9 +149,13 @@ describe("ClaudeCode command arguments integration", function()
149149
port_range = { min = 10000, max = 65535 },
150150
auto_start = false,
151151
terminal_cmd = nil,
152+
enable_terminal = true,
152153
log_level = "info",
153154
track_selection = true,
154155
visual_demotion_delay_ms = 50,
156+
connection_wait_delay = 200,
157+
connection_timeout = 10000,
158+
queue_timeout = 5000,
155159
diff_opts = {
156160
auto_close_on_accept = true,
157161
show_diff_stats = true,

tests/unit/config_spec.lua

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ describe("Configuration", function()
2929
port_range = { min = 10000, max = 65535 },
3030
auto_start = true,
3131
terminal_cmd = "toggleterm",
32+
enable_terminal = true,
3233
log_level = "debug",
3334
track_selection = false,
3435
visual_demotion_delay_ms = 50,

0 commit comments

Comments
 (0)