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-02-17 18:29:50 +00:00
|
|
|
function M.extract_highlight_colors(color_group, scope)
|
2021-02-21 14:37:59 +00:00
|
|
|
if vim.fn.hlexists(color_group) == 0 then return nil end
|
2021-04-13 12:36:51 +00:00
|
|
|
local 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
|
2021-02-20 03:21:05 +00:00
|
|
|
end
|
2021-04-13 12:36:51 +00:00
|
|
|
if color.foreground ~= nil then
|
|
|
|
color.fg = string.format('#%06x', color.foreground)
|
|
|
|
color.foreground = nil
|
2021-02-20 03:21:05 +00:00
|
|
|
end
|
|
|
|
if scope then return color[scope] end
|
|
|
|
return color
|
|
|
|
end
|
|
|
|
|
2021-02-21 14:37:59 +00:00
|
|
|
-- table to store the highlight names created by lualine
|
|
|
|
M.loaded_highlights = {}
|
|
|
|
|
|
|
|
-- sets loaded_highlights table
|
2021-03-22 14:29:37 +00:00
|
|
|
function M.save_highlight(highlight_name, highlight_args)
|
|
|
|
M.loaded_highlights[highlight_name] = highlight_args
|
2021-02-21 14:37:59 +00:00
|
|
|
end
|
|
|
|
|
2021-03-22 14:29:37 +00:00
|
|
|
function M.reload_highlights()
|
|
|
|
local highlight = require('lualine.highlight')
|
|
|
|
for _, highlight_args in pairs(M.loaded_highlights) do
|
2021-04-07 00:34:36 +00:00
|
|
|
highlight.highlight(unpack(highlight_args))
|
2021-02-21 14:37:59 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2021-02-20 03:21:05 +00:00
|
|
|
-- determine if an highlight exist and isn't cleared
|
|
|
|
function M.highlight_exists(highlight_name)
|
2021-02-21 14:37:59 +00:00
|
|
|
return M.loaded_highlights[highlight_name] and true or false
|
2021-02-17 18:29:50 +00:00
|
|
|
end
|
|
|
|
|
2021-04-07 00:34:36 +00:00
|
|
|
-- clears loaded_highlights table and highlights
|
|
|
|
function M.clear_highlights()
|
|
|
|
for highlight_name, _ in pairs(M.loaded_highlights) do
|
|
|
|
vim.cmd('highlight clear ' .. highlight_name)
|
|
|
|
M.loaded_highlights[highlight_name] = nil
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2021-04-11 08:20:41 +00:00
|
|
|
-- remove empty strings from list
|
|
|
|
function M.list_shrink(list)
|
|
|
|
local new_list = {}
|
|
|
|
for i = 1, #list do
|
|
|
|
if list[i] and #list[i] > 0 then table.insert(new_list, list[i]) end
|
|
|
|
end
|
|
|
|
return new_list
|
|
|
|
end
|
|
|
|
|
2021-08-08 16:03:58 +00:00
|
|
|
-- Check if a auto command is already defined
|
|
|
|
local function autocmd_is_defined(event, patern, command_str)
|
|
|
|
return vim.api.nvim_exec(string.format("au lualine %s %s",
|
|
|
|
event, patern), true):find(command_str) ~= nil
|
|
|
|
end
|
|
|
|
|
|
|
|
-- Define a auto command if it's not already defined
|
|
|
|
function M.define_autocmd(event, patern, cmd)
|
|
|
|
if not cmd then cmd = patern; patern = '*' end
|
|
|
|
if not autocmd_is_defined(event, patern, cmd) then
|
|
|
|
vim.cmd(string.format("autocmd lualine %s %s %s", event, patern, 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()
|
|
|
|
return tonumber(vim.g.actual_curwin) == vim.fn.win_getid()
|
|
|
|
end
|
|
|
|
|
2021-08-20 03:54:18 +00:00
|
|
|
function M.is_valid_filename(name)
|
2021-08-20 04:34:31 +00:00
|
|
|
local invalid_chars="[^a-zA-Z0-9_. -]"
|
2021-08-20 03:54:18 +00:00
|
|
|
return name:find(invalid_chars) == nil
|
|
|
|
end
|
|
|
|
|
2021-08-25 11:43:12 +00:00
|
|
|
function M.charAt(str, pos)
|
|
|
|
return string.char(str:byte(pos))
|
|
|
|
end
|
|
|
|
|
2021-02-15 18:09:12 +00:00
|
|
|
return M
|