@@ -76,20 +76,14 @@ M.get_or_create_buf = function()
7676 local existing_bufnr = vim .fn .bufnr (tmp_name )
7777 if existing_bufnr ~= - 1 then
7878 -- Set modifiable
79- vim .api .nvim_set_option_value (" modifiable" , true , { buf = existing_bufnr })
79+ vim .api .nvim_set_option_value (" modifiable" , true , { buf = existing_bufnr })
8080 -- Prevent modified flag
81- vim .api .nvim_set_option_value (" buftype" , " nofile" , { buf = existing_bufnr })
81+ vim .api .nvim_set_option_value (" buftype" , " nofile" , { buf = existing_bufnr })
8282 -- Delete buffer content
83- vim .api .nvim_buf_set_lines (
84- existing_bufnr ,
85- 0 ,
86- vim .api .nvim_buf_line_count (existing_bufnr ) - 1 ,
87- false ,
88- {}
89- )
83+ vim .api .nvim_buf_set_lines (existing_bufnr , 0 , - 1 , false , {})
9084
9185 -- Make sure the filetype of the buffer is httpResult so it will be highlighted
92- vim .api .nvim_set_option_value (" ft" , " httpResult" , { buf = existing_bufnr } )
86+ vim .api .nvim_set_option_value (" ft" , " httpResult" , { buf = existing_bufnr })
9387
9488 return existing_bufnr
9589 end
@@ -116,9 +110,28 @@ local function create_callback(curl_cmd, opts)
116110 return
117111 end
118112 local res_bufnr = M .get_or_create_buf ()
119- local header_lines = res .headers
113+
114+ local headers = utils .filter (res .headers , function (value )
115+ return value ~= " "
116+ end , false )
117+
118+ headers = utils .map (headers , function (value )
119+ local _ , _ , http , status = string.find (value , " ^(HTTP.*)%s+(%d+)%s*$" )
120+
121+ if http and status then
122+ return http .. " " .. utils .http_status (tonumber (status ))
123+ end
124+
125+ return value
126+ end )
127+
128+ headers = utils .split_list (headers , function (value )
129+ return string.find (value , " ^HTTP.*$" )
130+ end )
131+
120132 res .headers = parse_headers (res .headers )
121- local content_type = res .headers [utils .key (res .headers ,' content-type' )]
133+
134+ local content_type = res .headers [utils .key (res .headers , " content-type" )]
122135 if content_type then
123136 content_type = content_type :match (" application/([-a-z]+)" ) or content_type :match (" text/(%l+)" )
124137 end
@@ -139,45 +152,32 @@ local function create_callback(curl_cmd, opts)
139152 end
140153 end
141154
142- -- This can be quite verbose so let user control it
143- if config .get (" result" ).show_curl_command then
144- vim .api .nvim_buf_set_lines (res_bufnr , 0 , 0 , false , { " Command: " .. curl_cmd })
145- end
146-
147155 if config .get (" result" ).show_url then
148156 --- Add metadata into the created buffer (status code, date, etc)
149157 -- Request statement (METHOD URL)
150- vim .api .nvim_buf_set_lines (res_bufnr , 0 , 0 , false , { method :upper () .. " " .. url })
158+ utils .write_block (res_bufnr , { method :upper () .. " " .. url }, false )
159+ end
160+
161+ -- This can be quite verbose so let user control it
162+ if config .get (" result" ).show_curl_command then
163+ utils .write_block (res_bufnr , { " Command: " .. curl_cmd }, true )
151164 end
152165
153166 if config .get (" result" ).show_http_info then
154- local line_count = vim .api .nvim_buf_line_count (res_bufnr )
155- local separator = config .get (" result" ).show_url and 0 or 1
156167 -- HTTP version, status code and its meaning, e.g. HTTP/1.1 200 OK
157- vim .api .nvim_buf_set_lines (
158- res_bufnr ,
159- line_count - separator ,
160- line_count - separator ,
161- false ,
162- { " HTTP/1.1 " .. utils .http_status (res .status ) }
163- )
168+ utils .write_block (res_bufnr , { " HTTP/1.1 " .. utils .http_status (res .status ) }, false )
164169 end
165170
166171 if config .get (" result" ).show_headers then
167- local line_count = vim .api .nvim_buf_line_count (res_bufnr )
168172 -- Headers, e.g. Content-Type: application/json
169- vim .api .nvim_buf_set_lines (
170- res_bufnr ,
171- line_count + 1 ,
172- line_count + 1 + # header_lines ,
173- false ,
174- header_lines
175- )
173+ for _ , header_block in ipairs (headers ) do
174+ utils .write_block (res_bufnr , header_block , true )
175+ end
176176 end
177177
178178 --- Add the curl command results into the created buffer
179179 local formatter = config .get (" result" ).formatters [content_type ]
180- -- formate response body
180+ -- format response body
181181 if type (formatter ) == " function" then
182182 local ok , out = pcall (formatter , res .body )
183183 -- check if formatter ran successfully
@@ -220,8 +220,8 @@ local function create_callback(curl_cmd, opts)
220220 buf_content = buf_content .. " \n #+END"
221221
222222 local lines = utils .split (buf_content , " \n " )
223- local line_count = vim . api . nvim_buf_line_count ( res_bufnr ) - 1
224- vim . api . nvim_buf_set_lines (res_bufnr , line_count , line_count + # lines , false , lines )
223+
224+ utils . write_block (res_bufnr , lines )
225225
226226 -- Only open a new split if the buffer is not loaded into the current window
227227 if vim .fn .bufwinnr (res_bufnr ) == - 1 then
0 commit comments