Skip to content

Commit bf2f4a7

Browse files
authored
feat: add statusline function (#10)
Problem: no way to expose compiling/watching state to statusline plugins like lualine or heirline without polling status() and formatting it manually. Solution: add `require('preview').statusline()` that returns 'compiling', 'watching', or '' for direct use in statusline components.
1 parent 187474b commit bf2f4a7

2 files changed

Lines changed: 22 additions & 0 deletions

File tree

lua/preview/init.lua

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@
4141
---@field toggle fun(bufnr?: integer)
4242
---@field open fun(bufnr?: integer)
4343
---@field status fun(bufnr?: integer): preview.Status
44+
---@field statusline fun(bufnr?: integer): string
4445
---@field get_config fun(): preview.Config
4546
local M = {}
4647

@@ -188,6 +189,19 @@ function M.status(bufnr)
188189
return compiler.status(bufnr)
189190
end
190191

192+
---@param bufnr? integer
193+
---@return string
194+
function M.statusline(bufnr)
195+
bufnr = bufnr or vim.api.nvim_get_current_buf()
196+
local s = compiler.status(bufnr)
197+
if s.compiling then
198+
return 'compiling'
199+
elseif s.watching then
200+
return 'watching'
201+
end
202+
return ''
203+
end
204+
191205
M._test = {
192206
---@diagnostic disable-next-line: assign-type-mismatch
193207
reset = function()

spec/init_spec.lua

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,4 +100,12 @@ describe('preview', function()
100100
helpers.delete_buffer(bufnr)
101101
end)
102102
end)
103+
104+
describe('statusline', function()
105+
it('returns empty string when idle', function()
106+
local bufnr = helpers.create_buffer({})
107+
assert.are.equal('', preview.statusline(bufnr))
108+
helpers.delete_buffer(bufnr)
109+
end)
110+
end)
103111
end)

0 commit comments

Comments
 (0)