1- local M = {}
1+ local ui = {}
22
33local config = require (" rest-nvim.config" )
44local utils = require (" rest-nvim.utils" )
55local paneui = require (" rest-nvim.ui.panes" )
6- local response = require (" rest-nvim.response" )
6+ local res = require (" rest-nvim.response" )
77
88local 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
2020end
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[]
2345local 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 .. "%=%<"
115134winbar = winbar .. " %{%v:lua.require('rest-nvim.ui.result').stat_winbar()%}"
116135winbar = winbar .. " %#RestText#|%#Normal# "
117136winbar = 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
181199end
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 ()
185210end
186211
187- return M
212+ return ui
0 commit comments