Skip to content

Commit f333d52

Browse files
committed
feat(condition)!: hard code and strongly type buffer matchers
1 parent 3bdab23 commit f333d52

File tree

2 files changed

+45
-15
lines changed

2 files changed

+45
-15
lines changed

lua/astroui/config.lua

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -46,9 +46,6 @@
4646
---}
4747
---```
4848
---@field attributes table<string,table>?
49-
---**MEANT FOR INTERNAL USE ONLY**
50-
---A table of functions used for matching buffers to various attributes
51-
---@field buf_matchers table<string,fun(pattern_list:string[],bufnr:integer):boolean>?
5249
---Configure colors of components defined in the `status` API. Check the AstroNvim documentation for a complete list of color names.
5350
---Example:
5451
---
@@ -191,7 +188,6 @@ local M = {
191188
text_icons = {},
192189
status = {
193190
attributes = {},
194-
buf_matchers = {},
195191
colors = {},
196192
fallback_colors = {},
197193
icon_highlights = {},

lua/astroui/status/condition.lua

Lines changed: 45 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -9,25 +9,59 @@
99
---@class astroui.status.condition
1010
local M = {}
1111

12-
local config = require("astroui").config.status
12+
---@alias BufMatcherPattern string
13+
---@alias BufMatcherPatterns BufMatcherPattern|BufMatcherPattern[]
14+
---@alias BufMatcherKinds "filetype"|"buftype"|"bufname"
15+
---@alias BufMatcher fun(patterns: BufMatcherPatterns, bufnr: integer): boolean
16+
17+
---@param str BufMatcherPattern
18+
---@param patterns BufMatcherPatterns
19+
---@return boolean
20+
local function pattern_match(str, patterns)
21+
if type(patterns) == "string" then patterns = { patterns } end
22+
for _, pattern in ipairs(patterns) do
23+
if str:find(pattern) then return true end
24+
end
25+
return false
26+
end
1327

14-
--- A condition function if the window is currently active
15-
---@return boolean # whether or not the window is currently actie
16-
-- @usage local heirline_component = { provider = "Example Provider", condition = require("astroui.status").condition.is_active }
17-
function M.is_active() return vim.api.nvim_get_current_win() == tonumber(vim.g.actual_curwin) end
28+
---@type table<BufMatcherKinds, BufMatcher>
29+
local buf_matchers = {
30+
filetype = function(pattern_list, bufnr) return pattern_match(vim.bo[bufnr].filetype, pattern_list) end,
31+
buftype = function(pattern_list, bufnr) return pattern_match(vim.bo[bufnr].buftype, pattern_list) end,
32+
bufname = function(pattern_list, bufnr)
33+
local bufname = vim.fn.fnamemodify(vim.api.nvim_buf_get_name(bufnr), ":t")
34+
---@cast bufname -nil
35+
return pattern_match(bufname, pattern_list)
36+
end,
37+
}
1838

1939
--- A condition function if the buffer filetype,buftype,bufname match a pattern
20-
---@param patterns table the table of patterns to match
21-
---@param bufnr number of the buffer to match (Default: 0 [current])
40+
---@param patterns table<BufMatcherKinds, BufMatcherPatterns> the table of patterns to match
41+
---@param bufnr integer? of the buffer to match (Default: 0 [current])
42+
---@param op "and"|"or"? whether or not to require all pattern types to match or any (Default: "or")
2243
---@return boolean # whether or not LSP is attached
2344
-- @usage local heirline_component = { provider = "Example Provider", condition = function() return require("astroui.status").condition.buffer_matches { buftype = { "terminal" } } end }
24-
function M.buffer_matches(patterns, bufnr)
25-
for kind, pattern_list in pairs(patterns) do
26-
if config.buf_matchers[kind](pattern_list, bufnr) then return true end
45+
function M.buffer_matches(patterns, bufnr, op)
46+
if not op then op = "or" end
47+
if not bufnr then bufnr = 0 end
48+
if require("astrocore.buffer").is_valid(bufnr) then
49+
for kind, pattern_list in pairs(patterns) do
50+
if buf_matchers[kind](pattern_list, bufnr) then
51+
if op == "or" then return true end
52+
else
53+
if op == "and" then return false end
54+
end
55+
end
2756
end
28-
return false
57+
return op == "and"
2958
end
3059

60+
--- A condition function if the window is currently active
61+
---@return boolean # whether or not the window is currently actie
62+
-- @usage local heirline_component = { provider = "Example Provider", condition = require("astroui.status").condition.is_active }
63+
function M.is_active() return vim.api.nvim_get_current_win() == tonumber(vim.g.actual_curwin) end
64+
3165
--- A condition function if a macro is being recorded
3266
---@return boolean # whether or not a macro is currently being recorded
3367
-- @usage local heirline_component = { provider = "Example Provider", condition = require("astroui.status").condition.is_macro_recording }

0 commit comments

Comments
 (0)