|
517 | 517 |
|
518 | 518 | return M |
519 | 519 |
|
| 520 | +--- Finds the existing Claude terminal buffer, even if it's not in a window. |
| 521 | +-- @local |
| 522 | +-- @return number|nil The buffer number if found, otherwise nil. |
| 523 | +local function find_existing_terminal_buffer_by_name() |
| 524 | + local buffers = vim.api.nvim_list_bufs() |
| 525 | + for _, buf in ipairs(buffers) do |
| 526 | + if vim.api.nvim_buf_is_valid(buf) and vim.bo[buf].buftype == 'terminal' then |
| 527 | + local buf_name = vim.api.nvim_buf_get_name(buf) |
| 528 | + if buf_name:match("claude") then |
| 529 | + return buf |
| 530 | + end |
| 531 | + end |
| 532 | + end |
| 533 | + return nil |
| 534 | +end |
| 535 | + |
| 536 | +--- Opens a window for an existing buffer. |
| 537 | +-- @local |
| 538 | +-- @param bufnr number The buffer number to open. |
| 539 | +-- @param effective_term_config table Configuration for split_side and split_width_percentage. |
| 540 | +local function open_window_for_buffer(bufnr, effective_term_config) |
| 541 | + local original_win = vim.api.nvim_get_current_win() |
| 542 | + |
| 543 | + local width = math.floor(vim.o.columns * effective_term_config.split_width_percentage) |
| 544 | + local full_height = vim.o.lines |
| 545 | + local placement_modifier |
| 546 | + |
| 547 | + if effective_term_config.split_side == "left" then |
| 548 | + placement_modifier = "topleft " |
| 549 | + else |
| 550 | + placement_modifier = "botright " |
| 551 | + end |
| 552 | + |
| 553 | + vim.cmd(placement_modifier .. width .. "vsplit") |
| 554 | + |
| 555 | + local new_winid = vim.api.nvim_get_current_win() |
| 556 | + |
| 557 | + vim.api.nvim_win_set_height(new_winid, full_height) |
| 558 | + |
| 559 | + vim.api.nvim_win_set_buf(new_winid, bufnr) |
| 560 | + |
| 561 | + managed_fallback_terminal_winid = new_winid |
| 562 | + managed_fallback_terminal_bufnr = bufnr |
| 563 | + |
| 564 | + vim.api.nvim_set_current_win(managed_fallback_terminal_winid) |
| 565 | + vim.cmd("startinsert") |
| 566 | +end |
| 567 | + |
0 commit comments