Skip to content

Commit

Permalink
fix(statistics): allow all curl statistics, ordering, highlight
Browse files Browse the repository at this point in the history
  • Loading branch information
JMarkin committed Aug 27, 2024
1 parent 0ca046d commit ee53d01
Show file tree
Hide file tree
Showing 6 changed files with 53 additions and 10 deletions.
5 changes: 5 additions & 0 deletions lua/rest-nvim/config/check.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions lua/rest-nvim/config/default.lua
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,8 @@ local default_config = {
---See `man curl` for `--write-out` flag
---@type table<string,RestStatisticsStyle>
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 },
},
},
},
Expand Down
2 changes: 2 additions & 0 deletions lua/rest-nvim/config/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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`)
Expand Down
42 changes: 34 additions & 8 deletions lua/rest-nvim/ui/result.lua
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
---
---@brief [[
---
--- rest.nvim result UI implmentation
--- rest.nvim result UI implementation
---
---@brief ]]

Expand Down Expand Up @@ -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,
Expand All @@ -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

Expand Down
1 change: 1 addition & 0 deletions rest.nvim-scm-1.rockspec
Original file line number Diff line number Diff line change
Expand Up @@ -46,5 +46,6 @@ build = {
"ftdetect",
"ftplugin",
"queries",
"syntax",
}
}
9 changes: 9 additions & 0 deletions syntax/http_stat.vim
Original file line number Diff line number Diff line change
@@ -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"

0 comments on commit ee53d01

Please sign in to comment.