@@ -76,20 +76,14 @@ M.get_or_create_buf = function()
76
76
local existing_bufnr = vim .fn .bufnr (tmp_name )
77
77
if existing_bufnr ~= - 1 then
78
78
-- 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 })
80
80
-- 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 })
82
82
-- 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 , {})
90
84
91
85
-- 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 })
93
87
94
88
return existing_bufnr
95
89
end
@@ -116,9 +110,29 @@ local function create_callback(curl_cmd, opts)
116
110
return
117
111
end
118
112
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
+ vim .print (value , http , status )
121
+
122
+ if http and status then
123
+ return http .. " " .. utils .http_status (tonumber (status ))
124
+ end
125
+
126
+ return value
127
+ end )
128
+
129
+ headers = utils .split_list (headers , function (value )
130
+ return string.find (value , " ^HTTP.*$" )
131
+ end )
132
+
120
133
res .headers = parse_headers (res .headers )
121
- local content_type = res .headers [utils .key (res .headers ,' content-type' )]
134
+
135
+ local content_type = utils .get_value (res .headers , " content-type" )
122
136
if content_type then
123
137
content_type = content_type :match (" application/([-a-z]+)" ) or content_type :match (" text/(%l+)" )
124
138
end
@@ -139,45 +153,32 @@ local function create_callback(curl_cmd, opts)
139
153
end
140
154
end
141
155
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
-
147
156
if config .get (" result" ).show_url then
148
157
--- Add metadata into the created buffer (status code, date, etc)
149
158
-- Request statement (METHOD URL)
150
- vim .api .nvim_buf_set_lines (res_bufnr , 0 , 0 , false , { method :upper () .. " " .. url })
159
+ utils .write_block (res_bufnr , { method :upper () .. " " .. url }, false )
160
+ end
161
+
162
+ -- This can be quite verbose so let user control it
163
+ if config .get (" result" ).show_curl_command then
164
+ utils .write_block (res_bufnr , { " Command: " .. curl_cmd }, true )
151
165
end
152
166
153
167
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
156
168
-- 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
- )
169
+ utils .write_block (res_bufnr , { " HTTP/1.1 " .. utils .http_status (res .status ) }, false )
164
170
end
165
171
166
172
if config .get (" result" ).show_headers then
167
- local line_count = vim .api .nvim_buf_line_count (res_bufnr )
168
173
-- 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
- )
174
+ for _ , header_block in ipairs (headers ) do
175
+ utils .write_block (res_bufnr , header_block , true )
176
+ end
176
177
end
177
178
178
179
--- Add the curl command results into the created buffer
179
180
local formatter = config .get (" result" ).formatters [content_type ]
180
- -- formate response body
181
+ -- format response body
181
182
if type (formatter ) == " function" then
182
183
local ok , out = pcall (formatter , res .body )
183
184
-- check if formatter ran successfully
@@ -220,8 +221,8 @@ local function create_callback(curl_cmd, opts)
220
221
buf_content = buf_content .. " \n #+END"
221
222
222
223
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 )
224
+
225
+ utils . write_block (res_bufnr , lines )
225
226
226
227
-- Only open a new split if the buffer is not loaded into the current window
227
228
if vim .fn .bufwinnr (res_bufnr ) == - 1 then
0 commit comments