|
9 | 9 | ---@class astroui.status.condition |
10 | 10 | local M = {} |
11 | 11 |
|
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 |
13 | 27 |
|
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 | +} |
18 | 38 |
|
19 | 39 | --- 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") |
22 | 43 | ---@return boolean # whether or not LSP is attached |
23 | 44 | -- @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 |
27 | 56 | end |
28 | | - return false |
| 57 | + return op == "and" |
29 | 58 | end |
30 | 59 |
|
| 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 | + |
31 | 65 | --- A condition function if a macro is being recorded |
32 | 66 | ---@return boolean # whether or not a macro is currently being recorded |
33 | 67 | -- @usage local heirline_component = { provider = "Example Provider", condition = require("astroui.status").condition.is_macro_recording } |
|
0 commit comments