Skip to content

Commit 58a62d9

Browse files
authored
refactor: use lua table for response body
2 parents 59d5331 + e0a35ea commit 58a62d9

File tree

2 files changed

+23
-34
lines changed

2 files changed

+23
-34
lines changed

lua/rest-nvim/init.lua

Lines changed: 22 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -231,17 +231,16 @@ local function curl_cmd(opts)
231231
end
232232

233233
--- Add metadata into the created buffer (status code, date, etc)
234-
local line_count = api.nvim_buf_line_count(res_bufnr) - 1
235234
-- Request statement (METHOD URL)
236235
api.nvim_buf_set_lines(
237236
res_bufnr,
238-
line_count,
239-
line_count,
237+
0,
238+
0,
240239
false,
241240
{ parsed_url.method .. ' ' .. parsed_url.url }
242241
)
243242
-- HTTP version, status code and its meaning, e.g. HTTP/1.1 200 OK
244-
line_count = api.nvim_buf_line_count(res_bufnr)
243+
local line_count = api.nvim_buf_line_count(res_bufnr)
245244
api.nvim_buf_set_lines(
246245
res_bufnr,
247246
line_count,
@@ -250,38 +249,28 @@ local function curl_cmd(opts)
250249
{ 'HTTP/1.1 ' .. utils.http_status(res.status) }
251250
)
252251
-- Headers, e.g. Content-Type: application/json
253-
for _, header in ipairs(res.headers) do
254-
line_count = api.nvim_buf_line_count(res_bufnr)
255-
api.nvim_buf_set_lines(res_bufnr, line_count, line_count, false, { header })
256-
end
252+
api.nvim_buf_set_lines(
253+
res_bufnr,
254+
line_count + 1,
255+
line_count + #res.headers,
256+
false,
257+
res.headers
258+
)
257259

258260
--- Add the curl command results into the created buffer
259-
for line in utils.iter_lines(res.body) do
260-
if json_body then
261-
-- Format JSON output and then add it into the buffer
262-
-- line by line because Vim doesn't allow strings with newlines
263-
local out = fn.system("jq", line)
264-
for _, _line in ipairs(utils.split(out, '\n')) do
265-
line_count = api.nvim_buf_line_count(res_bufnr) - 1
266-
api.nvim_buf_set_lines(
267-
res_bufnr,
268-
line_count,
269-
line_count,
270-
false,
271-
{ _line }
272-
)
273-
end
274-
else
275-
line_count = api.nvim_buf_line_count(res_bufnr) - 1
276-
api.nvim_buf_set_lines(
277-
res_bufnr,
278-
line_count,
279-
line_count,
280-
false,
281-
{ line }
282-
)
283-
end
261+
if json_body then
262+
-- format JSON body
263+
res.body = fn.system("jq", res.body)
284264
end
265+
local lines = utils.split(res.body, '\n')
266+
line_count = api.nvim_buf_line_count(res_bufnr) - 1
267+
api.nvim_buf_set_lines(
268+
res_bufnr,
269+
line_count,
270+
line_count + #lines,
271+
false,
272+
lines
273+
)
285274

286275
-- Only open a new split if the buffer is not loaded into the current window
287276
if fn.bufwinnr(res_bufnr) == -1 then

tests/using_env_vars/post_create_user.http

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# Environment variables are readed from an `.env` file in the current working
1+
# Environment variables are read from an `.env` file in the current working
22
# directory and if they are not found, they will fallback to be searched in
33
# the system environment variables.
44
#

0 commit comments

Comments
 (0)