@@ -124,10 +124,39 @@ function M.close()
124
124
end
125
125
end
126
126
127
+ --- Simple toggle: always show/hide terminal regardless of focus
127
128
--- @param cmd_string string
128
129
--- @param env_table table
129
130
--- @param config table
130
- function M .toggle (cmd_string , env_table , config )
131
+ function M .simple_toggle (cmd_string , env_table , config )
132
+ if not is_available () then
133
+ vim .notify (" Snacks.nvim terminal provider selected but Snacks.terminal not available." , vim .log .levels .ERROR )
134
+ return
135
+ end
136
+
137
+ local logger = require (" claudecode.logger" )
138
+
139
+ -- Check if terminal exists and is visible
140
+ if terminal and terminal :buf_valid () and terminal .win then
141
+ -- Terminal is visible, hide it
142
+ logger .debug (" terminal" , " Simple toggle: hiding visible terminal" )
143
+ terminal :toggle ()
144
+ elseif terminal and terminal :buf_valid () and not terminal .win then
145
+ -- Terminal exists but not visible, show it
146
+ logger .debug (" terminal" , " Simple toggle: showing hidden terminal" )
147
+ terminal :toggle ()
148
+ else
149
+ -- No terminal exists, create new one
150
+ logger .debug (" terminal" , " Simple toggle: creating new terminal" )
151
+ M .open (cmd_string , env_table , config )
152
+ end
153
+ end
154
+
155
+ --- Smart focus toggle: switches to terminal if not focused, hides if currently focused
156
+ --- @param cmd_string string
157
+ --- @param env_table table
158
+ --- @param config table
159
+ function M .focus_toggle (cmd_string , env_table , config )
131
160
if not is_available () then
132
161
vim .notify (" Snacks.nvim terminal provider selected but Snacks.terminal not available." , vim .log .levels .ERROR )
133
162
return
@@ -137,7 +166,7 @@ function M.toggle(cmd_string, env_table, config)
137
166
138
167
-- Terminal exists, is valid, but not visible
139
168
if terminal and terminal :buf_valid () and not terminal .win then
140
- logger .debug (" terminal" , " Toggle existing managed Snacks terminal" )
169
+ logger .debug (" terminal" , " Focus toggle: showing hidden terminal" )
141
170
terminal :toggle ()
142
171
-- Terminal exists, is valid, and is visible
143
172
elseif terminal and terminal :buf_valid () and terminal .win then
@@ -146,9 +175,11 @@ function M.toggle(cmd_string, env_table, config)
146
175
147
176
-- you're IN it
148
177
if claude_term_neovim_win_id == current_neovim_win_id then
178
+ logger .debug (" terminal" , " Focus toggle: hiding terminal (currently focused)" )
149
179
terminal :toggle ()
150
180
-- you're NOT in it
151
181
else
182
+ logger .debug (" terminal" , " Focus toggle: focusing terminal" )
152
183
vim .api .nvim_set_current_win (claude_term_neovim_win_id )
153
184
if terminal .buf and vim .api .nvim_buf_is_valid (terminal .buf ) then
154
185
if vim .api .nvim_buf_get_option (terminal .buf , " buftype" ) == " terminal" then
@@ -160,11 +191,19 @@ function M.toggle(cmd_string, env_table, config)
160
191
end
161
192
-- No terminal exists
162
193
else
163
- logger .debug (" terminal" , " No valid terminal exists, creating new one " )
194
+ logger .debug (" terminal" , " Focus toggle: creating new terminal " )
164
195
M .open (cmd_string , env_table , config )
165
196
end
166
197
end
167
198
199
+ --- Legacy toggle function for backward compatibility (defaults to simple_toggle)
200
+ --- @param cmd_string string
201
+ --- @param env_table table
202
+ --- @param config table
203
+ function M .toggle (cmd_string , env_table , config )
204
+ M .simple_toggle (cmd_string , env_table , config )
205
+ end
206
+
168
207
--- @return number | nil
169
208
function M .get_active_bufnr ()
170
209
if terminal and terminal :buf_valid () and terminal .buf then
0 commit comments