Skip to content

Commit

Permalink
feat(statusline): statusline api
Browse files Browse the repository at this point in the history
  • Loading branch information
folke committed May 30, 2024
1 parent 3b01742 commit c219a1a
Showing 1 changed file with 47 additions and 4 deletions.
51 changes: 47 additions & 4 deletions lua/trouble/api.lua
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
local Actions = require("trouble.config.actions")
local Config = require("trouble.config")
local Util = require("trouble.util")
local View = require("trouble.view")

---@alias trouble.ApiFn fun(opts?: trouble.Config|string): trouble.View
---@alias trouble.Open trouble.Config|{focus?:boolean, new?:boolean}
---@alias trouble.Open trouble.Mode|{focus?:boolean, new?:boolean}

---@class trouble.api: trouble.actions
local M = {}
Expand Down Expand Up @@ -111,13 +110,57 @@ function M.get_items(opts)
local view = M.find_last(opts)
local ret = {} ---@type trouble.Item[]
if view then
for _, items in pairs(view.items) do
vim.list_extend(ret, items)
for _, source in pairs(view.sections) do
vim.list_extend(ret, source.items or {})
end
end
return ret
end

---@param opts? trouble.Config|string
---@return {get: fun():string, cond: fun():boolean}
function M.statusline(opts)
local Spec = require("trouble.spec")
local Section = require("trouble.view.section")
local Render = require("trouble.view.render")
opts = Config.get(opts)
opts.results.indent_guides = false
opts.icons.indent.ws = ""
local renderer = Render.new(opts, {
multiline = false,
indent = false,
})
local status = nil ---@type string?
---@cast opts trouble.Mode

local s = Spec.section(opts)
local section = Section.new(s, opts)
section.on_update = function()
status = nil
if package.loaded["lualine"] then
require("lualine").refresh()
else
vim.cmd.redrawstatus()
end
end
section:listen()
section:refresh()
return {
cond = function()
return section.node and section.node:count() > 0
end,
function()
if status then
return status
end
renderer:clear()
renderer:sections({ section })
status = "%#StatusLine#" .. renderer:statusline()
return status
end,
}
end

return setmetatable(M, {
__index = function(_, k)
return M.action(k)
Expand Down

0 comments on commit c219a1a

Please sign in to comment.