-
Notifications
You must be signed in to change notification settings - Fork 194
Expand file tree
/
Copy pathterminal.lua
More file actions
254 lines (224 loc) · 9.89 KB
/
terminal.lua
File metadata and controls
254 lines (224 loc) · 9.89 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
--- Module to manage a dedicated vertical split terminal for Claude Code.
-- Supports Snacks.nvim or a native Neovim terminal fallback.
-- @module claudecode.terminal
--- @class TerminalProvider
--- @field setup function
--- @field open function
--- @field close function
--- @field toggle function
--- @field get_active_bufnr function
--- @field is_available function
--- @field _get_terminal_for_test function
local M = {}
local claudecode_server_module = require("claudecode.server.init")
local config = {
split_side = "right",
split_width_percentage = 0.30,
provider = "auto",
show_native_term_exit_tip = true,
terminal_cmd = nil,
auto_close = true,
}
-- Lazy load providers
local providers = {}
--- Loads a terminal provider module
--- @param provider_name string The name of the provider to load
--- @return TerminalProvider|nil provider The provider module, or nil if loading failed
local function load_provider(provider_name)
if not providers[provider_name] then
local ok, provider = pcall(require, "claudecode.terminal." .. provider_name)
if ok then
providers[provider_name] = provider
else
return nil
end
end
return providers[provider_name]
end
--- Gets the effective terminal provider, guaranteed to return a valid provider
--- Falls back to native provider if configured provider is unavailable
--- @return TerminalProvider provider The terminal provider module (never nil)
local function get_provider()
local logger = require("claudecode.logger")
if config.provider == "auto" then
-- Try snacks first, then fallback to native silently
local snacks_provider = load_provider("snacks")
if snacks_provider and snacks_provider.is_available() then
logger.debug("terminal", "Auto-detected snacks terminal provider")
return snacks_provider
end
-- Fall through to native provider
elseif config.provider == "snacks" then
local snacks_provider = load_provider("snacks")
if snacks_provider and snacks_provider.is_available() then
return snacks_provider
else
logger.warn("terminal", "'snacks' provider configured, but Snacks.nvim not available. Falling back to 'native'.")
end
elseif config.provider == "native" then
-- noop, will use native provider as default below
logger.debug("terminal", "Using native terminal provider")
else
logger.warn("terminal", "Invalid provider configured: " .. tostring(config.provider) .. ". Defaulting to 'native'.")
end
local native_provider = load_provider("native")
if not native_provider then
error("ClaudeCode: Critical error - native terminal provider failed to load")
end
return native_provider
end
--- Builds the effective terminal configuration by merging defaults with overrides
--- @param opts_override table|nil Optional overrides for terminal appearance
--- @return table The effective terminal configuration
local function build_config(opts_override)
local effective_config = vim.deepcopy(config)
if type(opts_override) == "table" then
local validators = {
split_side = function(val)
return val == "left" or val == "right"
end,
split_width_percentage = function(val)
return type(val) == "number" and val > 0 and val < 1
end,
}
for key, val in pairs(opts_override) do
if effective_config[key] ~= nil and validators[key] and validators[key](val) then
effective_config[key] = val
end
end
end
return {
split_side = effective_config.split_side,
split_width_percentage = effective_config.split_width_percentage,
auto_close = effective_config.auto_close,
}
end
--- Gets the claude command string and necessary environment variables
--- @param cmd_args string|nil Optional arguments to append to the command
--- @return string cmd_string The command string
--- @return table env_table The environment variables table
local function get_claude_command_and_env(cmd_args)
-- Inline get_claude_command logic
local cmd_from_config = config.terminal_cmd
local base_cmd
if not cmd_from_config or cmd_from_config == "" then
base_cmd = "claude" -- Default if not configured
else
base_cmd = cmd_from_config
end
local cmd_string
if cmd_args and cmd_args ~= "" then
cmd_string = base_cmd .. " " .. cmd_args
else
cmd_string = base_cmd
end
local sse_port_value = claudecode_server_module.state.port
local env_table = {
ENABLE_IDE_INTEGRATION = "true",
FORCE_CODE_TERMINAL = "true",
}
if sse_port_value then
env_table["CLAUDE_CODE_SSE_PORT"] = tostring(sse_port_value)
end
return cmd_string, env_table
end
--- Configures the terminal module.
-- Merges user-provided terminal configuration with defaults and sets the terminal command.
-- @param user_term_config table (optional) Configuration options for the terminal.
-- @field user_term_config.split_side string 'left' or 'right' (default: 'right').
-- @field user_term_config.split_width_percentage number Percentage of screen width (0.0 to 1.0, default: 0.30).
-- @field user_term_config.provider string 'snacks' or 'native' (default: 'snacks').
-- @field user_term_config.show_native_term_exit_tip boolean Show tip for exiting native terminal (default: true).
-- @param p_terminal_cmd string|nil The command to run in the terminal (from main config).
function M.setup(user_term_config, p_terminal_cmd)
if user_term_config == nil then -- Allow nil, default to empty table silently
user_term_config = {}
elseif type(user_term_config) ~= "table" then -- Warn if it's not nil AND not a table
vim.notify("claudecode.terminal.setup expects a table or nil for user_term_config", vim.log.levels.WARN)
user_term_config = {}
end
if p_terminal_cmd == nil or type(p_terminal_cmd) == "string" then
config.terminal_cmd = p_terminal_cmd
else
vim.notify(
"claudecode.terminal.setup: Invalid terminal_cmd provided: " .. tostring(p_terminal_cmd) .. ". Using default.",
vim.log.levels.WARN
)
config.terminal_cmd = nil -- Fallback to default behavior
end
for k, v in pairs(user_term_config) do
if config[k] ~= nil and k ~= "terminal_cmd" then -- terminal_cmd is handled above
if k == "split_side" and (v == "left" or v == "right") then
config[k] = v
elseif k == "split_width_percentage" and type(v) == "number" and v > 0 and v < 1 then
config[k] = v
elseif k == "provider" and (v == "snacks" or v == "native") then
config[k] = v
elseif k == "show_native_term_exit_tip" and type(v) == "boolean" then
config[k] = v
elseif k == "auto_close" and type(v) == "boolean" then
config[k] = v
else
vim.notify("claudecode.terminal.setup: Invalid value for " .. k .. ": " .. tostring(v), vim.log.levels.WARN)
end
elseif k ~= "terminal_cmd" then -- Avoid warning for terminal_cmd if passed in user_term_config
vim.notify("claudecode.terminal.setup: Unknown configuration key: " .. k, vim.log.levels.WARN)
end
end
-- Setup providers with config
local provider = get_provider()
provider.setup(config)
end
--- Opens or focuses the Claude terminal.
-- @param opts_override table (optional) Overrides for terminal appearance (split_side, split_width_percentage).
-- @param cmd_args string|nil (optional) Arguments to append to the claude command.
function M.open(opts_override, cmd_args)
local effective_config = build_config(opts_override)
local cmd_string, claude_env_table = get_claude_command_and_env(cmd_args)
get_provider().open(cmd_string, claude_env_table, effective_config)
end
--- Closes the managed Claude terminal if it's open and valid.
function M.close()
get_provider().close()
end
--- Simple toggle: always show/hide the Claude terminal regardless of focus.
-- @param opts_override table (optional) Overrides for terminal appearance (split_side, split_width_percentage).
-- @param cmd_args string|nil (optional) Arguments to append to the claude command.
function M.simple_toggle(opts_override, cmd_args)
local effective_config = build_config(opts_override)
local cmd_string, claude_env_table = get_claude_command_and_env(cmd_args)
get_provider().simple_toggle(cmd_string, claude_env_table, effective_config)
end
--- Smart focus toggle: switches to terminal if not focused, hides if currently focused.
-- @param opts_override table (optional) Overrides for terminal appearance (split_side, split_width_percentage).
-- @param cmd_args string|nil (optional) Arguments to append to the claude command.
function M.focus_toggle(opts_override, cmd_args)
local effective_config = build_config(opts_override)
local cmd_string, claude_env_table = get_claude_command_and_env(cmd_args)
get_provider().focus_toggle(cmd_string, claude_env_table, effective_config)
end
--- Toggles the Claude terminal open or closed (legacy function - use simple_toggle or focus_toggle).
-- @param opts_override table (optional) Overrides for terminal appearance (split_side, split_width_percentage).
-- @param cmd_args string|nil (optional) Arguments to append to the claude command.
function M.toggle(opts_override, cmd_args)
-- Default to simple toggle for backward compatibility
M.simple_toggle(opts_override, cmd_args)
end
--- Gets the buffer number of the currently active Claude Code terminal.
-- This checks both Snacks and native fallback terminals.
-- @return number|nil The buffer number if an active terminal is found, otherwise nil.
function M.get_active_terminal_bufnr()
return get_provider().get_active_bufnr()
end
--- Gets the managed terminal instance for testing purposes.
-- NOTE: This function is intended for use in tests to inspect internal state.
-- The underscore prefix indicates it's not part of the public API for regular use.
-- @return snacks.terminal|nil The managed Snacks terminal instance, or nil.
function M._get_managed_terminal_for_test()
local snacks_provider = load_provider("snacks")
if snacks_provider and snacks_provider._get_terminal_for_test then
return snacks_provider._get_terminal_for_test()
end
return nil
end
return M