2021-02-13 00:03:57 +00:00
|
|
|
-- Copyright (c) 2020-2021 hoob3rt
|
|
|
|
-- MIT license, see LICENSE for more details.
|
2021-03-18 00:57:59 +00:00
|
|
|
local utils_section = require('lualine.utils.section')
|
2020-12-30 14:48:51 +00:00
|
|
|
local highlight = require('lualine.highlight')
|
2021-03-18 01:13:40 +00:00
|
|
|
local config = require('lualine.defaults')
|
2020-12-30 14:48:51 +00:00
|
|
|
|
2021-03-10 23:07:38 +00:00
|
|
|
local function apply_configuration(config_table)
|
|
|
|
if not config_table then return end
|
|
|
|
local function parse_sections(section_group_name)
|
|
|
|
if not config_table[section_group_name] then return end
|
|
|
|
for section_name, section in pairs(config_table[section_group_name]) do
|
2021-03-17 00:02:13 +00:00
|
|
|
config[section_group_name][section_name] =
|
2021-03-15 23:37:46 +00:00
|
|
|
config_table[section_group_name][section_name]
|
2021-03-10 23:07:38 +00:00
|
|
|
if type(section) == 'table' then
|
|
|
|
for _, component in pairs(section) do
|
|
|
|
if type(component) == 'table' and type(component[2]) == 'table' then
|
|
|
|
local options = component[2]
|
|
|
|
component[2] = nil
|
2021-03-15 23:37:46 +00:00
|
|
|
for key, val in pairs(options) do component[key] = val end
|
2021-03-10 23:07:38 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
parse_sections('options')
|
|
|
|
parse_sections('sections')
|
|
|
|
parse_sections('inactive_sections')
|
2021-03-06 15:03:00 +00:00
|
|
|
parse_sections('tabline')
|
2021-03-17 00:02:13 +00:00
|
|
|
if config_table.extensions then config.extensions = config_table.extensions end
|
2021-03-10 23:07:38 +00:00
|
|
|
end
|
2020-12-30 14:48:51 +00:00
|
|
|
|
2021-02-20 03:21:05 +00:00
|
|
|
local function check_single_separator()
|
2021-03-17 00:02:13 +00:00
|
|
|
local compoennt_separator = config.options.component_separators
|
|
|
|
local section_separator = config.options.section_separators
|
|
|
|
if config.options.component_separators ~= nil then
|
|
|
|
if type(config.options.component_separators) == 'string' then
|
|
|
|
config.options.component_separators =
|
|
|
|
{compoennt_separator, compoennt_separator}
|
|
|
|
elseif #config.options.component_separators == 1 then
|
|
|
|
config.options.component_separators =
|
|
|
|
{
|
|
|
|
config.options.component_separators[1],
|
|
|
|
config.options.component_separators[1]
|
|
|
|
}
|
2021-02-22 19:48:23 +00:00
|
|
|
end
|
2021-02-20 03:21:05 +00:00
|
|
|
end
|
2021-03-17 00:02:13 +00:00
|
|
|
if config.options.section_separators ~= nil then
|
|
|
|
if type(config.options.section_separators) == 'string' then
|
|
|
|
config.options.section_separators = {section_separator, section_separator}
|
|
|
|
elseif #config.options.section_separators == 1 then
|
|
|
|
config.options.section_separators =
|
|
|
|
{
|
|
|
|
config.options.section_separators[1],
|
|
|
|
config.options.section_separators[1]
|
|
|
|
}
|
2021-02-22 19:48:23 +00:00
|
|
|
end
|
2021-02-20 03:21:05 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2021-02-15 18:09:12 +00:00
|
|
|
local function component_loader(component)
|
2021-04-11 08:20:41 +00:00
|
|
|
if type(component[1]) == 'function' then
|
|
|
|
return require 'lualine.components.special.functon_component':new(component)
|
|
|
|
end
|
2021-02-15 18:09:12 +00:00
|
|
|
if type(component[1]) == 'string' then
|
|
|
|
-- load the component
|
2021-04-11 08:20:41 +00:00
|
|
|
local ok, loaded_component = pcall(require,
|
|
|
|
'lualine.components.' .. component[1])
|
|
|
|
if ok then
|
|
|
|
component.component_name = component[1]
|
|
|
|
loaded_component = loaded_component:new(component)
|
|
|
|
elseif component[1]:find('[gvtwb]?o?:') == 1 then
|
|
|
|
loaded_component =
|
|
|
|
require 'lualine.components.special.vim_var_component':new(component)
|
|
|
|
else
|
|
|
|
loaded_component =
|
|
|
|
require 'lualine.components.special.eval_func_component':new(component)
|
2021-02-15 18:09:12 +00:00
|
|
|
end
|
2021-04-11 08:20:41 +00:00
|
|
|
return loaded_component
|
2021-02-15 18:09:12 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2021-03-10 23:07:38 +00:00
|
|
|
local function load_sections(sections)
|
|
|
|
for section_name, section in pairs(sections) do
|
|
|
|
for index, component in pairs(section) do
|
|
|
|
if type(component) == 'string' or type(component) == 'function' then
|
|
|
|
component = {component}
|
2020-12-30 14:48:51 +00:00
|
|
|
end
|
2021-03-10 23:07:38 +00:00
|
|
|
component.self = {}
|
|
|
|
component.self.section = section_name
|
2021-04-11 08:20:41 +00:00
|
|
|
-- apply default args
|
|
|
|
component = vim.tbl_extend('keep', component, config.options)
|
|
|
|
section[index] = component_loader(component)
|
2020-12-30 14:48:51 +00:00
|
|
|
end
|
|
|
|
end
|
2021-03-10 23:07:38 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
local function load_components()
|
2021-03-17 00:02:13 +00:00
|
|
|
load_sections(config.sections)
|
|
|
|
load_sections(config.inactive_sections)
|
|
|
|
load_sections(config.tabline)
|
2020-12-30 14:48:51 +00:00
|
|
|
end
|
|
|
|
|
2021-03-15 23:37:46 +00:00
|
|
|
local function load_extensions()
|
2021-03-17 00:02:13 +00:00
|
|
|
for index, extension in pairs(config.extensions) do
|
2021-03-10 23:07:38 +00:00
|
|
|
local local_extension = require('lualine.extensions.' .. extension)
|
|
|
|
load_sections(local_extension.sections)
|
|
|
|
load_sections(local_extension.inactive_sections)
|
2021-03-17 00:02:13 +00:00
|
|
|
config.extensions[index] = local_extension
|
2020-12-30 14:48:51 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2021-03-13 00:21:37 +00:00
|
|
|
local function statusline(sections, is_focused)
|
2021-02-20 03:21:05 +00:00
|
|
|
local function create_status_builder()
|
|
|
|
-- The sequence sections should maintain
|
|
|
|
local section_sequence = {'a', 'b', 'c', 'x', 'y', 'z'}
|
|
|
|
local status_builder = {}
|
|
|
|
for _, section_name in ipairs(section_sequence) do
|
2021-03-15 23:37:46 +00:00
|
|
|
if sections['lualine_' .. section_name] then
|
2021-02-20 03:21:05 +00:00
|
|
|
-- insert highlight+components of this section to status_builder
|
|
|
|
local section_highlight = highlight.format_highlight(is_focused,
|
2021-03-15 23:37:46 +00:00
|
|
|
'lualine_' ..
|
|
|
|
section_name)
|
2021-03-18 00:57:59 +00:00
|
|
|
local section_data = utils_section.draw_section(
|
2021-03-15 23:37:46 +00:00
|
|
|
sections['lualine_' .. section_name],
|
|
|
|
section_highlight)
|
2021-02-20 03:21:05 +00:00
|
|
|
if #section_data > 0 then
|
2021-03-15 23:37:46 +00:00
|
|
|
table.insert(status_builder,
|
|
|
|
{name = section_name, data = section_data})
|
2021-02-20 03:21:05 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
return status_builder
|
2020-12-30 14:48:51 +00:00
|
|
|
end
|
2021-02-20 03:21:05 +00:00
|
|
|
-- status_builder stores statusline without section_separators
|
|
|
|
local status_builder = create_status_builder()
|
|
|
|
|
|
|
|
-- Actual statusline
|
|
|
|
local status = {}
|
|
|
|
local half_passed = false
|
2021-03-15 23:37:46 +00:00
|
|
|
for i = 1, #status_builder do
|
2021-02-20 03:21:05 +00:00
|
|
|
-- midsection divider
|
|
|
|
if not half_passed and status_builder[i].name > 'c' then
|
2021-03-15 23:37:46 +00:00
|
|
|
table.insert(status,
|
|
|
|
highlight.format_highlight(is_focused, 'lualine_c') .. '%=')
|
2021-02-20 03:21:05 +00:00
|
|
|
half_passed = true
|
|
|
|
end
|
|
|
|
-- provide section_separators when statusline is in focus
|
|
|
|
if is_focused then
|
|
|
|
-- component separator needs to have fg = current_section.bg
|
|
|
|
-- and bg = adjacent_section.bg
|
2021-03-15 23:37:46 +00:00
|
|
|
local previous_section = status_builder[i - 1] or {}
|
2021-02-20 03:21:05 +00:00
|
|
|
local current_section = status_builder[i]
|
2021-03-15 23:37:46 +00:00
|
|
|
local next_section = status_builder[i + 1] or {}
|
2021-02-20 03:21:05 +00:00
|
|
|
-- For 2nd half we need to show separator before section
|
|
|
|
if current_section.name > 'x' then
|
2021-03-15 23:37:46 +00:00
|
|
|
local transitional_highlight = highlight.get_transitional_highlights(
|
|
|
|
previous_section.data,
|
|
|
|
current_section.data, true)
|
2021-03-17 00:02:13 +00:00
|
|
|
if transitional_highlight and config.options.section_separators and
|
|
|
|
config.options.section_separators[2] then
|
2021-03-15 23:37:46 +00:00
|
|
|
table.insert(status, transitional_highlight ..
|
2021-03-17 00:02:13 +00:00
|
|
|
config.options.section_separators[2])
|
2021-02-20 03:21:05 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
-- **( insert the actual section in the middle )** --
|
|
|
|
table.insert(status, status_builder[i].data)
|
|
|
|
|
|
|
|
-- For 1st half we need to show separator after section
|
|
|
|
if current_section.name < 'c' then
|
2021-03-15 23:37:46 +00:00
|
|
|
local transitional_highlight = highlight.get_transitional_highlights(
|
|
|
|
current_section.data,
|
|
|
|
next_section.data)
|
2021-03-17 00:02:13 +00:00
|
|
|
if transitional_highlight and config.options.section_separators and
|
|
|
|
config.options.section_separators[1] then
|
2021-03-15 23:37:46 +00:00
|
|
|
table.insert(status, transitional_highlight ..
|
2021-03-17 00:02:13 +00:00
|
|
|
config.options.section_separators[1])
|
2021-02-20 03:21:05 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
else -- when not in focus
|
|
|
|
table.insert(status, status_builder[i].data)
|
|
|
|
end
|
2020-12-30 14:48:51 +00:00
|
|
|
end
|
2021-02-20 03:21:05 +00:00
|
|
|
-- incase none of x,y,z was configured lets not fill whole statusline with a,b,c section
|
|
|
|
if not half_passed then
|
2021-03-15 23:37:46 +00:00
|
|
|
table.insert(status,
|
|
|
|
highlight.format_highlight(is_focused, 'lualine_c') .. '%=')
|
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-03-13 00:21:37 +00:00
|
|
|
-- check if any extension matches the filetype and return proper sections
|
|
|
|
local function get_extension_sections()
|
|
|
|
local sections, inactive_sections = nil, nil
|
2021-03-17 00:02:13 +00:00
|
|
|
for _, extension in ipairs(config.extensions) do
|
2021-03-13 00:21:37 +00:00
|
|
|
for _, filetype in ipairs(extension.filetypes) do
|
|
|
|
if vim.bo.filetype == filetype then
|
|
|
|
sections = extension.sections
|
|
|
|
inactive_sections = extension.inactive_sections
|
|
|
|
break
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
return {sections = sections, inactive_sections = inactive_sections}
|
|
|
|
end
|
|
|
|
|
2021-02-05 12:29:34 +00:00
|
|
|
local function status_dispatch()
|
2021-03-13 00:21:37 +00:00
|
|
|
local extension_sections = get_extension_sections()
|
2021-02-05 12:29:34 +00:00
|
|
|
if vim.g.statusline_winid == vim.fn.win_getid() then
|
2021-03-13 00:21:37 +00:00
|
|
|
local sections = extension_sections.sections
|
2021-03-17 00:02:13 +00:00
|
|
|
if sections == nil then sections = config.sections end
|
2021-03-13 00:21:37 +00:00
|
|
|
return statusline(sections, true)
|
2021-02-05 12:29:34 +00:00
|
|
|
else
|
2021-03-13 00:21:37 +00:00
|
|
|
local inactive_sections = extension_sections.inactive_sections
|
2021-03-17 00:02:13 +00:00
|
|
|
if inactive_sections == nil then
|
|
|
|
inactive_sections = config.inactive_sections
|
|
|
|
end
|
2021-03-13 00:21:37 +00:00
|
|
|
return statusline(inactive_sections, false)
|
2021-02-05 12:29:34 +00:00
|
|
|
end
|
2021-01-05 23:43:36 +00:00
|
|
|
end
|
|
|
|
|
2021-03-17 00:02:13 +00:00
|
|
|
local function tabline() return statusline(config.tabline, true) end
|
2021-03-06 15:03:00 +00:00
|
|
|
|
|
|
|
local function setup_theme()
|
2021-04-08 11:06:31 +00:00
|
|
|
local function get_theme_from_config()
|
|
|
|
local theme_name = config.options.theme
|
|
|
|
if type(theme_name) == 'string' then
|
|
|
|
local ok, theme = pcall(require, 'lualine.themes.' .. theme_name)
|
|
|
|
if ok then return theme end
|
|
|
|
elseif type(theme_name) == 'table' then
|
|
|
|
-- use the provided theme as-is
|
|
|
|
return config.options.theme
|
2021-04-07 00:34:36 +00:00
|
|
|
end
|
2021-04-08 11:06:31 +00:00
|
|
|
vim.api.nvim_echo({
|
|
|
|
{
|
|
|
|
'theme ' .. tostring(theme_name) .. ' not found, defaulting to gruvbox',
|
|
|
|
'ErrorMsg'
|
|
|
|
}
|
|
|
|
}, true, {})
|
|
|
|
return require 'lualine.themes.gruvbox'
|
2021-03-22 14:29:37 +00:00
|
|
|
end
|
2021-04-08 11:06:31 +00:00
|
|
|
local theme = get_theme_from_config()
|
2021-04-07 00:34:36 +00:00
|
|
|
highlight.create_highlight_groups(theme)
|
2021-01-15 04:15:04 +00:00
|
|
|
vim.api.nvim_exec([[
|
2021-02-20 03:21:05 +00:00
|
|
|
augroup lualine
|
|
|
|
autocmd!
|
2021-03-22 14:29:37 +00:00
|
|
|
autocmd ColorScheme * lua require'lualine.utils.utils'.reload_highlights()
|
2021-02-20 03:21:05 +00:00
|
|
|
augroup END
|
2021-01-15 04:15:04 +00:00
|
|
|
]], false)
|
2021-01-04 00:17:51 +00:00
|
|
|
end
|
|
|
|
|
2021-03-06 15:03:00 +00:00
|
|
|
local function set_tabline()
|
2021-03-17 00:02:13 +00:00
|
|
|
if next(config.tabline) ~= nil then
|
2021-04-09 11:34:22 +00:00
|
|
|
vim.o.tabline = "%!v:lua.require'lualine'.tabline()"
|
2021-03-06 15:03:00 +00:00
|
|
|
vim.o.showtabline = 2
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
local function set_statusline()
|
2021-03-17 00:02:13 +00:00
|
|
|
if next(config.sections) ~= nil or next(config.inactive_sections) ~= nil then
|
2021-04-09 11:34:22 +00:00
|
|
|
vim.o.statusline = "%!v:lua.require'lualine'.statusline()"
|
2021-03-06 15:03:00 +00:00
|
|
|
vim.api.nvim_exec([[
|
2021-04-09 11:34:22 +00:00
|
|
|
autocmd lualine WinLeave,BufLeave * lua vim.wo.statusline=require'lualine'.statusline()
|
|
|
|
autocmd lualine WinEnter,BufEnter * set statusline<
|
2021-03-06 15:03:00 +00:00
|
|
|
]], false)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2021-03-25 11:03:43 +00:00
|
|
|
local function setup(user_config)
|
2021-03-02 12:58:34 +00:00
|
|
|
apply_configuration(vim.g.lualine)
|
2021-03-17 00:02:13 +00:00
|
|
|
apply_configuration(user_config)
|
2021-02-20 03:21:05 +00:00
|
|
|
check_single_separator()
|
2021-03-06 15:03:00 +00:00
|
|
|
setup_theme()
|
2021-02-15 18:09:12 +00:00
|
|
|
load_components()
|
|
|
|
load_extensions()
|
2021-03-06 15:03:00 +00:00
|
|
|
set_statusline()
|
|
|
|
set_tabline()
|
2020-12-30 14:48:51 +00:00
|
|
|
end
|
|
|
|
|
2021-04-09 11:34:22 +00:00
|
|
|
return {
|
|
|
|
setup = setup,
|
|
|
|
statusline = status_dispatch,
|
|
|
|
tabline = tabline,
|
|
|
|
}
|