lualine.nvim/lua/lualine.lua

128 lines
3.5 KiB
Lua
Raw Normal View History

2020-12-30 14:48:51 +00:00
local utils = require('lualine.utils')
local highlight = require('lualine.highlight')
local M = { }
M.theme = 'gruvbox'
local theme_set = {}
2020-12-30 14:48:51 +00:00
M.separator = '|'
M.sections = {
lualine_a = { 'mode' },
lualine_b = { 'branch' },
lualine_c = { 'filename' },
lualine_x = { 'encoding', 'fileformat', 'filetype' },
lualine_y = { 'progress' },
lualine_z = { 'location' },
}
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 = {
}
local function load_components()
local function load_sections(sections)
2020-12-30 14:48:51 +00:00
for _, section in pairs(sections) do
for index, component in pairs(section) do
if type(component) == 'string' then
section[index] = require('lualine.components.' .. component)
end
end
end
end
load_sections(M.sections)
load_sections(M.inactive_sections)
2020-12-30 14:48:51 +00:00
end
local function load_extensions()
2020-12-30 14:48:51 +00:00
for _, extension in pairs(M.extensions) do
if type(extension) == 'string' then
require('lualine.extensions.' .. extension).load_extension()
2020-12-30 14:48:51 +00:00
end
if type(extension) == 'table' then
extension.load_extension()
2020-12-30 14:48:51 +00:00
end
if type(extension) == 'function' then
extension()
end
end
end
local function set_lualine_theme()
if type(M.theme) == 'string' then
M.theme = require('lualine.themes.'.. M.theme)
end
highlight.create_highlight_groups(M.theme)
theme_set = M.theme
end
local function statusline(is_focused)
2020-12-30 14:48:51 +00:00
local sections = M.sections
if not is_focused then
sections = M.inactive_sections
2020-12-30 14:48:51 +00:00
end
if M.theme ~= theme_set then
set_lualine_theme()
2020-12-30 14:48:51 +00:00
end
local status = {}
if sections.lualine_a then
table.insert(status, highlight.format_highlight(is_focused, 'lualine_a'))
table.insert(status, utils.draw_section(sections.lualine_a, M.separator))
2020-12-30 14:48:51 +00:00
end
if sections.lualine_b then
table.insert(status, highlight.format_highlight(is_focused, 'lualine_b'))
table.insert(status, utils.draw_section(sections.lualine_b, M.separator))
2020-12-30 14:48:51 +00:00
end
if sections.lualine_c then
table.insert(status, highlight.format_highlight(is_focused, 'lualine_c'))
table.insert(status, utils.draw_section(sections.lualine_c, M.separator))
2020-12-30 14:48:51 +00:00
end
table.insert(status, "%=")
if sections.lualine_x then
table.insert(status, highlight.format_highlight(is_focused, 'lualine_c'))
table.insert(status, utils.draw_section(sections.lualine_x, M.separator))
2020-12-30 14:48:51 +00:00
end
if sections.lualine_y then
table.insert(status, highlight.format_highlight(is_focused, 'lualine_b'))
table.insert(status, utils.draw_section(sections.lualine_y, M.separator))
2020-12-30 14:48:51 +00:00
end
if sections.lualine_z then
table.insert(status, highlight.format_highlight(is_focused, 'lualine_a'))
table.insert(status, utils.draw_section(sections.lualine_z, M.separator))
2020-12-30 14:48:51 +00:00
end
return table.concat(status)
2020-12-30 14:48:51 +00:00
end
2021-01-05 23:43:36 +00:00
function M.set_inactive_statusline()
vim.wo.statusline = statusline()
end
local function exec_autocommands()
_G.set_lualine_theme = set_lualine_theme
_G.set_active_statusline = statusline
vim.cmd([[augroup lualine]])
vim.cmd([[autocmd!]])
vim.cmd([[autocmd WinEnter,BufEnter * setlocal statusline=%!v:lua.set_active_statusline(1)]])
2021-01-05 23:43:36 +00:00
vim.cmd([[autocmd WinLeave,BufLeave * lua require('lualine').set_inactive_statusline()]])
vim.cmd([[autocmd ColorScheme * call v:lua.set_lualine_theme()]])
vim.cmd([[augroup END]])
end
function M.status()
load_components()
load_extensions()
set_lualine_theme()
exec_autocommands()
2020-12-30 14:48:51 +00:00
end
return M