|
1 | 1 | local M = {}
|
| 2 | +local response_win = nil |
| 3 | +local buffers = {} |
| 4 | +local MAX_BUFFERS = 10 |
| 5 | + |
| 6 | +local function get_timestamp() |
| 7 | + return os.date("%H:%M:%S") |
| 8 | +end |
| 9 | + |
| 10 | +local function navigate_buffers(direction) |
| 11 | + if not response_win or not vim.api.nvim_win_is_valid(response_win) then |
| 12 | + return |
| 13 | + end |
| 14 | + |
| 15 | + local current_buf = vim.api.nvim_win_get_buf(response_win) |
| 16 | + local current_idx = nil |
| 17 | + |
| 18 | + -- Find current buffer index |
| 19 | + for i, buf in ipairs(buffers) do |
| 20 | + if buf == current_buf then |
| 21 | + current_idx = i |
| 22 | + break |
| 23 | + end |
| 24 | + end |
| 25 | + |
| 26 | + if not current_idx then return end |
| 27 | + |
| 28 | + -- Calculate next buffer index |
| 29 | + local next_idx |
| 30 | + if direction == 'next' then |
| 31 | + next_idx = current_idx == 1 and #buffers or current_idx - 1 |
| 32 | + else |
| 33 | + next_idx = current_idx == #buffers and 1 or current_idx + 1 |
| 34 | + end |
| 35 | + |
| 36 | + -- Set the buffer in our response window |
| 37 | + if buffers[next_idx] and vim.api.nvim_buf_is_valid(buffers[next_idx]) then |
| 38 | + vim.api.nvim_win_set_buf(response_win, buffers[next_idx]) |
| 39 | + end |
| 40 | +end |
| 41 | + |
| 42 | +local function create_response_buffer() |
| 43 | + local buf = vim.api.nvim_create_buf(false, true) |
| 44 | + vim.api.nvim_buf_set_option(buf, 'buftype', 'nofile') |
| 45 | + vim.api.nvim_buf_set_option(buf, 'swapfile', false) |
| 46 | + vim.api.nvim_buf_set_option(buf, 'bufhidden', 'hide') |
| 47 | + vim.api.nvim_buf_set_option(buf, 'filetype', 'http_response') |
| 48 | + |
| 49 | + -- Set buffer-local keymaps |
| 50 | + local opts = { noremap = true, silent = true, callback = function() end } |
| 51 | + vim.api.nvim_buf_set_keymap(buf, 'n', 'q', ':close<CR>', { noremap = true, silent = true }) |
| 52 | + vim.api.nvim_buf_set_keymap(buf, 'n', '<Esc>', ':close<CR>', { noremap = true, silent = true }) |
| 53 | + -- Add custom navigation commands |
| 54 | + vim.api.nvim_buf_set_keymap(buf, 'n', 'H', '', { |
| 55 | + noremap = true, |
| 56 | + silent = true, |
| 57 | + callback = function() navigate_buffers('prev') end |
| 58 | + }) |
| 59 | + vim.api.nvim_buf_set_keymap(buf, 'n', 'L', '', { |
| 60 | + noremap = true, |
| 61 | + silent = true, |
| 62 | + callback = function() navigate_buffers('next') end |
| 63 | + }) |
| 64 | + |
| 65 | + -- Add to our buffer list |
| 66 | + table.insert(buffers, 1, buf) |
| 67 | + |
| 68 | + -- Remove oldest buffer if we exceed MAX_BUFFERS |
| 69 | + if #buffers > MAX_BUFFERS then |
| 70 | + local old_buf = table.remove(buffers) |
| 71 | + if vim.api.nvim_buf_is_valid(old_buf) then |
| 72 | + vim.api.nvim_buf_delete(old_buf, { force = true }) |
| 73 | + end |
| 74 | + end |
| 75 | + |
| 76 | + return buf |
| 77 | +end |
| 78 | + |
| 79 | +local function get_response_win() |
| 80 | + if response_win and vim.api.nvim_win_is_valid(response_win) then |
| 81 | + return response_win |
| 82 | + end |
| 83 | + |
| 84 | + -- Find any existing window with our buffers |
| 85 | + for _, buf in pairs(buffers) do |
| 86 | + if vim.api.nvim_buf_is_valid(buf) then |
| 87 | + for _, win in ipairs(vim.api.nvim_list_wins()) do |
| 88 | + if vim.api.nvim_win_get_buf(win) == buf then |
| 89 | + response_win = win |
| 90 | + return win |
| 91 | + end |
| 92 | + end |
| 93 | + end |
| 94 | + end |
| 95 | + |
| 96 | + return nil |
| 97 | +end |
| 98 | + |
| 99 | +local function create_response_win() |
| 100 | + local split_direction = require('http_client.config').get('split_direction') |
| 101 | + local split_cmd |
| 102 | + if split_direction == "right" then |
| 103 | + split_cmd = 'vsplit' |
| 104 | + elseif split_direction == "left" then |
| 105 | + split_cmd = 'leftabove vsplit' |
| 106 | + elseif split_direction == "below" then |
| 107 | + split_cmd = 'split' |
| 108 | + elseif split_direction == "above" then |
| 109 | + split_cmd = 'leftabove split' |
| 110 | + else |
| 111 | + split_cmd = 'vsplit' -- Default to right if invalid option |
| 112 | + end |
| 113 | + |
| 114 | + -- Store current window to return to it |
| 115 | + local current_win = vim.api.nvim_get_current_win() |
| 116 | + |
| 117 | + vim.cmd(split_cmd) |
| 118 | + response_win = vim.api.nvim_get_current_win() |
| 119 | + |
| 120 | + -- Return to original window |
| 121 | + vim.api.nvim_set_current_win(current_win) |
| 122 | + |
| 123 | + return response_win |
| 124 | +end |
2 | 125 |
|
3 | 126 | function M.display_in_buffer(content, title)
|
4 | 127 | vim.schedule(function()
|
5 |
| - -- Create a new buffer |
6 |
| - local buf = vim.api.nvim_create_buf(false, true) |
7 |
| - vim.api.nvim_buf_set_option(buf, 'buftype', 'nofile') |
8 |
| - vim.api.nvim_buf_set_option(buf, 'swapfile', false) |
9 |
| - vim.api.nvim_buf_set_option(buf, 'bufhidden', 'wipe') |
10 |
| - |
11 |
| - -- Set buffer name |
12 |
| - local buf_name = title .. ' ' .. os.time() |
13 |
| - pcall(vim.api.nvim_buf_set_name, buf, buf_name) |
| 128 | + local timestamp = get_timestamp() |
| 129 | + -- Create new buffer for this response |
| 130 | + local buf = create_response_buffer() |
14 | 131 |
|
15 |
| - -- Set buffer content |
16 |
| - vim.api.nvim_buf_set_lines(buf, 0, -1, false, vim.split(content, '\n')) |
| 132 | + -- Try to get existing window or create new one |
| 133 | + local win = get_response_win() or create_response_win() |
17 | 134 |
|
18 |
| - -- Open in a vertical split |
19 |
| - local split_cmd |
20 |
| - local split_direction = require('http_client.config').get('split_direction') |
21 |
| - if split_direction == "right" then |
22 |
| - split_cmd = 'vsplit' |
23 |
| - elseif split_direction == "left" then |
24 |
| - split_cmd = 'leftabove vsplit' |
25 |
| - elseif split_direction == "below" then |
26 |
| - split_cmd = 'split' |
27 |
| - elseif split_direction == "above" then |
28 |
| - split_cmd = 'leftabove split' |
29 |
| - else |
30 |
| - split_cmd = 'vsplit' -- Default to right if invalid option |
31 |
| - end |
| 135 | + -- Set buffer name with timestamp |
| 136 | + local buf_name = string.format("[%s] %s", timestamp, title) |
| 137 | + pcall(vim.api.nvim_buf_set_name, buf, buf_name) |
32 | 138 |
|
33 |
| - vim.cmd(split_cmd) |
34 |
| - local win = vim.api.nvim_get_current_win() |
35 |
| - vim.api.nvim_win_set_buf(win, buf) |
| 139 | + -- Make buffer modifiable |
| 140 | + vim.api.nvim_buf_set_option(buf, 'modifiable', true) |
| 141 | + |
| 142 | + -- Set content |
| 143 | + vim.api.nvim_buf_set_lines(buf, 0, -1, false, vim.split(content, '\n')) |
36 | 144 |
|
37 | 145 | -- Set buffer to readonly
|
38 | 146 | vim.api.nvim_buf_set_option(buf, 'modifiable', false)
|
39 | 147 | vim.api.nvim_buf_set_option(buf, 'readonly', true)
|
40 | 148 |
|
41 |
| - -- Set buffer-local keymaps |
42 |
| - local opts = { noremap = true, silent = true } |
43 |
| - vim.api.nvim_buf_set_keymap(buf, 'n', 'q', ':close<CR>', opts) |
44 |
| - vim.api.nvim_buf_set_keymap(buf, 'n', '<Esc>', ':close<CR>', opts) |
45 |
| - |
46 |
| - vim.api.nvim_buf_set_option(buf, 'filetype', 'http_response') |
| 149 | + -- Set buffer in window |
| 150 | + vim.api.nvim_win_set_buf(win, buf) |
47 | 151 | end)
|
48 | 152 | end
|
49 | 153 |
|
|
0 commit comments