Merge branch 'master' into extensions

This commit is contained in:
hoob3rt 2021-03-11 00:13:18 +01:00
commit a4690ca4cf
7 changed files with 417 additions and 169 deletions

164
README.md
View File

@ -58,6 +58,9 @@ use {
``` ```
## Usage and customization ## Usage and customization
Lualine can be configured with both lua and vimscript.
Click [here](#lua-config-example) if you want to see a config example in lua and [here](#vimscript-config-example) if you want to see a config example in vimscript.
Lualine has sections as shown below. Lualine has sections as shown below.
``` ```
@ -69,8 +72,6 @@ Lualine has sections as shown below.
Each sections holds it's components e.g. current vim's mode. 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.
Configuration is currently limited to lua, please use lua block or a separate lua file to configure lualine.
### Starting lualine ### Starting lualine
```lua ```lua
local lualine = require('lualine') local lualine = require('lualine')
@ -131,17 +132,16 @@ lualine.inactive_sections = {
<details> <details>
<summary><b>Available components</b></summary> <summary><b>Available components</b></summary>
* general * branch (git branch)
* branch (git branch) * diagnostics (diagnostics count from your prefered source)
* diagnostics (diagnostics count from your prefered source) * encoding (file encoding)
* encoding (file encoding) * fileformat (file format)
* fileformat (file format) * filename
* filename * filetype
* filetype * location (location in file in line:column format)
* location (location in file in line:column format) * mode (vim mode)
* mode (vim mode) * progress (%progress in file)
* progress (%progress in file) * diff (git diff status)
* diff (git diff status)
</details> </details>
@ -165,7 +165,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
lualine.sections.lualine_b = { 'FugitiveHead' } lualine.sections.lualine_b = { 'FugitiveHead' }
``` ```
@ -178,7 +178,7 @@ You can use variables from vim and lua globals as a lualine component
Variables from g:, v:, t:, w:, b:, o, go:, vo:, to:, wo:, bo: scopes 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
lualine.sections.lualine_b = { 'g:coc_status', 'bo:filetype' } lualine.sections.lualine_b = { 'g:coc_status', 'bo:filetype' }
``` ```
@ -265,7 +265,7 @@ lualine.sections.lualine_b = {
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
} }
@ -274,6 +274,33 @@ lualine.sections.lualine_b = {
</details> </details>
### Using tabline as statusline
You can use lualine to display components in tabline .
The sections, configurations and highlights are same as statusline.
```
lualine.tabline = {
lualine_a = { },
lualine_b = { 'branch' },
lualine_c = { 'filename' },
lualine_x = { },
lualine_y = { },
lualine_z = { },
}
```
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
instead of lualine.sections & lualine.inactive_sections and setting them to empty
```
lualine.tabline = {
......
}
lualine.sections = {}
lualine.inactive_sections = {}
```
### Loading plugin extensions ### Loading plugin extensions
Lualine extensions change statusline appearance for a window/buffer with a plugin loaded e.g. [junegunn/fzf.vim](https://github.com/junegunn/fzf.vim) Lualine extensions change statusline appearance for a window/buffer with a plugin loaded e.g. [junegunn/fzf.vim](https://github.com/junegunn/fzf.vim)
@ -284,7 +311,7 @@ lualine.extensions = { 'fzf' }
All available extensions are listed in [EXTENSIONS.md](./EXTENSIONS.md) All available extensions are listed in [EXTENSIONS.md](./EXTENSIONS.md)
### Full config example using [packer.nvim](https://github.com/wbthomason/packer.nvim) ### Lua config example
<details> <details>
<summary><b>packer config</b></summary> <summary><b>packer config</b></summary>
@ -294,69 +321,68 @@ All available extensions are listed in [EXTENSIONS.md](./EXTENSIONS.md)
'hoob3rt/lualine.nvim', 'hoob3rt/lualine.nvim',
requires = {'kyazdani42/nvim-web-devicons', opt = true}, requires = {'kyazdani42/nvim-web-devicons', opt = true},
config = function() config = function()
local lualine = require('lualine') require('lualine').status{
lualine.options = { options = {
theme = 'gruvbox', theme = 'gruvbox',
section_separators = {'', ''}, section_separators = {'', ''},
component_separators = {'', ''}, component_separators = {'', ''},
icons_enabled = true, icons_enabled = true,
},
sections = {
lualine_a = { {'mode', upper = true} },
lualine_b = { {'branch', icon = ''} },
lualine_c = { {'filename', file_status = true} },
lualine_x = { 'encoding', 'fileformat', 'filetype' },
lualine_y = { 'progress' },
lualine_z = { 'location' },
},
inactive_sections = {
lualine_a = { },
lualine_b = { },
lualine_c = { 'filename' },
lualine_x = { 'location' },
lualine_y = { },
lualine_z = { }
},
extensions = { 'fzf' }
} }
lualine.sections = {
lualine_a = { 'mode' },
lualine_b = { 'branch' },
lualine_c = { 'filename' },
lualine_x = { 'encoding', 'fileformat', 'filetype' },
lualine_y = { 'progress' },
lualine_z = { 'location' },
}
lualine.inactive_sections = {
lualine_a = { },
lualine_b = { },
lualine_c = { 'filename' },
lualine_x = { 'location' },
lualine_y = { },
lualine_z = { }
}
lualine.extensions = { 'fzf' }
lualine.status()
end end
} }
``` ```
</details> </details>
### Full config example inside `.vimrc`/`init.vim` ### Vimscript config example
<details> <details>
<summary><b>vimrc config</b></summary> <summary><b>vimrc config</b></summary>
```vim ```vim
lua << EOF let g:lualine = {
local lualine = require('lualine') \'options' : {
lualine.options = { \ 'theme' : 'gruvbox',
theme = 'gruvbox', \ 'section_separators' : ['', ''],
section_separators = {'', ''}, \ 'component_separators' : ['', ''],
component_separators = {'', ''}, \ 'icons_enabled' : v:true,
icons_enabled = true, \},
} \'sections' : {
lualine.sections = { \ 'lualine_a' : [ ['mode', {'upper': v:true,},], ],
lualine_a = { 'mode' }, \ 'lualine_b' : [ ['branch', {'icon': '',}, ], ],
lualine_b = { 'branch' }, \ 'lualine_c' : [ ['filename', {'file_status': v:true,},], ],
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 = { } \},
} \'extensions' : [ 'fzf' ],
lualine.extensions = { 'fzf' } \}
lualine.status() lua require("lualine").status()
EOF
``` ```
</details> </details>

View File

@ -2,6 +2,9 @@
All available themes are only best effort ports by myself/ other users. If you find a theme to be weird/ wrong please open an issue/ pr. All available themes are only best effort ports by myself/ other users. If you find a theme to be weird/ wrong please open an issue/ pr.
### auto
if your favourite colorcheme is not yet supported you can use `auto` to make
lualine try to match your colorscheme.
### 16color ### 16color
![16normal_cropped](https://user-images.githubusercontent.com/41551030/108648240-02d2ae00-74bb-11eb-9ac1-495849621366.png) ![16normal_cropped](https://user-images.githubusercontent.com/41551030/108648240-02d2ae00-74bb-11eb-9ac1-495849621366.png)
![16insert_cropped](https://user-images.githubusercontent.com/41551030/108648219-f77f8280-74ba-11eb-84e4-978bf918c21f.png) ![16insert_cropped](https://user-images.githubusercontent.com/41551030/108648219-f77f8280-74ba-11eb-84e4-978bf918c21f.png)

View File

@ -1,5 +1,5 @@
*lualine.txt* A blazing fast and easy to configure statusline *lualine.txt* A blazing fast and easy to configure statusline
*lualine_nvim* *lualine* *lualine_nvim* *lualine*
Author: hoob3rt (https://github.com/hoob3rt) Author: hoob3rt (https://github.com/hoob3rt)
License: MIT License License: MIT License
@ -37,15 +37,16 @@ CONTENTS *lualine_contents*
1.3.4. Changing components.................|lualine_changing_components| 1.3.4. Changing components.................|lualine_changing_components|
1.3.5. Building Custom components............|lualine_custom_components| 1.3.5. Building Custom components............|lualine_custom_components|
1.3.6. Custom options...........................|lualine_custom_options| 1.3.6. Custom options...........................|lualine_custom_options|
1.3.7. Loading plugin extensions.....|lualine_loading_plugin_extensions| 1.3.7. Using tabline as statusline...............|lualine_using_tabline|
1.3.8 Config examples.........................|lualine_config_examples| 1.3.8. Loading plugin extensions.....|lualine_loading_plugin_extensions|
1.3.8.1. Packer.nvim......|lualine_config_example_using_packer.nvim| 1.3.9 Config examples.........................|lualine_config_examples|
1.3.8.2 VIML example.......|lualine_full_config_example_inside_viml| 1.3.9.1. Packer.nvim......|lualine_config_example_using_packer.nvim|
1.3.9.2 VIML example.......|lualine_full_config_example_inside_viml|
1.4. Contributing.....................................|lualine_contributing| 1.4. Contributing.....................................|lualine_contributing|
1.5. Screenshots.......................................|lualine_screenshots| 1.5. Screenshots.......................................|lualine_screenshots|
================================================================================ ================================================================================
PERFORMANCE COMPARISM *lualine_performance_comparism* PERFORMANCE COMPARISM *lualine_performance_comparism*
Unlike other statusline plugins lualine loads only defined components, nothing else. Unlike other statusline plugins lualine loads only defined components, nothing else.
@ -62,7 +63,7 @@ installed
-------------------------------------------------------------------------------- --------------------------------------------------------------------------------
INSTALLATION *lualine_installation* INSTALLATION *lualine_installation*
VIM-PLUG *lualine_vim-plug* VIM-PLUG *lualine_vim-plug*
> >
Plug 'hoob3rt/lualine.nvim' Plug 'hoob3rt/lualine.nvim'
" If you want to have icons in your statusline choose one of these " If you want to have icons in your statusline choose one of these
@ -70,7 +71,7 @@ VIM-PLUG *lualine_vim-plug*
Plug 'ryanoasis/vim-devicons' Plug 'ryanoasis/vim-devicons'
< <
PACKER.NVIM *lualine_packer.nvim* PACKER.NVIM *lualine_packer.nvim*
> >
use { use {
'hoob3rt/lualine.nvim', 'hoob3rt/lualine.nvim',
@ -111,7 +112,7 @@ 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 Please create a pr if you managed to port a popular theme before me, here is
how to do it (./CONTRIBUTING.md). how to do it (./CONTRIBUTING.md).
CHANGING SEPARATOR IN SECTION *lualine_changing_separator* CHANGING SEPARATOR IN SECTION *lualine_changing_separator*
Lualine defines two kinds of seperators. One is for sections and other is Lualine defines two kinds of seperators. One is for sections and other is
for components. Default section seperators are '', '' and component for components. Default section seperators are '', '' and component
@ -157,10 +158,10 @@ Lualine defaults~
Available components~ Available components~
general~ general~
* branch (git branch) * branch (git branch)
* diagnostics (diagnostics count from your prefered source) * diagnostics (diagnostics count from your prefered source)
* encoding (file encoding) * encoding (file encoding)
* fileformat (file format) * fileformat (file format)
* filename * filename
* filetype * filetype
* location (location in file in line:column format) * location (location in file in line:column format)
@ -170,7 +171,7 @@ Available components~
-------------------------------------------------------------------------------- --------------------------------------------------------------------------------
BUILDING YOUR COMPONENTS *lualine_custom_components* BUILDING YOUR COMPONENTS *lualine_custom_components*
Using custom functions as lualine component~ Using custom functions as lualine component~
@ -301,13 +302,13 @@ List of options are given below.
changes diagnostic's symbol characters. You can set one or more symbols changes diagnostic's symbol characters. You can set one or more symbols
for each level. for each level.
> >
{ {
'diagnostics', 'diagnostics',
symbols = { symbols = {
-- set the error symbol and use defaults for warn and info. -- set the error symbol and use defaults for warn and info.
error = '!!', error = '!!',
}, },
} }
< <
• filename~ • filename~
@ -346,13 +347,13 @@ List of options are given below.
Changes diff's symbol characters. You can set some symbols partly. Changes diff's symbol characters. You can set some symbols partly.
Color in #rrggbb format Color in #rrggbb format
> >
{ {
'diff', 'diff',
-- set the added symbol and use defaults for modified and removed. -- set the added symbol and use defaults for modified and removed.
symbols = { symbols = {
added = 'A', added = 'A',
}, },
} }
< <
Example:~ Example:~
@ -371,12 +372,39 @@ Example:~
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
} }
} }
-------------------------------------------------------------------------------
USING TABLINE AS STATUSLINE *lualine_using_tabline*
You can use lualine to display components in tabline .
The sections, configurations and highlights are same as statusline.
>
lualine.tabline = {
lualine_a = { },
lualine_b = { 'branch' },
lualine_c = { 'filename' },
lualine_x = { },
lualine_y = { },
lualine_z = { },
}
< <
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
instead of lualine.sections & lualine.inactive_sections and setting them to empty
>
lualine.tabline = {
......
}
lualine.sections = {}
lualine.inactive_sections = {}
<
-------------------------------------------------------------------------------- --------------------------------------------------------------------------------
LOADING PLUGIN EXTENSIONS *lualine_loading_plugin_extensions* LOADING PLUGIN EXTENSIONS *lualine_loading_plugin_extensions*
@ -393,7 +421,7 @@ using a plugin which is supported you can load it this way:
All available extensions are listed in EXTENSIONS.md (./EXTENSIONS.md) All available extensions are listed in EXTENSIONS.md (./EXTENSIONS.md)
-------------------------------------------------------------------------------- --------------------------------------------------------------------------------
CONGIG EXAMPLES *lualine_config_examples* CONGIG EXAMPLES *lualine_config_examples*
FULL CONFIG EXAMPLE USING PACKER.NVIM *lualine_config_example_using_packer.nvim* FULL CONFIG EXAMPLE USING PACKER.NVIM *lualine_config_example_using_packer.nvim*
@ -404,31 +432,31 @@ packer config
'hoob3rt/lualine.nvim', 'hoob3rt/lualine.nvim',
requires = {'kyazdani42/nvim-web-devicons', opt = true}, requires = {'kyazdani42/nvim-web-devicons', opt = true},
config = function() config = function()
local lualine = require('lualine') require('lualine').status{
lualine.options = { options = {
theme = 'gruvbox', theme = 'gruvbox',
section_separators = {'', ''}, section_separators = {'', ''},
component_separators = {'', ''}, component_separators = {'', ''},
icons_enabled = true, icons_enabled = true,
} },
lualine.sections = { sections = {
lualine_a = { 'mode' }, lualine_a = { {'mode', upper = true} },
lualine_b = { 'branch' }, lualine_b = { {'branch', icon = ''} },
lualine_c = { 'filename' }, lualine_c = { {'filename', file_status = true} },
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_a = { },
lualine_b = { },
lualine_c = { 'filename' },
lualine_x = { 'location' },
lualine_y = { },
lualine_z = { }
},
extensions = { 'fzf' }
} }
lualine.inactive_sections = {
lualine_a = { },
lualine_b = { },
lualine_c = { 'filename' },
lualine_x = { 'location' },
lualine_y = { },
lualine_z = { }
}
lualine.extensions = { 'fzf' }
lualine.status()
end end
} }
< <
@ -440,33 +468,32 @@ FULL CONFIG EXAMPLE INSIDE VIML *lualine_full_config_example_inside_viml*
vimrc config vimrc config
> >
lua << EOF let g:lualine = {
local lualine = require('lualine') \'options' : {
lualine.options = { \ 'theme' : 'gruvbox',
theme = 'gruvbox', \ 'section_separators' : ['', ''],
section_separators = {'', ''}, \ 'component_separators' : ['', ''],
component_separators = {'', ''}, \ 'icons_enabled' : v:true,
icons_enabled = true, \},
} \'sections' : {
lualine.sections = { \ 'lualine_a' : [ ['mode', {'upper': v:true,},], ],
lualine_a = { 'mode' }, \ 'lualine_b' : [ ['branch', {'icon': '',}, ], ],
lualine_b = { 'branch' }, \ 'lualine_c' : [ ['filename', {'file_status': v:true,},], ],
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 = { } \},
} \'extensions' : [ 'fzf' ],
lualine.extensions = { 'fzf' } \}
lualine.status() lua require("lualine").status()
EOF
< <
-------------------------------------------------------------------------------- --------------------------------------------------------------------------------

View File

@ -19,4 +19,5 @@ lualine_screenshots lualine.txt /*lualine_screenshots*
lualine_setting_theme lualine.txt /*lualine_setting_theme* lualine_setting_theme lualine.txt /*lualine_setting_theme*
lualine_starting_lualine lualine.txt /*lualine_starting_lualine* lualine_starting_lualine lualine.txt /*lualine_starting_lualine*
lualine_usage_and_customization lualine.txt /*lualine_usage_and_customization* lualine_usage_and_customization lualine.txt /*lualine_usage_and_customization*
lualine_using_tabline lualine.txt /*lualine_using_tabline*
lualine_vim-plug lualine.txt /*lualine_vim-plug* lualine_vim-plug lualine.txt /*lualine_vim-plug*

View File

@ -35,6 +35,8 @@ M.inactive_sections = {
lualine_z = { } lualine_z = { }
} }
M.tabline = {}
M.extensions = {} M.extensions = {}
local function apply_configuration(config_table) local function apply_configuration(config_table)
@ -59,6 +61,7 @@ local function apply_configuration(config_table)
parse_sections('options') parse_sections('options')
parse_sections('sections') parse_sections('sections')
parse_sections('inactive_sections') parse_sections('inactive_sections')
parse_sections('tabline')
if config_table.extensions then M.extensions = config_table.extensions end if config_table.extensions then M.extensions = config_table.extensions end
end end
@ -167,6 +170,7 @@ end
local function load_components() local function load_components()
load_sections(M.sections) load_sections(M.sections)
load_sections(M.inactive_sections) load_sections(M.inactive_sections)
load_sections(M.tabline)
end end
local function load_extensions() local function load_extensions()
@ -297,25 +301,49 @@ local function status_dispatch()
end end
end end
local function exec_autocommands() local function tabline()
return statusline(M.tabline, true)
end
local function setup_theme()
lualine_set_theme()
_G.lualine_set_theme = lualine_set_theme _G.lualine_set_theme = lualine_set_theme
_G.lualine_statusline = status_dispatch
vim.api.nvim_exec([[ vim.api.nvim_exec([[
augroup lualine augroup lualine
autocmd! autocmd!
autocmd ColorScheme * call v:lua.lualine_set_theme() autocmd ColorScheme * call v:lua.lualine_set_theme()
autocmd WinLeave,BufLeave * lua vim.wo.statusline=lualine_statusline()
autocmd WinEnter,BufEnter * setlocal statusline=%!v:lua.lualine_statusline()
augroup END augroup END
]], false) ]], false)
end end
function M.status() local function set_tabline()
if next(M.tabline) ~= nil then
_G.lualine_tabline = tabline
vim.o.tabline = '%!v:lua.lualine_tabline()'
vim.o.showtabline = 2
end
end
local function set_statusline()
if next(M.sections) ~= nil or next(M.inactive_sections) ~= nil then
_G.lualine_statusline = status_dispatch
vim.o.statusline = '%!v:lua.lualine_statusline()'
vim.api.nvim_exec([[
autocmd lualine WinLeave,BufLeave * lua vim.wo.statusline=lualine_statusline()
autocmd lualine WinEnter,BufEnter * setlocal statusline=%!v:lua.lualine_statusline()
]], false)
end
end
function M.status(config)
apply_configuration(vim.g.lualine)
apply_configuration(config)
check_single_separator() check_single_separator()
lualine_set_theme() setup_theme()
exec_autocommands()
load_components() load_components()
load_extensions() load_extensions()
set_statusline()
set_tabline()
end end
return M return M

View File

@ -142,7 +142,7 @@ end
-- @param left_section_data :(string) section before separator -- @param left_section_data :(string) section before separator
-- @param right_section_data:(string) section after separator -- @param right_section_data:(string) section after separator
-- @param reverse :(string) Whether it's a left separator or right separator -- @param reverse :(string) Whether it's a left separator or right separator
-- '▶️' and '◀️' needs reverse colors so this parameter needs to be set true. -- '▶️' and '◀️' needs reverse colors so this parameter needs to be set true.
-- @return: (string) formated highlight group name -- @return: (string) formated highlight group name
function M.get_transitional_highlights(left_section_data, right_section_data, reverse ) function M.get_transitional_highlights(left_section_data, right_section_data, reverse )
local left_highlight_name, right_highlight_name local left_highlight_name, right_highlight_name
@ -168,16 +168,16 @@ function M.get_transitional_highlights(left_section_data, right_section_data, re
right_highlight_name = 'lualine_c_normal' right_highlight_name = 'lualine_c_normal'
end end
end end
-- When both left and right highlights are same nothing to transition to -- When both left and right highlights are same nothing to transition to
if left_highlight_name == right_highlight_name then return end if left_highlight_name == right_highlight_name then return end
-- construct the name of hightlight group -- construct the name of hightlight group
local highlight_name local highlight_name
if left_highlight_name:find('lualine_') == 1 then if left_highlight_name:find('lualine_') == 1 then
highlight_name = left_highlight_name .. '_to_' .. right_highlight_name highlight_name = left_highlight_name .. '_to_' .. right_highlight_name
else else
highlight_name = 'lualine_' .. left_highlight_name .. '_to_' .. right_highlight_name highlight_name = 'lualine_' .. left_highlight_name .. '_to_' .. right_highlight_name
end end
if not utils.highlight_exists(highlight_name) then if not utils.highlight_exists(highlight_name) then
-- Create the highlight_group if needed -- Create the highlight_group if needed
@ -188,12 +188,12 @@ function M.get_transitional_highlights(left_section_data, right_section_data, re
local bg = utils.extract_highlight_colors(right_highlight_name, 'guibg') local bg = utils.extract_highlight_colors(right_highlight_name, 'guibg')
if not fg then fg = 'none' end if not fg then fg = 'none' end
if not bg then bg = 'none' end if not bg then bg = 'none' end
-- swap the bg and fg when reverse is true. As in that case highlight will -- swap the bg and fg when reverse is true. As in that case highlight will
-- be placed before section -- be placed before section
if reverse then fg, bg = bg, fg end if reverse then fg, bg = bg, fg end
highlight(highlight_name, fg, bg) highlight(highlight_name, fg, bg)
end end
-- Create highlights and setup to survive colorscheme changes -- Create highlights and setup to survive colorscheme changes
set_transitional_highlights() set_transitional_highlights()
utils.expand_set_theme(set_transitional_highlights) utils.expand_set_theme(set_transitional_highlights)
end end

163
lua/lualine/themes/auto.lua Normal file
View File

@ -0,0 +1,163 @@
local utils = require'lualine.utils.utils'
---------------
-- Constents --
---------------
-- fg and bg must have this much contrast range 0 < contrast_threshold < 0.5
local contrast_threshold = 0.3
-- how much brightness is changed in percentage for light and dark themes
local brightness_modifier_parameter = 10
-- retrives color value from highlight group name in syntax_list
-- first present highlight is returned
local function getHi( scope, syntaxlist )
for _ , highlight_name in pairs( syntaxlist ) do
if vim.fn.hlexists(highlight_name) ~= 0 then
local color = utils.extract_highlight_colors(highlight_name)
if color.reverse then
if scope == 'guibg' then scope = 'guifg' else scope = 'guibg' end
end
if color[scope] then return color[scope] end
end
end
return '#000000'
end
-- truns #rrggbb -> { red, green, blue }
local function rgb_str2num(rgb_color_str)
if rgb_color_str:find('#') == 1 then rgb_color_str = rgb_color_str:sub(2, #rgb_color_str) end
local red = tonumber(rgb_color_str:sub(1,2), 16)
local green = tonumber(rgb_color_str:sub(3,4), 16)
local blue = tonumber(rgb_color_str:sub(5,6), 16)
return { red = red, green = green, blue = blue, }
end
-- turns { red, green, blue } -> #rrggbb
local function rgb_num2str(rgb_color_num)
local rgb_color_str = string.format('#%02x%02x%02x', rgb_color_num.red,
rgb_color_num.green, rgb_color_num.blue)
return rgb_color_str
end
-- returns brightness lavel of color in range 0 to 1
-- arbitary value it's basicaly an weighted average
local function get_color_brightness(rgb_color)
local color = rgb_str2num(rgb_color)
local brightness = (color.red * 2 + color.green * 3 + color.blue) / 6
return brightness / 256
end
-- returns average of colors in range 0 to 1
-- used to ditermine contrast lavel
local function get_color_avg(rgb_color)
local color = rgb_str2num(rgb_color)
return (color.red + color.green + color.blue) / 3 / 256
end
-- clamps the val between left and right
local function clamp(val, left, right)
if val > right then return right end
if val < left then return left end
return val
end
-- changes braghtness of rgb_color by percentage
local function brightness_modifier(rgb_color, parcentage)
local color = rgb_str2num(rgb_color)
color.red = clamp(color.red + (color.red * parcentage / 100), 0, 255)
color.green = clamp(color.green + (color.green * parcentage / 100), 0, 255)
color.blue = clamp(color.blue + (color.blue * parcentage / 100), 0, 255)
return rgb_num2str(color)
end
-- changes contrast of rgb_color by amount
local function contrast_modifier(rgb_color, amount)
local color = rgb_str2num(rgb_color)
color.red = clamp(color.red + amount, 0, 255)
color.green = clamp(color.green + amount, 0, 255)
color.blue = clamp(color.blue + amount, 0, 255)
return rgb_num2str(color)
end
-- Changes brightness of foreground color to achive contrast
-- without changing the color
local function apply_contrast(highlight)
local hightlight_bg_avg = get_color_avg(highlight.bg)
local contrast_threshold_config = clamp(contrast_threshold, 0, 0.5)
local contranst_change_step = 5
if hightlight_bg_avg > .5 then
contranst_change_step = -contranst_change_step
end
-- donn't waste too much time here max 25 interation should be more than enough
local iteration_count = 1
while (math.abs(get_color_avg(highlight.fg) - hightlight_bg_avg) < contrast_threshold_config and iteration_count < 25) do
highlight.fg = contrast_modifier(highlight.fg, contranst_change_step)
iteration_count = iteration_count + 1
end
end
-- Get the colors to create theme
local colors = {
normal = getHi( 'guibg', {'PmenuSel', 'PmenuThumb', 'TabLineSel' } ),
insert = getHi( 'guifg', {'String', 'MoreMsg' } ),
replace = getHi( 'guifg', {'Number', 'Type' } ),
visual = getHi( 'guifg', {'Special', 'Boolean', 'Constant' } ),
command = getHi( 'guifg', {'Identifier' } ),
back1 = getHi( 'guibg', {'Normal', 'StatusLineNC' } ),
fore = getHi( 'guifg', {'Normal', 'StatusLine' } ),
back2 = getHi( 'guibg', {'StatusLine' } ),
}
-- Change brightness of colors
-- darken incase of light theme lighten incase of dark theme
if get_color_brightness(utils.extract_highlight_colors('Normal', 'guibg')) > 0.5 then
brightness_modifier_parameter = -brightness_modifier_parameter
end
for name, color in pairs(colors) do
colors[name] = brightness_modifier(color, brightness_modifier_parameter)
end
-- basic theme defination
local M = {
normal = {
a = { bg = colors.normal, fg = colors.back1, gui='bold' },
b = { bg = colors.back1, fg = colors.normal },
c = { bg = colors.back2, fg = colors.fore },
},
insert = {
a = { bg = colors.insert, fg = colors.back1, gui='bold' },
b = { bg = colors.back1, fg = colors.insert },
c = { bg = colors.back2, fg = colors.fore },
},
replace = {
a = { bg = colors.replace, fg= colors.back1, gui='bold' },
b = { bg = colors.back1, fg= colors.replace },
c = { bg = colors.back2, fg= colors.fore },
},
visual = {
a = { bg = colors.visual, fg= colors.back1, gui='bold' },
b = { bg = colors.back1, fg= colors.visual },
c = { bg = colors.back2, fg= colors.fore },
},
command = {
a = { bg = colors.command, fg = colors.back1, gui='bold' },
b = { bg = colors.back1, fg = colors.command },
c = { bg = colors.back2, fg = colors.fore },
},
}
M.terminal = M.command
M.inactive = M.normal
-- Apply prpper contrast so text is readable
for _, section in pairs(M) do
for _, highlight in pairs(section) do
apply_contrast(highlight)
end
end
return M