| 1 |
-- Copyright (c) 2020-2021 shadmansaleh |
| 2 |
-- MIT license, see LICENSE for more details. |
| 3 |
local M = {} |
| 4 |
|
| 5 |
-- Note for now only works for termguicolors scope can be bg or fg or any other |
| 6 |
-- attr parameter like bold/italic/reverse |
| 7 |
---@param color_group string hl_group name |
| 8 |
---@param scope string bg | fg | sp |
| 9 |
---@return table|string returns #rrggbb formatted color when scope is specified |
| 10 |
---- or complete color table when scope isn't specified |
| 11 |
function M.extract_highlight_colors(color_group, scope) |
| 12 |
local color = require('lualine.highlight').get_lualine_hl(color_group) |
| 13 |
if not color then |
| 14 |
if vim.fn.hlexists(color_group) == 0 then |
| 15 |
return nil |
| 16 |
end |
| 17 |
color = vim.api.nvim_get_hl_by_name(color_group, true) |
| 18 |
if color.background ~= nil then |
| 19 |
color.bg = string.format('#%06x', color.background) |
| 20 |
color.background = nil |
| 21 |
end |
| 22 |
if color.foreground ~= nil then |
| 23 |
color.fg = string.format('#%06x', color.foreground) |
| 24 |
color.foreground = nil |
| 25 |
end |
| 26 |
if color.special ~= nil then |
| 27 |
color.sp = string.format('#%06x', color.special) |
| 28 |
color.special = nil |
| 29 |
end |
| 30 |
end |
| 31 |
if scope then |
| 32 |
return color[scope] |
| 33 |
end |
| 34 |
return color |
| 35 |
end |
| 36 |
|
| 37 |
--- retrieves color value from highlight group name in syntax_list |
| 38 |
--- first present highlight is returned |
| 39 |
---@param scope string|table |
| 40 |
---@param syntaxlist table |
| 41 |
---@param default string |
| 42 |
---@return string|nil |
| 43 |
function M.extract_color_from_hllist(scope, syntaxlist, default) |
| 44 |
scope = type(scope) == 'string' and { scope } or scope |
| 45 |
for _, highlight_name in ipairs(syntaxlist) do |
| 46 |
if vim.fn.hlexists(highlight_name) ~= 0 then |
| 47 |
local color = M.extract_highlight_colors(highlight_name) |
| 48 |
for _, sc in ipairs(scope) do |
| 49 |
if color.reverse then |
| 50 |
if sc == 'bg' then |
| 51 |
sc = 'fg' |
| 52 |
else |
| 53 |
sc = 'bg' |
| 54 |
end |
| 55 |
end |
| 56 |
if color[sc] then |
| 57 |
return color[sc] |
| 58 |
end |
| 59 |
end |
| 60 |
end |
| 61 |
end |
| 62 |
return default |
| 63 |
end |
| 64 |
|
| 65 |
---remove empty strings from list |
| 66 |
---@param list table |
| 67 |
---@return table |
| 68 |
function M.list_shrink(list) |
| 69 |
local new_list = {} |
| 70 |
for i = 1, #list do |
| 71 |
if list[i] and #list[i] > 0 then |
| 72 |
table.insert(new_list, list[i]) |
| 73 |
end |
| 74 |
end |
| 75 |
return new_list |
| 76 |
end |
| 77 |
|
| 78 |
--- Check if a auto command is already defined |
| 79 |
---@param event string |
| 80 |
---@param pattern string |
| 81 |
---@param command_str string |
| 82 |
---@return boolean whether autocmd is already defined |
| 83 |
local function autocmd_is_defined(event, pattern, command_str) |
| 84 |
return vim.api.nvim_exec(string.format('au lualine %s %s', event, pattern), true):find(command_str) ~= nil |
| 85 |
end |
| 86 |
|
| 87 |
--- Define a auto command if it's not already defined |
| 88 |
---@param event string event name |
| 89 |
---@param pattern string event pattern |
| 90 |
---@param cmd string command to run on event |
| 91 |
---@param group string group name defaults to lualine |
| 92 |
function M.define_autocmd(event, pattern, cmd, group) |
| 93 |
if not cmd then |
| 94 |
cmd = pattern |
| 95 |
pattern = '*' |
| 96 |
end |
| 97 |
if not autocmd_is_defined(event, pattern, cmd) then |
| 98 |
vim.cmd(string.format('autocmd %s %s %s %s', group or 'lualine', event, pattern, cmd)) |
| 99 |
end |
| 100 |
end |
| 101 |
|
| 102 |
-- Check if statusline is on focused window or not |
| 103 |
function M.is_focused() |
| 104 |
return tonumber(vim.g.actual_curwin) == vim.api.nvim_get_current_win() |
| 105 |
end |
| 106 |
|
| 107 |
--- Check what's the character at pos |
| 108 |
---@param str string |
| 109 |
---@param pos number |
| 110 |
---@return string character at position pos in string str |
| 111 |
function M.charAt(str, pos) |
| 112 |
return string.char(str:byte(pos)) |
| 113 |
end |
| 114 |
|
| 115 |
-- deepcopy adapted from penlight |
| 116 |
-- https://github.com/lunarmodules/Penlight/blob/0653cdb05591454a9804a7fee8c873b8f06b0b8f/lua/pl/tablex.lua#L98-L122 |
| 117 |
local function cycle_aware_copy(t, cache) |
| 118 |
if type(t) ~= 'table' then |
| 119 |
return t |
| 120 |
end |
| 121 |
if cache[t] then |
| 122 |
return cache[t] |
| 123 |
end |
| 124 |
local res = {} |
| 125 |
cache[t] = res |
| 126 |
local mt = getmetatable(t) |
| 127 |
for k, v in pairs(t) do |
| 128 |
k = cycle_aware_copy(k, cache) |
| 129 |
v = cycle_aware_copy(v, cache) |
| 130 |
res[k] = v |
| 131 |
end |
| 132 |
setmetatable(res, mt) |
| 133 |
return res |
| 134 |
end |
| 135 |
|
| 136 |
--- make a deep copy of a table, recursively copying all the keys and fields. |
| 137 |
-- This supports cycles in tables; cycles will be reproduced in the copy. |
| 138 |
-- This will also set the copied table's metatable to that of the original. |
| 139 |
-- @within Copying |
| 140 |
-- @tab t A table |
| 141 |
-- @return new table |
| 142 |
function M.deepcopy(t) |
| 143 |
return cycle_aware_copy(t, {}) |
| 144 |
end |
| 145 |
|
| 146 |
--- Check if comp is a lualine component |
| 147 |
--- @param comp any |
| 148 |
--- @return boolean |
| 149 |
function M.is_component(comp) |
| 150 |
if type(comp) ~= 'table' then |
| 151 |
return false |
| 152 |
end |
| 153 |
local mt = getmetatable(comp) |
| 154 |
return mt and mt.__is_lualine_component == true |
| 155 |
end |
| 156 |
|
| 157 |
--- Call function with args and return it's result. |
| 158 |
--- If error occurs during fn retry times times. |
| 159 |
---@param fn function Function to call. |
| 160 |
---@param args table List of arguments used for calling function. |
| 161 |
---@param times number Number of times to retry on error. |
| 162 |
---@return any Result of fn. |
| 163 |
function M.retry_call(fn, args, times) |
| 164 |
times = times or 3 |
| 165 |
for _ = 0, times - 1 do |
| 166 |
local result = { pcall(fn, unpack(args)) } |
| 167 |
if result[1] == true then |
| 168 |
return unpack(result, 2) |
| 169 |
end |
| 170 |
end |
| 171 |
return fn(unpack(args)) |
| 172 |
end |
| 173 |
|
| 174 |
--- Wrap a function in retry_call |
| 175 |
---@param fn function Function to call. |
| 176 |
---@param times number Number of times to retry on error. |
| 177 |
---@return function retry call wrapped function |
| 178 |
function M.retry_call_wrap(fn, times) |
| 179 |
return function(...) |
| 180 |
return M.retry_call(fn, { ... }, times) |
| 181 |
end |
| 182 |
end |
| 183 |
|
| 184 |
---Escape % in str so it doesn't get picked as stl item. |
| 185 |
---@param str string |
| 186 |
---@return string |
| 187 |
function M.stl_escape(str) |
| 188 |
if type(str) ~= 'string' then |
| 189 |
return str |
| 190 |
end |
| 191 |
return str:gsub('%%', '%%%%') |
| 192 |
end |
| 193 |
|
| 194 |
---A safe call inside a timer |
| 195 |
---@param timer userdata |
| 196 |
---@param augroup string|nil autocmd group to reset too on error. |
| 197 |
---@param fn function |
| 198 |
---@param max_err integer |
| 199 |
---@param err_msg string |
| 200 |
---@return function a wrapped fn that can be called inside a timer and that |
| 201 |
---stops the timer after max_err errors in calling fn |
| 202 |
function M.timer_call(timer, augroup, fn, max_err, err_msg) |
| 203 |
local err_cnt, ret = 0, nil |
| 204 |
max_err = max_err or 3 |
| 205 |
return vim.schedule_wrap(function(...) |
| 206 |
if err_cnt > max_err then |
| 207 |
vim.loop.timer_stop(timer) |
| 208 |
if augroup then |
| 209 |
vim.cmd(string.format([[augroup %s | exe "autocmd!" | augroup END]], augroup)) |
| 210 |
end |
| 211 |
error(err_msg .. ':\n' .. tostring(ret)) |
| 212 |
end |
| 213 |
local ok |
| 214 |
ok, ret = pcall(fn, ...) |
| 215 |
if ok then |
| 216 |
err_cnt = 0 |
| 217 |
else |
| 218 |
err_cnt = err_cnt + 1 |
| 219 |
end |
| 220 |
return ret |
| 221 |
end) |
| 222 |
end |
| 223 |
|
| 224 |
return M |