Skip to content

Commit 01c661b

Browse files
feat: print request name, url, and version in UI
1 parent 94e3e29 commit 01c661b

File tree

4 files changed

+67
-40
lines changed

4 files changed

+67
-40
lines changed

lua/rest-nvim/commands.lua

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ local dotenv = require("rest-nvim.dotenv")
4040
local request = require("rest-nvim.request")
4141
local logger = require("rest-nvim.logger")
4242
local parser = require("rest-nvim.parser")
43+
local ui = require("rest-nvim.ui.result")
4344

4445

4546
---@type table<string, RestCmd>
@@ -53,7 +54,9 @@ local rest_command_tbl = {
5354
request.run_by_name(args[1])
5455
return
5556
end
57+
ui.clear()
5658
-- TODO: open result window here (use `:horizontal`)
59+
ui.open_ui()
5760
request.run()
5861
end,
5962
---@return string[]

lua/rest-nvim/request.lua

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -32,17 +32,11 @@ local function run_request(req)
3232
logger.debug("run_request")
3333
local client = require("rest-nvim.client.curl.cli")
3434
rest_nvim_last_request = req
35+
ui.update({request=req})
3536

3637
-- remove previous result
3738
response.current = nil
38-
-- clear the ui
39-
ui.update()
40-
41-
-- open result UI
42-
ui.open_ui()
43-
4439
-- TODO: set UI with request informations (e.g. method & get)
45-
4640
nio.run(function ()
4741
local ok, res = pcall(client.request(req).wait)
4842
if not ok then
@@ -59,8 +53,10 @@ local function run_request(req)
5953
logger.info("handler done")
6054

6155
-- update result UI
62-
-- NOTE: wrap with schedule to set vim variable outside of lua callback loop
63-
vim.schedule(ui.update)
56+
-- NOTE: wrap with schedule to access vim variables outside of lua callback loop
57+
vim.schedule(function ()
58+
ui.update({response = res})
59+
end)
6460
end)
6561
-- FIXME: return future to pass the command state
6662
end

lua/rest-nvim/ui/panes.lua

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
---@mod rest-nvim.ui.panes Small internal library to create pane style window
22

3+
local utils = require("rest-nvim.utils")
4+
35
---@class rest.ui.panes.Pane
46
---@field name string
57
---@field bufnr number
@@ -71,7 +73,6 @@ function M.create_pane_group(name, pane_opts, opts)
7173
-- small trick to ensure buffer is loaded before the `BufWinEnter` event
7274
-- unless lazy-setting winbar won't work
7375
vim.fn.bufload(self.bufnr)
74-
vim.bo[self.bufnr].modifiable = false
7576
vim.bo[self.bufnr].swapfile = false
7677
vim.bo[self.bufnr].buftype = "nofile"
7778
vim.b[self.bufnr].__pane_group = name
@@ -88,6 +89,8 @@ function M.create_pane_group(name, pane_opts, opts)
8889
local modifiable = pane_opt.render(self) or false
8990
if not modifiable then
9091
vim.bo[self.bufnr].undolevels = -1
92+
else
93+
vim.bo[self.bufnr].undolevels = vim.o.undolevels
9194
end
9295
vim.bo[self.bufnr].modifiable = modifiable
9396
end

lua/rest-nvim/ui/result.lua

Lines changed: 55 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
local M = {}
1+
local ui = {}
22

33
local config = require("rest-nvim.config")
44
local utils = require("rest-nvim.utils")
55
local paneui = require("rest-nvim.ui.panes")
6-
local response = require("rest-nvim.response")
6+
local res = require("rest-nvim.response")
77

88
local function set_lines(buffer, lines)
99
vim.api.nvim_buf_set_lines(buffer, 0, -1, false, lines)
@@ -19,45 +19,66 @@ local function syntax_highlight(buffer, filetype)
1919
end
2020
end
2121

22+
---@class rest.UIData
23+
local data = {
24+
---@type rest.Request?
25+
request = nil,
26+
---@type rest.Response?
27+
response = nil
28+
}
29+
30+
---@param req rest.Request
31+
---@return string[]
32+
local function render_request(req)
33+
local req_line = req.method .. " " .. req.url
34+
if req.http_version then
35+
req_line = req_line .. " " .. req.http_version
36+
end
37+
return {
38+
"### " .. req.name,
39+
req_line,
40+
""
41+
}
42+
end
43+
2244
---@type rest.ui.panes.PaneOpts[]
2345
local panes = {
2446
{
2547
name = "Response",
2648
render = function(self)
27-
local result = response.current
28-
if not result then
49+
if not data.request then
2950
vim.bo[self.bufnr].undolevels = -1
3051
set_lines(self.bufnr, { "Loading..." })
3152
return
3253
end
3354
syntax_highlight(self.bufnr, "http")
34-
vim.bo[self.bufnr].undolevels = 1000
35-
-- TODO: hmmmm how to bring request data here?
36-
local lines = {
37-
"GET" .. " " .. "TODO",
38-
"",
39-
}
40-
local body = response.try_format_body(result.headers["content-type"], result.body)
41-
vim.list_extend(lines, body)
55+
local lines = render_request(data.request)
56+
if data.response then
57+
local body = res.try_format_body(data.response.headers["content-type"], data.response.body)
58+
table.insert(lines, "#+RES")
59+
vim.list_extend(lines, body)
60+
table.insert(lines, "#+END")
61+
else
62+
vim.list_extend(lines, { "# Loading..." })
63+
end
4264
set_lines(self.bufnr, lines)
43-
return true
65+
return false
4466
end,
4567
},
4668
{
4769
name = "Statistics",
4870
render = function(self)
49-
local result = response.current
50-
if not result then
71+
if not data.response then
5172
set_lines(self.bufnr, { "Loading..." })
5273
return
5374
end
5475
local lines = {}
55-
if not result.statistics then
76+
if not data.response.statistics then
5677
set_lines(self.bufnr, { "No Statistics" })
5778
return
5879
end
5980
syntax_highlight(self.bufnr, "jproperties")
60-
for key, value in pairs(result.statistics) do
81+
for key, value in pairs(data.response.statistics) do
6182
local skip_cookie = config.result.window.cookies and key == "set-cookies"
6283
if not skip_cookie then
6384
table.insert(lines, ("%s: %s"):format(key, value))
@@ -71,14 +92,13 @@ if config.result.window.cookies then
7192
table.insert(panes, 2, {
7293
name = "Cookies",
7394
render = function(self)
74-
local result = response.current
75-
if not result then
95+
if not data.response then
7696
set_lines(self.bufnr, { "Loading..." })
7797
return
7898
end
7999
local lines = {}
80100
---@type string?
81-
local cookies_raw = vim.tbl_get(result, "headers", "set-cookies")
101+
local cookies_raw = vim.tbl_get(data.response, "headers", "set-cookies")
82102
if not cookies_raw then
83103
set_lines(self.bufnr, { "No Cookies" })
84104
return
@@ -93,14 +113,13 @@ if config.result.window.headers then
93113
table.insert(panes, 2, {
94114
name = "Headers",
95115
render = function(self)
96-
local result = response.current
97-
if not result then
116+
if not data.response then
98117
set_lines(self.bufnr, { "Loading..." })
99118
return
100119
end
101120
syntax_highlight(self.bufnr, "jproperties")
102121
local lines = {}
103-
local headers = vim.iter(result.headers):totable()
122+
local headers = vim.iter(data.response.headers):totable()
104123
table.sort(headers, function(b, a) return a[1] > b[1] end)
105124
for _, pair in ipairs(headers) do
106125
table.insert(lines, ("%s: %s"):format(pair[1], pair[2]))
@@ -115,13 +134,12 @@ winbar = winbar .. "%=%<"
115134
winbar = winbar .. "%{%v:lua.require('rest-nvim.ui.result').stat_winbar()%}"
116135
winbar = winbar .. " %#RestText#|%#Normal# "
117136
winbar = winbar .. "%#RestText#Press %#Keyword#?%#RestText# for help%#Normal# "
118-
function M.stat_winbar()
137+
function ui.stat_winbar()
119138
local content = ""
120-
local stats = vim.tbl_get(response, "current", "statistics")
121-
if not stats then
139+
if not data.response then
122140
return "Loading...%#Normal#"
123141
end
124-
for stat_name, stat_value in pairs(stats) do
142+
for stat_name, stat_value in pairs(data.response.statistics) do
125143
local style = config.result.behavior.statistics.stats[stat_name] or {}
126144
if style.winbar then
127145
local title = type(style.winbar) == "string" and style.winbar or (style.title or stat_name):lower()
@@ -168,7 +186,7 @@ vim.api.nvim_set_hl(0, "RestPaneTitle", {
168186
})
169187

170188
---@param horizontal? boolean
171-
function M.open_ui(horizontal)
189+
function ui.open_ui(horizontal)
172190
local winnr = vim.iter(vim.api.nvim_tabpage_list_wins(0)):find(function(id)
173191
local buf = vim.api.nvim_win_get_buf(id)
174192
return vim.b[buf].__pane_group == group.name
@@ -180,8 +198,15 @@ function M.open_ui(horizontal)
180198
end
181199
end
182200

183-
function M.update()
201+
function ui.clear()
202+
data = {}
203+
group:render()
204+
end
205+
206+
---@param new_data rest.UIData
207+
function ui.update(new_data)
208+
data = vim.tbl_deep_extend("force", data, new_data)
184209
group:render()
185210
end
186211

187-
return M
212+
return ui

0 commit comments

Comments
 (0)