2021-02-13 00:03:57 +00:00
|
|
|
-- Copyright (c) 2020-2021 hoob3rt
|
|
|
|
-- MIT license, see LICENSE for more details.
|
|
|
|
|
2021-02-15 18:09:12 +00:00
|
|
|
local utils_component = require('lualine.utils.component')
|
|
|
|
local utils = require('lualine.utils.utils')
|
2020-12-30 14:48:51 +00:00
|
|
|
local highlight = require('lualine.highlight')
|
|
|
|
|
|
|
|
local M = { }
|
|
|
|
|
2021-01-04 00:17:51 +00:00
|
|
|
local theme_set = {}
|
2020-12-30 14:48:51 +00:00
|
|
|
|
2021-02-15 18:09:12 +00:00
|
|
|
M.options = {
|
|
|
|
icons_enabled = true,
|
|
|
|
theme = 'gruvbox',
|
|
|
|
separator = '|',
|
|
|
|
}
|
2020-12-30 14:48:51 +00:00
|
|
|
|
|
|
|
M.sections = {
|
|
|
|
lualine_a = { 'mode' },
|
|
|
|
lualine_b = { 'branch' },
|
|
|
|
lualine_c = { 'filename' },
|
|
|
|
lualine_x = { 'encoding', 'fileformat', 'filetype' },
|
|
|
|
lualine_y = { 'progress' },
|
|
|
|
lualine_z = { 'location' },
|
|
|
|
}
|
|
|
|
|
2021-01-04 01:14:29 +00:00
|
|
|
M.inactive_sections = {
|
2020-12-30 14:48:51 +00:00
|
|
|
lualine_a = { },
|
|
|
|
lualine_b = { },
|
|
|
|
lualine_c = { 'filename' },
|
|
|
|
lualine_x = { 'location' },
|
|
|
|
lualine_y = { },
|
2021-01-05 23:43:36 +00:00
|
|
|
lualine_z = { }
|
2020-12-30 14:48:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
M.extensions = {
|
|
|
|
}
|
|
|
|
|
2021-02-10 10:40:29 +00:00
|
|
|
local function load_special_components(component)
|
|
|
|
return function()
|
|
|
|
-- precedence lualine_component > vim_var > lua_var > vim_function
|
|
|
|
if component:find('[gvtwb]?o?:') == 1 then
|
|
|
|
-- vim veriable component
|
|
|
|
-- accepts g:, v:, t:, w:, b:, o, go:, vo:, to:, wo:, bo:
|
|
|
|
-- filters g portion from g:var
|
|
|
|
local scope = component:match('[gvtwb]?o?')
|
|
|
|
-- filters var portion from g:var
|
|
|
|
-- For some reason overwriting component var from outer scope causes the
|
|
|
|
-- component not to work . So creating a new local name component to use:/
|
|
|
|
local component = component:sub(#scope + 2, #component)
|
|
|
|
-- Displays nothing when veriable aren't present
|
|
|
|
local return_val = vim[scope][component]
|
|
|
|
if return_val == nil then return '' end
|
|
|
|
local ok
|
|
|
|
ok, return_val = pcall(tostring, return_val)
|
|
|
|
if ok then return return_val end
|
|
|
|
return ''
|
|
|
|
elseif loadstring(string.format('return %s ~= nil', component)) and
|
|
|
|
loadstring(string.format([[return %s ~= nil]], component))() then
|
|
|
|
-- lua veriable component
|
|
|
|
return loadstring(string.format([[
|
|
|
|
local ok, return_val = pcall(tostring, %s)
|
|
|
|
if ok then return return_val end
|
|
|
|
return '']], component))()
|
|
|
|
else
|
|
|
|
-- vim function component
|
|
|
|
local ok, return_val = pcall(vim.fn[component])
|
|
|
|
if not ok then return '' end -- function call failed
|
|
|
|
ok, return_val = pcall(tostring, return_val)
|
|
|
|
if ok then return return_val else return '' end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2021-02-15 18:09:12 +00:00
|
|
|
local function component_loader(component)
|
|
|
|
if type(component[1]) == 'function' then return component end
|
|
|
|
if type(component[1]) == 'string' then
|
|
|
|
-- Keep component name for later use as component[1] will be overwritten
|
|
|
|
-- With component function
|
|
|
|
component.component_name = component[1]
|
|
|
|
-- apply default args
|
|
|
|
for opt_name, opt_val in pairs(M.options) do
|
|
|
|
if component[opt_name] == nil then
|
|
|
|
component[opt_name] = opt_val
|
|
|
|
end
|
|
|
|
end
|
|
|
|
-- load the component
|
|
|
|
local ok, loaded_component = pcall(require, 'lualine.components.' .. component.component_name)
|
|
|
|
if not ok then
|
|
|
|
loaded_component = load_special_components(component.component_name)
|
|
|
|
end
|
|
|
|
component[1] = loaded_component
|
|
|
|
if type(component[1]) == 'table' then
|
|
|
|
component[1] = component[1].init(component)
|
|
|
|
end
|
|
|
|
-- set custom highlights
|
|
|
|
if component.color then
|
|
|
|
local function update_color()
|
|
|
|
component.color_highlight = highlight.create_component_highlight_group(
|
|
|
|
component.color, component.component_name, component)
|
|
|
|
end
|
|
|
|
update_color()
|
|
|
|
utils.expand_set_theme(update_color)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
|
2021-01-04 01:14:29 +00:00
|
|
|
local function load_components()
|
|
|
|
local function load_sections(sections)
|
2021-02-15 18:09:12 +00:00
|
|
|
for section_name, section in pairs(sections) do
|
2020-12-30 14:48:51 +00:00
|
|
|
for index, component in pairs(section) do
|
2021-02-15 18:09:12 +00:00
|
|
|
if type(component) == 'string' or type(component) == 'function' then
|
|
|
|
component = {component}
|
2020-12-30 14:48:51 +00:00
|
|
|
end
|
2021-02-15 18:09:12 +00:00
|
|
|
component.self = {}
|
|
|
|
component.self.section = section_name
|
|
|
|
component_loader(component)
|
|
|
|
section[index] = component
|
2020-12-30 14:48:51 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2021-01-04 01:14:29 +00:00
|
|
|
load_sections(M.sections)
|
|
|
|
load_sections(M.inactive_sections)
|
2020-12-30 14:48:51 +00:00
|
|
|
end
|
|
|
|
|
2021-01-04 01:14:29 +00:00
|
|
|
local function load_extensions()
|
2020-12-30 14:48:51 +00:00
|
|
|
for _, extension in pairs(M.extensions) do
|
|
|
|
if type(extension) == 'string' then
|
2021-01-04 01:14:29 +00:00
|
|
|
require('lualine.extensions.' .. extension).load_extension()
|
2020-12-30 14:48:51 +00:00
|
|
|
end
|
|
|
|
if type(extension) == 'table' then
|
2021-01-04 01:14:29 +00:00
|
|
|
extension.load_extension()
|
2020-12-30 14:48:51 +00:00
|
|
|
end
|
|
|
|
if type(extension) == 'function' then
|
|
|
|
extension()
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2021-01-04 01:14:29 +00:00
|
|
|
local function set_lualine_theme()
|
2021-02-15 18:09:12 +00:00
|
|
|
if type(M.options.theme) == 'string' then
|
|
|
|
M.options.theme = require('lualine.themes.'.. M.options.theme)
|
2021-01-04 00:17:51 +00:00
|
|
|
end
|
2021-02-15 18:09:12 +00:00
|
|
|
highlight.create_highlight_groups(M.options.theme)
|
|
|
|
theme_set = M.options.theme
|
2021-01-04 00:17:51 +00:00
|
|
|
end
|
|
|
|
|
2021-02-05 12:29:34 +00:00
|
|
|
local function statusline(sections, is_focused)
|
2021-02-12 11:53:52 +00:00
|
|
|
if M.theme ~= theme_set then
|
|
|
|
set_lualine_theme()
|
|
|
|
end
|
2021-01-03 19:11:22 +00:00
|
|
|
local status = {}
|
2021-01-04 01:14:29 +00:00
|
|
|
if sections.lualine_a then
|
2021-02-15 18:09:12 +00:00
|
|
|
local hl = highlight.format_highlight(is_focused, 'lualine_a')
|
|
|
|
table.insert(status, utils_component.draw_section(sections.lualine_a, hl))
|
2020-12-30 14:48:51 +00:00
|
|
|
end
|
2021-01-04 01:14:29 +00:00
|
|
|
if sections.lualine_b then
|
2021-02-15 18:09:12 +00:00
|
|
|
local hl = highlight.format_highlight(is_focused, 'lualine_b')
|
|
|
|
table.insert(status, utils_component.draw_section(sections.lualine_b, hl))
|
2020-12-30 14:48:51 +00:00
|
|
|
end
|
2021-01-04 01:14:29 +00:00
|
|
|
if sections.lualine_c then
|
2021-02-15 18:09:12 +00:00
|
|
|
local hl = highlight.format_highlight(is_focused, 'lualine_c')
|
|
|
|
table.insert(status, utils_component.draw_section(sections.lualine_c, hl))
|
2020-12-30 14:48:51 +00:00
|
|
|
end
|
2021-01-03 19:11:22 +00:00
|
|
|
table.insert(status, "%=")
|
2021-01-04 01:14:29 +00:00
|
|
|
if sections.lualine_x then
|
2021-02-15 18:09:12 +00:00
|
|
|
local hl = highlight.format_highlight(is_focused, 'lualine_c')
|
|
|
|
table.insert(status, utils_component.draw_section(sections.lualine_x, hl))
|
2020-12-30 14:48:51 +00:00
|
|
|
end
|
2021-01-04 01:14:29 +00:00
|
|
|
if sections.lualine_y then
|
2021-02-15 18:09:12 +00:00
|
|
|
local hl = highlight.format_highlight(is_focused, 'lualine_b')
|
|
|
|
table.insert(status, utils_component.draw_section(sections.lualine_y, hl))
|
2020-12-30 14:48:51 +00:00
|
|
|
end
|
2021-01-04 01:14:29 +00:00
|
|
|
if sections.lualine_z then
|
2021-02-15 18:09:12 +00:00
|
|
|
local hl = highlight.format_highlight(is_focused, 'lualine_a')
|
|
|
|
table.insert(status, utils_component.draw_section(sections.lualine_z, hl))
|
2020-12-30 14:48:51 +00:00
|
|
|
end
|
2021-01-03 19:11:22 +00:00
|
|
|
return table.concat(status)
|
2020-12-30 14:48:51 +00:00
|
|
|
end
|
|
|
|
|
2021-02-05 12:29:34 +00:00
|
|
|
local function status_dispatch()
|
|
|
|
if vim.g.statusline_winid == vim.fn.win_getid() then
|
|
|
|
return statusline(M.sections, true)
|
|
|
|
else
|
|
|
|
return statusline(M.inactive_sections, false)
|
|
|
|
end
|
2021-01-05 23:43:36 +00:00
|
|
|
end
|
|
|
|
|
2021-01-04 01:14:29 +00:00
|
|
|
local function exec_autocommands()
|
|
|
|
_G.set_lualine_theme = set_lualine_theme
|
2021-01-15 04:15:04 +00:00
|
|
|
vim.api.nvim_exec([[
|
|
|
|
augroup lualine
|
|
|
|
autocmd!
|
|
|
|
autocmd ColorScheme * call v:lua.set_lualine_theme()
|
|
|
|
augroup END
|
|
|
|
]], false)
|
2021-01-04 00:17:51 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
function M.status()
|
2021-01-04 01:14:29 +00:00
|
|
|
set_lualine_theme()
|
|
|
|
exec_autocommands()
|
2021-02-15 18:09:12 +00:00
|
|
|
load_components()
|
|
|
|
load_extensions()
|
2021-02-05 12:29:34 +00:00
|
|
|
_G.lualine_statusline = status_dispatch
|
|
|
|
vim.o.statusline = '%!v:lua.lualine_statusline()'
|
2020-12-30 14:48:51 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
return M
|