From bf2f4a78e23febad48673fb4767ef83d9263534e Mon Sep 17 00:00:00 2001 From: Barrett Ruth <62671086+barrettruth@users.noreply.github.com> Date: Tue, 3 Mar 2026 13:37:36 -0500 Subject: [PATCH] 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. --- lua/preview/init.lua | 14 ++++++++++++++ spec/init_spec.lua | 8 ++++++++ 2 files changed, 22 insertions(+) diff --git a/lua/preview/init.lua b/lua/preview/init.lua index 641da4a..f6d0006 100644 --- a/lua/preview/init.lua +++ b/lua/preview/init.lua @@ -41,6 +41,7 @@ ---@field toggle fun(bufnr?: integer) ---@field open fun(bufnr?: integer) ---@field status fun(bufnr?: integer): preview.Status +---@field statusline fun(bufnr?: integer): string ---@field get_config fun(): preview.Config local M = {} @@ -188,6 +189,19 @@ function M.status(bufnr) return compiler.status(bufnr) end +---@param bufnr? integer +---@return string +function M.statusline(bufnr) + bufnr = bufnr or vim.api.nvim_get_current_buf() + local s = compiler.status(bufnr) + if s.compiling then + return 'compiling' + elseif s.watching then + return 'watching' + end + return '' +end + M._test = { ---@diagnostic disable-next-line: assign-type-mismatch reset = function() diff --git a/spec/init_spec.lua b/spec/init_spec.lua index 5b97c8a..5c49276 100644 --- a/spec/init_spec.lua +++ b/spec/init_spec.lua @@ -100,4 +100,12 @@ describe('preview', function() helpers.delete_buffer(bufnr) end) end) + + describe('statusline', function() + it('returns empty string when idle', function() + local bufnr = helpers.create_buffer({}) + assert.are.equal('', preview.statusline(bufnr)) + helpers.delete_buffer(bufnr) + end) + end) end)