| 1 |
-- Copyright (c) 2020-2021 hoob3rt |
| 2 |
-- MIT license, see LICENSE for more details. |
| 3 |
local require = require('lualine_require').require |
| 4 |
local utils = require('lualine.utils.utils') |
| 5 |
local modules = require('lualine_require').lazy_require { |
| 6 |
utils_notices = 'lualine.utils.notices', |
| 7 |
} |
| 8 |
|
| 9 |
local config = { |
| 10 |
options = { |
| 11 |
icons_enabled = true, |
| 12 |
theme = 'auto', |
| 13 |
component_separators = { left = '', right = '' }, |
| 14 |
section_separators = { left = '', right = '' }, |
| 15 |
disabled_filetypes = { |
| 16 |
statusline = {}, |
| 17 |
winbar = {}, |
| 18 |
}, |
| 19 |
ignore_focus = {}, |
| 20 |
always_divide_middle = true, |
| 21 |
always_show_tabline = true, |
| 22 |
globalstatus = vim.go.laststatus == 3, |
| 23 |
refresh = { |
| 24 |
statusline = 1000, |
| 25 |
tabline = 1000, |
| 26 |
winbar = 1000, |
| 27 |
refresh_time = 16, -- ~60fps |
| 28 |
events = { |
| 29 |
'WinEnter', |
| 30 |
'BufEnter', |
| 31 |
'BufWritePost', |
| 32 |
'SessionLoadPost', |
| 33 |
'FileChangedShellPost', |
| 34 |
'VimResized', |
| 35 |
'Filetype', |
| 36 |
'CursorMoved', |
| 37 |
'CursorMovedI', |
| 38 |
'ModeChanged', |
| 39 |
}, |
| 40 |
}, |
| 41 |
}, |
| 42 |
sections = { |
| 43 |
lualine_a = { 'mode' }, |
| 44 |
lualine_b = { 'branch', 'diff', 'diagnostics' }, |
| 45 |
lualine_c = { 'filename' }, |
| 46 |
lualine_x = { 'encoding', 'fileformat', 'filetype' }, |
| 47 |
lualine_y = { 'progress' }, |
| 48 |
lualine_z = { 'location' }, |
| 49 |
}, |
| 50 |
inactive_sections = { |
| 51 |
lualine_a = {}, |
| 52 |
lualine_b = {}, |
| 53 |
lualine_c = { 'filename' }, |
| 54 |
lualine_x = { 'location' }, |
| 55 |
lualine_y = {}, |
| 56 |
lualine_z = {}, |
| 57 |
}, |
| 58 |
tabline = {}, |
| 59 |
winbar = {}, |
| 60 |
inactive_winbar = {}, |
| 61 |
extensions = {}, |
| 62 |
} |
| 63 |
|
| 64 |
--- Ensure sure user config doesn't disable lualine refresh completely |
| 65 |
---@param refresh_options table | nil |
| 66 |
---@return table | nil |
| 67 |
local function fix_refresh_timer(refresh_options) |
| 68 |
if refresh_options == nil then |
| 69 |
return |
| 70 |
end |
| 71 |
if refresh_options.refresh_time <= 0 then |
| 72 |
refresh_options.refresh_time = 16 |
| 73 |
end |
| 74 |
return refresh_options |
| 75 |
end |
| 76 |
|
| 77 |
--- change separator format 'x' to {left='x', right='x'} |
| 78 |
---@param separators string|table |
| 79 |
---@return table |
| 80 |
local function fix_separators(separators) |
| 81 |
if separators ~= nil then |
| 82 |
if type(separators) == 'string' then |
| 83 |
return { left = separators, right = separators } |
| 84 |
end |
| 85 |
end |
| 86 |
return separators |
| 87 |
end |
| 88 |
|
| 89 |
---copy raw disabled_filetypes to inner statusline & winbar tables. |
| 90 |
---@param disabled_filetypes table |
| 91 |
---@return table |
| 92 |
local function fix_disabled_filetypes(disabled_filetypes) |
| 93 |
if disabled_filetypes == nil then |
| 94 |
return |
| 95 |
end |
| 96 |
if disabled_filetypes.statusline == nil then |
| 97 |
disabled_filetypes.statusline = {} |
| 98 |
end |
| 99 |
if disabled_filetypes.winbar == nil then |
| 100 |
disabled_filetypes.winbar = {} |
| 101 |
end |
| 102 |
for k, disabled_ft in ipairs(disabled_filetypes) do |
| 103 |
table.insert(disabled_filetypes.statusline, disabled_ft) |
| 104 |
table.insert(disabled_filetypes.winbar, disabled_ft) |
| 105 |
disabled_filetypes[k] = nil |
| 106 |
end |
| 107 |
return disabled_filetypes |
| 108 |
end |
| 109 |
---extends config based on config_table |
| 110 |
---@param config_table table |
| 111 |
---@return table copy of config |
| 112 |
local function apply_configuration(config_table) |
| 113 |
if not config_table then |
| 114 |
return utils.deepcopy(config) |
| 115 |
end |
| 116 |
local function parse_sections(section_group_name) |
| 117 |
if config_table[section_group_name] == nil then |
| 118 |
return |
| 119 |
end |
| 120 |
if not next(config_table[section_group_name]) then |
| 121 |
config[section_group_name] = {} |
| 122 |
return |
| 123 |
end |
| 124 |
for section_name, section in pairs(config_table[section_group_name]) do |
| 125 |
if section_name == 'refresh' then |
| 126 |
config[section_group_name][section_name] = |
| 127 |
vim.tbl_deep_extend('force', config[section_group_name][section_name], utils.deepcopy(section)) |
| 128 |
else |
| 129 |
config[section_group_name][section_name] = utils.deepcopy(section) |
| 130 |
end |
| 131 |
end |
| 132 |
end |
| 133 |
if vim.fn.has('nvim-0.8') == 0 and (next(config_table.winbar or {}) or next(config_table.inactive_winbar or {})) then |
| 134 |
modules.utils_notices.add_notice('### winbar\nSorry `winbar can only be used in neovim 0.8 or higher.\n') |
| 135 |
config_table.winbar = {} |
| 136 |
config_table.inactive_winbar = {} |
| 137 |
end |
| 138 |
parse_sections('options') |
| 139 |
parse_sections('sections') |
| 140 |
parse_sections('inactive_sections') |
| 141 |
parse_sections('tabline') |
| 142 |
parse_sections('winbar') |
| 143 |
parse_sections('inactive_winbar') |
| 144 |
if config_table.extensions then |
| 145 |
config.extensions = utils.deepcopy(config_table.extensions) |
| 146 |
end |
| 147 |
config.options.refresh = fix_refresh_timer(config.options.refresh) |
| 148 |
config.options.section_separators = fix_separators(config.options.section_separators) |
| 149 |
config.options.component_separators = fix_separators(config.options.component_separators) |
| 150 |
config.options.disabled_filetypes = fix_disabled_filetypes(config.options.disabled_filetypes) |
| 151 |
return utils.deepcopy(config) |
| 152 |
end |
| 153 |
|
| 154 |
--- returns current active config |
| 155 |
---@return table a copy of config |
| 156 |
local function get_current_config() |
| 157 |
return utils.deepcopy(config) |
| 158 |
end |
| 159 |
|
| 160 |
return { |
| 161 |
get_config = get_current_config, |
| 162 |
apply_configuration = apply_configuration, |
| 163 |
} |