From ee53d01f5bef20c467b9a087cb692bbff64c4de9 Mon Sep 17 00:00:00 2001 From: jmarkin Date: Tue, 27 Aug 2024 10:40:34 +0300 Subject: [PATCH] fix(statistics): allow all curl statistics, ordering, highlight --- lua/rest-nvim/config/check.lua | 5 ++++ lua/rest-nvim/config/default.lua | 4 +-- lua/rest-nvim/config/init.lua | 2 ++ lua/rest-nvim/ui/result.lua | 42 ++++++++++++++++++++++++++------ rest.nvim-scm-1.rockspec | 1 + syntax/http_stat.vim | 9 +++++++ 6 files changed, 53 insertions(+), 10 deletions(-) create mode 100644 syntax/http_stat.vim diff --git a/lua/rest-nvim/config/check.lua b/lua/rest-nvim/config/check.lua index 4554069e..81281e9b 100644 --- a/lua/rest-nvim/config/check.lua +++ b/lua/rest-nvim/config/check.lua @@ -86,6 +86,11 @@ function check.get_unrecognized_keys(tbl, default_tbl) end end end + for k, _ in pairs(ret) do + if k:find("statistics") then + ret[k] = nil + end + end return vim.tbl_keys(ret) end diff --git a/lua/rest-nvim/config/default.lua b/lua/rest-nvim/config/default.lua index 08600824..4f1060e2 100644 --- a/lua/rest-nvim/config/default.lua +++ b/lua/rest-nvim/config/default.lua @@ -38,8 +38,8 @@ local default_config = { ---See `man curl` for `--write-out` flag ---@type table statistics = { - time_total = { winbar = "take", title = "Time taken" }, - size_download = { winbar = "size", title = "Download size" }, + time_total = { winbar = "take", title = "Time taken", order = 1 }, + size_download = { winbar = "size", title = "Download size", order = 2 }, }, }, }, diff --git a/lua/rest-nvim/config/init.lua b/lua/rest-nvim/config/init.lua index afe6c4ce..46b9569e 100644 --- a/lua/rest-nvim/config/init.lua +++ b/lua/rest-nvim/config/init.lua @@ -71,6 +71,8 @@ local config --- Winbar title. Set to `false` or `nil` to not show for winbar, set to empty string --- to hide title If true, rest.nvim will use lowered `title` field ---@field winbar? string|boolean +--- Order position from low to high +---@field order? number ---@class rest.Opts.Cookies --- Enable the cookies support (Default: `true`) diff --git a/lua/rest-nvim/ui/result.lua b/lua/rest-nvim/ui/result.lua index c5c6ae5a..778200d7 100644 --- a/lua/rest-nvim/ui/result.lua +++ b/lua/rest-nvim/ui/result.lua @@ -2,7 +2,7 @@ --- ---@brief [[ --- ---- rest.nvim result UI implmentation +--- rest.nvim result UI implementation --- ---@brief ]] @@ -161,9 +161,23 @@ local panes = { set_lines(self.bufnr, { "No Statistics" }) return end - syntax_highlight(self.bufnr, "jproperties") + vim.bo[self.bufnr].syntax = "http_stat" + + local ordered = {} for key, value in pairs(data.response.statistics) do - table.insert(lines, ("%s: %s"):format(key, value)) + local style = config.clients.curl.statistics[key] or {} + local title = style["title"] or key + + table.insert(ordered, { + order = style.order or 0, + val = ("%s: %s"):format(title, value), + }) + end + table.sort(ordered, function(a, b) + return a.order < b.order + end) + for _, v in ipairs(ordered) do + table.insert(lines, v.val) end set_lines(self.bufnr, lines) end, @@ -179,21 +193,33 @@ winbar = winbar .. "%#RestText#Press %#Keyword#?%#RestText# for help%#Normal# " ---Winbar component showing response statistics ---@return string function ui.stat_winbar() - local content = "" if not data.response then return "Loading...%#Normal#" end - for stat_name, stat_value in pairs(data.response.statistics) do - local style = config.clients.curl.statistics[stat_name] or {} + local ordered = {} + for stat_name, style in pairs(config.clients.curl.statistics) do + local stat_value = data.response.statistics[stat_name] or "" if style.winbar then local title = type(style.winbar) == "string" and style.winbar or (style.title or stat_name):lower() if title ~= "" then title = title .. ": " end - local value, representation = vim.split(stat_value, " ")[1], vim.split(stat_value, " ")[2] - content = content .. " %#RestText#" .. title .. "%#Number#" .. value .. " %#Normal#" .. representation + table.insert(ordered, { + order = style.order or 0, + val = string.format("%%#RestText#%s%%#Normal#%s", title, stat_value), + }) end end + if #ordered == 0 then + return "" + end + table.sort(ordered, function(a, b) + return a.order < b.order + end) + local content = "" + for _, v in ipairs(ordered) do + content = content .. " " .. v.val + end return content end diff --git a/rest.nvim-scm-1.rockspec b/rest.nvim-scm-1.rockspec index 2670bdaa..908f5c13 100644 --- a/rest.nvim-scm-1.rockspec +++ b/rest.nvim-scm-1.rockspec @@ -46,5 +46,6 @@ build = { "ftdetect", "ftplugin", "queries", + "syntax", } } diff --git a/syntax/http_stat.vim b/syntax/http_stat.vim new file mode 100644 index 00000000..82d9dee8 --- /dev/null +++ b/syntax/http_stat.vim @@ -0,0 +1,9 @@ +if exists("b:current_syntax") | finish | endif + +syntax match httpStatField "^\s*\zs.\{-}\ze:" +syntax match httpStatValue ": \zs.*$" + +highlight link httpStatField Identifier +highlight link httpStatValue String + +let b:current_syntax = "http_stat"