2021-02-15 18:09:12 +00:00
|
|
|
-- Copyright (c) 2020-2021 shadmansaleh
|
|
|
|
-- MIT license, see LICENSE for more details.
|
|
|
|
local M = {}
|
|
|
|
|
2021-04-13 12:36:51 +00:00
|
|
|
-- Note for now only works for termguicolors scope can be bg or fg or any other
|
|
|
|
-- attr parameter like bold/italic/reverse
|
2021-10-12 14:04:47 +00:00
|
|
|
---@param color_group string hl_group name
|
2022-09-10 11:42:14 +00:00
|
|
|
---@param scope string bg | fg | sp
|
2022-05-30 14:25:05 +00:00
|
|
|
---@return table|string returns #rrggbb formatted color when scope is specified
|
|
|
|
---- or complete color table when scope isn't specified
|
2021-02-17 18:29:50 +00:00
|
|
|
function M.extract_highlight_colors(color_group, scope)
|
2022-03-02 13:37:08 +00:00
|
|
|
local color = require('lualine.highlight').get_lualine_hl(color_group)
|
|
|
|
if not color then
|
|
|
|
if vim.fn.hlexists(color_group) == 0 then
|
|
|
|
return nil
|
|
|
|
end
|
|
|
|
color = vim.api.nvim_get_hl_by_name(color_group, true)
|
|
|
|
if color.background ~= nil then
|
|
|
|
color.bg = string.format('#%06x', color.background)
|
|
|
|
color.background = nil
|
|
|
|
end
|
|
|
|
if color.foreground ~= nil then
|
|
|
|
color.fg = string.format('#%06x', color.foreground)
|
|
|
|
color.foreground = nil
|
|
|
|
end
|
2022-09-10 11:42:14 +00:00
|
|
|
if color.special ~= nil then
|
|
|
|
color.sp = string.format('#%06x', color.special)
|
|
|
|
color.special = nil
|
|
|
|
end
|
2021-02-20 03:21:05 +00:00
|
|
|
end
|
2021-09-03 18:28:20 +00:00
|
|
|
if scope then
|
|
|
|
return color[scope]
|
|
|
|
end
|
2021-02-20 03:21:05 +00:00
|
|
|
return color
|
|
|
|
end
|
|
|
|
|
2022-05-30 14:25:05 +00:00
|
|
|
--- retrieves color value from highlight group name in syntax_list
|
2021-10-12 14:04:47 +00:00
|
|
|
--- first present highlight is returned
|
2022-09-11 10:43:31 +00:00
|
|
|
---@param scope string|table
|
2021-10-12 14:04:47 +00:00
|
|
|
---@param syntaxlist table
|
|
|
|
---@param default string
|
|
|
|
---@return string|nil
|
2021-09-17 09:16:38 +00:00
|
|
|
function M.extract_color_from_hllist(scope, syntaxlist, default)
|
2022-09-11 10:43:31 +00:00
|
|
|
scope = type(scope) == 'string' and { scope } or scope
|
2021-09-17 09:16:38 +00:00
|
|
|
for _, highlight_name in ipairs(syntaxlist) do
|
|
|
|
if vim.fn.hlexists(highlight_name) ~= 0 then
|
|
|
|
local color = M.extract_highlight_colors(highlight_name)
|
2022-09-11 10:43:31 +00:00
|
|
|
for _, sc in ipairs(scope) do
|
|
|
|
if color.reverse then
|
|
|
|
if sc == 'bg' then
|
|
|
|
sc = 'fg'
|
|
|
|
else
|
|
|
|
sc = 'bg'
|
|
|
|
end
|
|
|
|
end
|
|
|
|
if color[sc] then
|
|
|
|
return color[sc]
|
2021-09-17 09:16:38 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
return default
|
|
|
|
end
|
|
|
|
|
2021-10-12 14:04:47 +00:00
|
|
|
---remove empty strings from list
|
|
|
|
---@param list table
|
|
|
|
---@return table
|
2021-04-11 08:20:41 +00:00
|
|
|
function M.list_shrink(list)
|
|
|
|
local new_list = {}
|
|
|
|
for i = 1, #list do
|
2021-09-03 18:28:20 +00:00
|
|
|
if list[i] and #list[i] > 0 then
|
|
|
|
table.insert(new_list, list[i])
|
|
|
|
end
|
2021-04-11 08:20:41 +00:00
|
|
|
end
|
|
|
|
return new_list
|
|
|
|
end
|
|
|
|
|
2021-10-12 14:04:47 +00:00
|
|
|
--- Check if a auto command is already defined
|
|
|
|
---@param event string
|
2022-05-30 14:25:05 +00:00
|
|
|
---@param pattern string
|
2021-10-12 14:04:47 +00:00
|
|
|
---@param command_str string
|
|
|
|
---@return boolean whether autocmd is already defined
|
2022-05-30 14:25:05 +00:00
|
|
|
local function autocmd_is_defined(event, pattern, command_str)
|
|
|
|
return vim.api.nvim_exec(string.format('au lualine %s %s', event, pattern), true):find(command_str) ~= nil
|
2021-08-08 16:03:58 +00:00
|
|
|
end
|
|
|
|
|
2021-10-12 14:04:47 +00:00
|
|
|
--- Define a auto command if it's not already defined
|
|
|
|
---@param event string event name
|
2022-05-30 14:25:05 +00:00
|
|
|
---@param pattern string event pattern
|
2021-10-12 14:04:47 +00:00
|
|
|
---@param cmd string command to run on event
|
2022-07-22 13:29:55 +00:00
|
|
|
---@param group string group name defaults to lualine
|
|
|
|
function M.define_autocmd(event, pattern, cmd, group)
|
2021-09-03 18:28:20 +00:00
|
|
|
if not cmd then
|
2022-05-30 14:25:05 +00:00
|
|
|
cmd = pattern
|
|
|
|
pattern = '*'
|
2021-09-03 18:28:20 +00:00
|
|
|
end
|
2022-05-30 14:25:05 +00:00
|
|
|
if not autocmd_is_defined(event, pattern, cmd) then
|
2022-07-22 13:29:55 +00:00
|
|
|
vim.cmd(string.format('autocmd %s %s %s %s', group or 'lualine', event, pattern, cmd))
|
2021-05-04 04:56:58 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2021-08-08 16:03:58 +00:00
|
|
|
-- Check if statusline is on focused window or not
|
|
|
|
function M.is_focused()
|
2022-01-04 01:22:52 +00:00
|
|
|
return tonumber(vim.g.actual_curwin) == vim.api.nvim_get_current_win()
|
2021-08-08 16:03:58 +00:00
|
|
|
end
|
|
|
|
|
2022-05-30 14:25:05 +00:00
|
|
|
--- Check what's the character at pos
|
2021-10-12 14:04:47 +00:00
|
|
|
---@param str string
|
|
|
|
---@param pos number
|
2022-05-30 14:25:05 +00:00
|
|
|
---@return string character at position pos in string str
|
2021-08-25 11:43:12 +00:00
|
|
|
function M.charAt(str, pos)
|
|
|
|
return string.char(str:byte(pos))
|
|
|
|
end
|
|
|
|
|
2021-11-25 06:57:00 +00:00
|
|
|
-- deepcopy adapted from penlight
|
|
|
|
-- https://github.com/lunarmodules/Penlight/blob/0653cdb05591454a9804a7fee8c873b8f06b0b8f/lua/pl/tablex.lua#L98-L122
|
|
|
|
local function cycle_aware_copy(t, cache)
|
|
|
|
if type(t) ~= 'table' then
|
|
|
|
return t
|
|
|
|
end
|
|
|
|
if cache[t] then
|
|
|
|
return cache[t]
|
|
|
|
end
|
|
|
|
local res = {}
|
|
|
|
cache[t] = res
|
|
|
|
local mt = getmetatable(t)
|
|
|
|
for k, v in pairs(t) do
|
|
|
|
k = cycle_aware_copy(k, cache)
|
|
|
|
v = cycle_aware_copy(v, cache)
|
|
|
|
res[k] = v
|
|
|
|
end
|
|
|
|
setmetatable(res, mt)
|
|
|
|
return res
|
|
|
|
end
|
|
|
|
|
|
|
|
--- make a deep copy of a table, recursively copying all the keys and fields.
|
|
|
|
-- This supports cycles in tables; cycles will be reproduced in the copy.
|
|
|
|
-- This will also set the copied table's metatable to that of the original.
|
|
|
|
-- @within Copying
|
|
|
|
-- @tab t A table
|
|
|
|
-- @return new table
|
|
|
|
function M.deepcopy(t)
|
|
|
|
return cycle_aware_copy(t, {})
|
|
|
|
end
|
|
|
|
|
|
|
|
--- Check if comp is a lualine component
|
|
|
|
--- @param comp any
|
|
|
|
--- @return boolean
|
|
|
|
function M.is_component(comp)
|
|
|
|
if type(comp) ~= 'table' then
|
|
|
|
return false
|
|
|
|
end
|
|
|
|
local mt = getmetatable(comp)
|
|
|
|
return mt and mt.__is_lualine_component == true
|
|
|
|
end
|
|
|
|
|
2022-01-31 17:34:53 +00:00
|
|
|
--- Call function with args and return it's result.
|
|
|
|
--- If error occurs during fn retry times times.
|
|
|
|
---@param fn function Function to call.
|
|
|
|
---@param args table List of arguments used for calling function.
|
|
|
|
---@param times number Number of times to retry on error.
|
|
|
|
---@return any Result of fn.
|
|
|
|
function M.retry_call(fn, args, times)
|
|
|
|
times = times or 3
|
2022-01-31 17:36:06 +00:00
|
|
|
for _ = 0, times - 1 do
|
|
|
|
local result = { pcall(fn, unpack(args)) }
|
2022-01-31 17:34:53 +00:00
|
|
|
if result[1] == true then
|
|
|
|
return unpack(result, 2)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
return fn(unpack(args))
|
|
|
|
end
|
|
|
|
|
|
|
|
--- Wrap a function in retry_call
|
|
|
|
---@param fn function Function to call.
|
|
|
|
---@param times number Number of times to retry on error.
|
2022-05-30 14:25:05 +00:00
|
|
|
---@return function retry call wrapped function
|
2022-01-31 17:34:53 +00:00
|
|
|
function M.retry_call_wrap(fn, times)
|
|
|
|
return function(...)
|
2022-01-31 17:36:06 +00:00
|
|
|
return M.retry_call(fn, { ... }, times)
|
2022-01-31 17:34:53 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2022-02-15 16:54:29 +00:00
|
|
|
---Escape % in str so it doesn't get picked as stl item.
|
|
|
|
---@param str string
|
|
|
|
---@return string
|
|
|
|
function M.stl_escape(str)
|
2022-02-15 17:17:05 +00:00
|
|
|
if type(str) ~= 'string' then
|
|
|
|
return str
|
|
|
|
end
|
2022-02-15 16:54:29 +00:00
|
|
|
return str:gsub('%%', '%%%%')
|
|
|
|
end
|
|
|
|
|
2022-07-22 13:29:55 +00:00
|
|
|
---A safe call inside a timmer
|
|
|
|
---@param timer userdata
|
|
|
|
---@param augroup string|nil autocmd group to reset too on error.
|
|
|
|
---@param fn function
|
|
|
|
---@param max_err integer
|
|
|
|
---@param err_msg string
|
|
|
|
---@return function a wraped fn that can be called inside a timer and that
|
|
|
|
---stops the timer after max_err errors in calling fn
|
|
|
|
function M.timer_call(timer, augroup, fn, max_err, err_msg)
|
|
|
|
local err_cnt, ret = 0, nil
|
|
|
|
max_err = max_err or 3
|
|
|
|
return vim.schedule_wrap(function(...)
|
|
|
|
if err_cnt > max_err then
|
|
|
|
vim.loop.timer_stop(timer)
|
|
|
|
if augroup then
|
|
|
|
vim.cmd(string.format([[augroup %s | exe "autocmd!" | augroup END]], augroup))
|
|
|
|
end
|
2022-07-22 13:30:37 +00:00
|
|
|
error(err_msg .. ':\n' .. tostring(ret))
|
2022-07-22 13:29:55 +00:00
|
|
|
end
|
|
|
|
local ok
|
|
|
|
ok, ret = pcall(fn, ...)
|
|
|
|
if ok then
|
|
|
|
err_cnt = 0
|
|
|
|
else
|
|
|
|
err_cnt = err_cnt + 1
|
|
|
|
end
|
|
|
|
return ret
|
|
|
|
end)
|
|
|
|
end
|
|
|
|
|
2021-02-15 18:09:12 +00:00
|
|
|
return M
|