Merge pull request #132 from hoob3rt/setup

refactor: moving to setup function
This commit is contained in:
Hubert Pelczarski 2021-03-17 01:22:58 +01:00 committed by GitHub
commit 0a6c16a5d9
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 185 additions and 133 deletions

101
README.md
View File

@ -68,33 +68,34 @@ Each sections holds it's components e.g. current vim's mode.
Colorscheme of sections is mirrored, meaning section `A` will have the same colorscheme as section `Z` etc. Colorscheme of sections is mirrored, meaning section `A` will have the same colorscheme as section `Z` etc.
### Starting lualine ### Starting lualine
All configurations happens in the setup function
```lua ```lua
local lualine = require('lualine') require('lualine').setup{}
lualine.status()
``` ```
### Setting a theme ### Setting a theme
```lua ```lua
lualine.options.theme = 'gruvbox' options = {theme = 'gruvbox'}
``` ```
All available themes are listed in [THEMES.md](./THEMES.md) All available themes are listed in [THEMES.md](./THEMES.md)
Please create a pr if you managed to port a popular theme before me, [here is how to do it](./CONTRIBUTING.md). Please create a pr if you managed to port a popular theme before me, [here is how to do it](./CONTRIBUTING.md).
### Changing separator in section ### Changing separators
Lualine defines two kinds of seperators. One is for sections and other is for components. Default section seperators are '', '' and component separators are '', ''. Lualine defines two kinds of seperators. One is for sections and other is for components. Default section seperators are '', '' and component separators are '', ''.
They require powerline patched fonts. But you can easily change yours to something else like below They require powerline patched fonts. But you can easily change yours to something else like below
```lua ```lua
lualine.options.section_separators = {'', ''} options = {
lualine.options.component_separators = {'', ''} section_separators = {'', ''},
component_separators = {'', ''}
}
``` ```
or disable it or disable it
```lua ```lua
lualine.options.section_separators = nil options = {section_separators = nil, component_separators = nil}
lualine.options.component_separators = nil
``` ```
### Changing components in lualine sections ### Changing components in lualine sections
@ -103,22 +104,21 @@ lualine.options.component_separators = nil
<summary><b>Lualine defaults</b></summary> <summary><b>Lualine defaults</b></summary>
```lua ```lua
lualine.sections = { sections = {
lualine_a = { 'mode' }, lualine_a = {'mode'},
lualine_b = { 'branch' }, lualine_b = {'branch'},
lualine_c = { 'filename' }, lualine_c = {'filename'},
lualine_x = { 'encoding', 'fileformat', 'filetype' }, lualine_x = {'encoding', 'fileformat', 'filetype'},
lualine_y = { 'progress' }, lualine_y = {'progress'},
lualine_z = { 'location' }, lualine_z = {'location'}
} },
inactive_sections = {
lualine.inactive_sections = { lualine_a = {},
lualine_a = { }, lualine_b = {},
lualine_b = { }, lualine_c = {'filename'},
lualine_c = { 'filename' }, lualine_x = {'location'},
lualine_x = { 'location' }, lualine_y = {},
lualine_y = { }, lualine_z = {}
lualine_z = { }
} }
``` ```
@ -149,7 +149,7 @@ You can define a custom function as a lualine component
local function hello() local function hello()
return [[hello world]] return [[hello world]]
end end
lualine.sections.lualine_a = { hello } sections = {lualine_a = {'hello'}}
``` ```
</details> </details>
@ -161,7 +161,7 @@ lualine.sections.lualine_a = { hello }
You can use vim functions as a lualine component You can use vim functions as a lualine component
```lua ```lua
lualine.sections.lualine_b = { 'FugitiveHead' } sections = {lualine_a = {'FugitiveHead'}}
``` ```
</details> </details>
@ -174,7 +174,7 @@ Variables from g:, v:, t:, w:, b:, o, go:, vo:, to:, wo:, bo: scopes
can be used. Scopes ending with o are options usualy accessed with `&` in vimscript can be used. Scopes ending with o are options usualy accessed with `&` in vimscript
```lua ```lua
lualine.sections.lualine_b = { 'g:coc_status', 'bo:filetype' } sections = {lualine_a = {'g:coc_status', 'bo:filetype'}}
``` ```
</details> </details>
@ -201,7 +201,7 @@ lower | false | Changes components to be lowercase | all
format | nil | Takes a function . The funtion gets the result of component as argument and it's return value is displayed. So this function can parse and format the output as user wants. | all format | nil | Takes a function . The funtion gets the result of component as argument and it's return value is displayed. So this function can parse and format the output as user wants. | all
##### Global options example ##### Global options example
```lua ```lua
lualine.options.icons_enabled = true options = {icons_enabled = true}
``` ```
#### Component specific options #### Component specific options
@ -246,24 +246,23 @@ symbols | `{added = '+', modified = '~', removed = '-'}` | changes diff's symbol
##### Component options example ##### Component options example
```lua ```lua
lualine.sections.lualine_b = { sections = {
{ lualine_b = {
'branch', {'branch', icon = '', upper = true, color = {fg = '#00aa22'}}, {
icon = '',
upper = true,
color = { fg = '#00aa22' }
},
{
'filename', 'filename',
full_name = true, full_name = true,
shorten = true, shorten = true,
format = function(name) format = function(name)
-- Capitalize first charecter of filename to capital. -- Capitalize first charecter of filename to capital.
local path, fname = name:match('(.*/)(.*)') local path, fname = name:match('(.*/)(.*)')
if not path then path = ''; fname = name end if not path then
path = '';
fname = name
end
return path .. fname:sub(1, 1):upper() .. fname:sub(2, #fname) return path .. fname:sub(1, 1):upper() .. fname:sub(2, #fname)
end end
} }
}
} }
``` ```
@ -275,13 +274,13 @@ You can use lualine to display components in tabline.
The sections, configurations and highlights are same as statusline. The sections, configurations and highlights are same as statusline.
```lua ```lua
lualine.tabline = { tabline = {
lualine_a = { }, lualine_a = {},
lualine_b = { 'branch' }, lualine_b = {'branch'},
lualine_c = { 'filename' }, lualine_c = {'filename'},
lualine_x = { }, lualine_x = {},
lualine_y = { }, lualine_y = {},
lualine_z = { }, lualine_z = {}
} }
``` ```
This will show branch and filename component in top of neovim inside tabline . This will show branch and filename component in top of neovim inside tabline .
@ -290,11 +289,11 @@ This will show branch and filename component in top of neovim inside tabline .
You can also completely move your statuline to tabline by configuring lualine.tabline You can also completely move your statuline to tabline by configuring lualine.tabline
instead of lualine.sections & lualine.inactive_sections and setting them to empty instead of lualine.sections & lualine.inactive_sections and setting them to empty
```lua ```lua
lualine.tabline = { tabline = {
...... ......
} },
lualine.sections = {} sections = {},
lualine.inactive_sections = {} inactive_sections = {},
``` ```
</details> </details>
@ -303,7 +302,7 @@ Lualine extensions change statusline appearance for a window/buffer with a plugi
By default no plugin extension are loaded to improve performance. If you are using a plugin which is supported you can load it this way: By default no plugin extension are loaded to improve performance. If you are using a plugin which is supported you can load it this way:
```lua ```lua
lualine.extensions = { 'fzf' } extensions = { 'fzf' }
``` ```
<details> <details>
@ -325,7 +324,7 @@ lualine.extensions = { 'fzf' }
'hoob3rt/lualine.nvim', 'hoob3rt/lualine.nvim',
requires = {'kyazdani42/nvim-web-devicons', opt = true}, requires = {'kyazdani42/nvim-web-devicons', opt = true},
config = function() config = function()
require('lualine').status{ require('lualine').setup{
options = { options = {
theme = 'gruvbox', theme = 'gruvbox',
section_separators = {'', ''}, section_separators = {'', ''},
@ -387,6 +386,6 @@ let g:lualine = {
\}, \},
\'extensions' : [ 'fzf' ], \'extensions' : [ 'fzf' ],
\} \}
lua require("lualine").status() lua require("lualine").setup()
``` ```
</details> </details>

32
lua/lualine/config.lua Normal file
View File

@ -0,0 +1,32 @@
local M = {}
M.options = {
icons_enabled = true,
theme = 'gruvbox',
component_separators = {'', ''},
section_separators = {'', ''}
}
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 = {
lualine_a = {},
lualine_b = {},
lualine_c = {'filename'},
lualine_x = {'location'},
lualine_y = {},
lualine_z = {}
}
M.tabline = {}
M.extensions = {}
return M

View File

@ -3,44 +3,16 @@
local utils_component = require('lualine.utils.component') local utils_component = require('lualine.utils.component')
local utils = require('lualine.utils.utils') local utils = require('lualine.utils.utils')
local highlight = require('lualine.highlight') local highlight = require('lualine.highlight')
local config = require('lualine.config')
local M = {} local M = {}
M.options = {
icons_enabled = true,
theme = 'gruvbox',
component_separators = {'', ''},
section_separators = {'', ''}
}
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 = {
lualine_a = {},
lualine_b = {},
lualine_c = {'filename'},
lualine_x = {'location'},
lualine_y = {},
lualine_z = {}
}
M.tabline = {}
M.extensions = {}
local function apply_configuration(config_table) local function apply_configuration(config_table)
if not config_table then return end if not config_table then return end
local function parse_sections(section_group_name) local function parse_sections(section_group_name)
if not config_table[section_group_name] then return end if not config_table[section_group_name] then return end
for section_name, section in pairs(config_table[section_group_name]) do for section_name, section in pairs(config_table[section_group_name]) do
M[section_group_name][section_name] = config[section_group_name][section_name] =
config_table[section_group_name][section_name] config_table[section_group_name][section_name]
if type(section) == 'table' then if type(section) == 'table' then
for _, component in pairs(section) do for _, component in pairs(section) do
@ -57,29 +29,32 @@ local function apply_configuration(config_table)
parse_sections('sections') parse_sections('sections')
parse_sections('inactive_sections') parse_sections('inactive_sections')
parse_sections('tabline') parse_sections('tabline')
if config_table.extensions then M.extensions = config_table.extensions end if config_table.extensions then config.extensions = config_table.extensions end
end end
local function check_single_separator() local function check_single_separator()
local compoennt_separator = M.options.component_separators local compoennt_separator = config.options.component_separators
local section_separator = M.options.section_separators local section_separator = config.options.section_separators
if M.options.component_separators ~= nil then if config.options.component_separators ~= nil then
if type(M.options.component_separators) == 'string' then if type(config.options.component_separators) == 'string' then
M.options.component_separators = { config.options.component_separators =
compoennt_separator, compoennt_separator {compoennt_separator, compoennt_separator}
} elseif #config.options.component_separators == 1 then
elseif #M.options.component_separators == 1 then config.options.component_separators =
M.options.component_separators = { {
M.options.component_separators[1], M.options.component_separators[1] config.options.component_separators[1],
config.options.component_separators[1]
} }
end end
end end
if M.options.section_separators ~= nil then if config.options.section_separators ~= nil then
if type(M.options.section_separators) == 'string' then if type(config.options.section_separators) == 'string' then
M.options.section_separators = {section_separator, section_separator} config.options.section_separators = {section_separator, section_separator}
elseif #M.options.section_separators == 1 then elseif #config.options.section_separators == 1 then
M.options.section_separators = { config.options.section_separators =
M.options.section_separators[1], M.options.section_separators[1] {
config.options.section_separators[1],
config.options.section_separators[1]
} }
end end
end end
@ -132,7 +107,7 @@ local function component_loader(component)
-- With component function -- With component function
component.component_name = component[1] component.component_name = component[1]
-- apply default args -- apply default args
for opt_name, opt_val in pairs(M.options) do for opt_name, opt_val in pairs(config.options) do
if component[opt_name] == nil then component[opt_name] = opt_val end if component[opt_name] == nil then component[opt_name] = opt_val end
end end
-- load the component -- load the component
@ -173,39 +148,39 @@ local function load_sections(sections)
end end
local function load_components() local function load_components()
load_sections(M.sections) load_sections(config.sections)
load_sections(M.inactive_sections) load_sections(config.inactive_sections)
load_sections(M.tabline) load_sections(config.tabline)
end end
local function load_extensions() local function load_extensions()
for index, extension in pairs(M.extensions) do for index, extension in pairs(config.extensions) do
local local_extension = require('lualine.extensions.' .. extension) local local_extension = require('lualine.extensions.' .. extension)
load_sections(local_extension.sections) load_sections(local_extension.sections)
load_sections(local_extension.inactive_sections) load_sections(local_extension.inactive_sections)
M.extensions[index] = local_extension config.extensions[index] = local_extension
end end
end end
local function lualine_set_theme() local function lualine_set_theme()
if type(M.options.theme) == 'string' then if type(config.options.theme) == 'string' then
M.options.theme = require('lualine.themes.' .. M.options.theme) config.options.theme = require('lualine.themes.' .. config.options.theme)
-- change the theme table in component so their custom -- change the theme table in component so their custom
-- highlights can reflect theme change -- highlights can reflect theme change
local function reset_component_theme(sections) local function reset_component_theme(sections)
for _, section in pairs(sections) do for _, section in pairs(sections) do
for _, component in pairs(section) do for _, component in pairs(section) do
if type(component) == 'table' then if type(component) == 'table' then
component.theme = M.options.theme component.theme = config.options.theme
end end
end end
end end
end end
reset_component_theme(M.sections) reset_component_theme(config.sections)
reset_component_theme(M.inactive_sections) reset_component_theme(config.inactive_sections)
end end
utils.clear_highlights() utils.clear_highlights()
highlight.create_highlight_groups(M.options.theme) highlight.create_highlight_groups(config.options.theme)
end end
local function statusline(sections, is_focused) local function statusline(sections, is_focused)
@ -255,10 +230,10 @@ local function statusline(sections, is_focused)
local transitional_highlight = highlight.get_transitional_highlights( local transitional_highlight = highlight.get_transitional_highlights(
previous_section.data, previous_section.data,
current_section.data, true) current_section.data, true)
if transitional_highlight and M.options.section_separators and if transitional_highlight and config.options.section_separators and
M.options.section_separators[2] then config.options.section_separators[2] then
table.insert(status, transitional_highlight .. table.insert(status, transitional_highlight ..
M.options.section_separators[2]) config.options.section_separators[2])
end end
end end
@ -270,10 +245,10 @@ local function statusline(sections, is_focused)
local transitional_highlight = highlight.get_transitional_highlights( local transitional_highlight = highlight.get_transitional_highlights(
current_section.data, current_section.data,
next_section.data) next_section.data)
if transitional_highlight and M.options.section_separators and if transitional_highlight and config.options.section_separators and
M.options.section_separators[1] then config.options.section_separators[1] then
table.insert(status, transitional_highlight .. table.insert(status, transitional_highlight ..
M.options.section_separators[1]) config.options.section_separators[1])
end end
end end
else -- when not in focus else -- when not in focus
@ -291,7 +266,7 @@ end
-- check if any extension matches the filetype and return proper sections -- check if any extension matches the filetype and return proper sections
local function get_extension_sections() local function get_extension_sections()
local sections, inactive_sections = nil, nil local sections, inactive_sections = nil, nil
for _, extension in ipairs(M.extensions) do for _, extension in ipairs(config.extensions) do
for _, filetype in ipairs(extension.filetypes) do for _, filetype in ipairs(extension.filetypes) do
if vim.bo.filetype == filetype then if vim.bo.filetype == filetype then
sections = extension.sections sections = extension.sections
@ -307,16 +282,18 @@ local function status_dispatch()
local extension_sections = get_extension_sections() local extension_sections = get_extension_sections()
if vim.g.statusline_winid == vim.fn.win_getid() then if vim.g.statusline_winid == vim.fn.win_getid() then
local sections = extension_sections.sections local sections = extension_sections.sections
if sections == nil then sections = M.sections end if sections == nil then sections = config.sections end
return statusline(sections, true) return statusline(sections, true)
else else
local inactive_sections = extension_sections.inactive_sections local inactive_sections = extension_sections.inactive_sections
if inactive_sections == nil then inactive_sections = M.inactive_sections end if inactive_sections == nil then
inactive_sections = config.inactive_sections
end
return statusline(inactive_sections, false) return statusline(inactive_sections, false)
end end
end end
local function tabline() return statusline(M.tabline, true) end local function tabline() return statusline(config.tabline, true) end
local function setup_theme() local function setup_theme()
lualine_set_theme() lualine_set_theme()
@ -330,7 +307,7 @@ local function setup_theme()
end end
local function set_tabline() local function set_tabline()
if next(M.tabline) ~= nil then if next(config.tabline) ~= nil then
_G.lualine_tabline = tabline _G.lualine_tabline = tabline
vim.o.tabline = '%!v:lua.lualine_tabline()' vim.o.tabline = '%!v:lua.lualine_tabline()'
vim.o.showtabline = 2 vim.o.showtabline = 2
@ -338,7 +315,7 @@ local function set_tabline()
end end
local function set_statusline() local function set_statusline()
if next(M.sections) ~= nil or next(M.inactive_sections) ~= nil then if next(config.sections) ~= nil or next(config.inactive_sections) ~= nil then
_G.lualine_statusline = status_dispatch _G.lualine_statusline = status_dispatch
vim.o.statusline = '%!v:lua.lualine_statusline()' vim.o.statusline = '%!v:lua.lualine_statusline()'
vim.api.nvim_exec([[ vim.api.nvim_exec([[
@ -348,9 +325,38 @@ local function set_statusline()
end end
end end
function M.status(config) function M.setup(user_config)
if M.options ~= nil or M.sections ~= nil or M.inactive_sections ~= nil or
M.tabline ~= nil or M.extensions ~= nil then
if vim.api.nvim_echo then
vim.api.nvim_echo({
{
'lualine.nvim: lualine is moving to setup function to be consistent with other lua plugins, all other configuration options will be removed by 24.03.2021, please change your configuration(example in readme)',
'WarningMsg'
}
}, true, {})
else
print(
'lualine.nvim: lualine is moving to setup function to be consistent with other lua plugins, all other configuration options will be removed by 24.03.2021, please change your configuration(example in readme)')
end
if M.options ~=nil then
user_config.options = M.options
end
if M.sections ~=nil then
user_config.sections = M.sections
end
if M.inactive_sections ~=nil then
user_config.inactive_sections = M.inactive_sections
end
if M.tabline ~=nil then
user_config.tabline = M.tabline
end
if M.extensions ~=nil then
user_config.extensions = M.extensions
end
end
apply_configuration(vim.g.lualine) apply_configuration(vim.g.lualine)
apply_configuration(config) apply_configuration(user_config)
check_single_separator() check_single_separator()
setup_theme() setup_theme()
load_components() load_components()
@ -359,4 +365,19 @@ function M.status(config)
set_tabline() set_tabline()
end end
function M.status(user_config)
if vim.api.nvim_echo then
vim.api.nvim_echo({
{
'lualine.nvim: status function has been ranamed to setup and will be removed by 24.03.2021, please change your configuration',
'WarningMsg'
}
}, true, {})
else
print(
'lualine.nvim: status function has been ranamed to setup and will be removed by 24.03.2021, please change your configuration')
end
return M.setup(user_config)
end
return M return M