Skip to content

Commit f66f271

Browse files
authored
Merge pull request #6 from heilgar/split_window
split window
2 parents 07fb32f + 9258df0 commit f66f271

File tree

3 files changed

+178
-41
lines changed

3 files changed

+178
-41
lines changed

CHANGELOG.md

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,34 @@ All notable changes to this project will be documented in this file.
55
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
66
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
77

8+
## [1.2.0] 2024-12-30
9+
### Added
10+
- Resopnse window management
11+
- New tab-based interface for resopnse history
12+
- Timestamp-based tab naming
13+
- Navigation between response with H/L keys
14+
- Maximum 10 most recent responses preserved
15+
### Changed
16+
- Response display now uses a single split window
17+
- All responses appear in the same window as tabs
18+
- Improved window management and cleanup
19+
- Better response history organization
20+
21+
- Optimized buffer management for response history
22+
- Enhanced navigation between response tabs
23+
24+
### Improved
25+
- Response window behavior and consistency
26+
- Buffer cleanup and management
27+
- Tab navigation user experience
28+
29+
## [1.1.1] 2024-12-10
30+
### Changed
31+
- Added `create_keybindings` option to control creation of default keybindings
32+
- Modified plugin initialization to prevent automatic setup with defaults
33+
- Default keybindings are now configurable and can be disabled completely
34+
- Fixed plugin setup to properly handle user configurations
35+
836
## [1.1.0] 2024-09-17
937
### Added
1038
- New feature: Generate and display curl command in dry run output

README.md

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,7 @@ The core goal is to ensure compatibility with .http files from IntelliJ or VSCod
1212
## Table of Contents
1313

1414
- [Features](#features)
15-
- [Installation](#installation)
16-
- [Configuration](#configuration)
15+
- [Installation and configuration](#installation)
1716
- [Usage](#usage)
1817
- [Commands](#commands)
1918
- [Keybindings](#keybindings)
@@ -44,7 +43,7 @@ The core goal is to ensure compatibility with .http files from IntelliJ or VSCod
4443
- Telescope integration for environment selection
4544
- Compatible with [JetBrains HTTP Client](https://www.jetbrains.com/help/idea/http-client-in-product-code-editor.html) and [VSCode Restclient](https://github.com/Huachao/vscode-restclient)
4645

47-
## Installation
46+
## Installation and Configuration
4847

4948
This plugin is designed to be installed with [Lazy.nvim](https://github.com/folke/lazy.nvim).
5049

@@ -58,14 +57,11 @@ Add the following to your Neovim configuration:
5857
},
5958
config = function()
6059
require("http_client").setup({
61-
-- Optional: Configure default options here
60+
create_keybindings = false -- Disable default keybindings completely
6261
})
6362
end,
6463
}
6564
```
66-
67-
### Configuration
68-
6965
This plugin can be configured automatically using the `after/plugin` directory. The default configuration is set in `after/plugin/http_client.lua`. You can override these settings by creating your own `after/plugin/http_client.lua` file in your Neovim configuration directory.
7066

7167
Example configuration:
@@ -122,6 +118,15 @@ You can adjust these settings to your preferences.
122118
- `:HttpDryRun`: Perform a dry run of the request under the cursor.
123119
- `:HttpCopyCurl`: Copy the curl command for the HTTP request under the cursor.
124120

121+
### Response Window
122+
Responses are displayed in a dedicated split window with the following features:
123+
- Each response creates a new tab with timestamp
124+
- Latest 10 responses are preserved
125+
- Navigation:
126+
- `H` - Previous response
127+
- `L` - Next response
128+
- `q` or `<Esc>` - Close response window
129+
125130
### Keybindings
126131

127132
The plugin comes with the following default keybindings:

lua/http_client/ui/display.lua

Lines changed: 138 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,49 +1,153 @@
11
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
2125

3126
function M.display_in_buffer(content, title)
4127
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()
14131

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()
17134

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)
32138

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'))
36144

37145
-- Set buffer to readonly
38146
vim.api.nvim_buf_set_option(buf, 'modifiable', false)
39147
vim.api.nvim_buf_set_option(buf, 'readonly', true)
40148

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)
47151
end)
48152
end
49153

0 commit comments

Comments
 (0)