feat: allow lualine to be temporarily completely disabled

Adds `lualine.hide()`

closes #776
This commit is contained in:
shadmansaleh 2022-08-03 21:09:09 +06:00
parent b80ba74d4c
commit e8294ac56a
4 changed files with 118 additions and 16 deletions

View File

@ -871,6 +871,26 @@ You can disable lualine for specific filetypes:
options = { disabled_filetypes = {'lua'} } options = { disabled_filetypes = {'lua'} }
``` ```
You can also disable lualine completely.
```lua
require('lualine').hide({
place = {'statusline', 'tabline', 'winbar'}, -- The segmentthis change applies to.
unhide = false, -- whether to reenable lualine again/
})
```
The arguments show for hide above are default values.
Which means even if the hide function is called without
arguments it'll work as if these were passed.
So in short to disable lualine completely you can do
```lua
require('lualine').hide()
```
To enable it again you can do
```lua
require('lualine').hide({unhide=true})
```
<!-- panvimdoc-ignore-start --> <!-- panvimdoc-ignore-start -->
### Contributors ### Contributors

View File

@ -932,6 +932,33 @@ You can disable lualine for specific filetypes:
< <
You can also disable lualine completely.
>
require('lualine').hide({
place = {'statusline', 'tabline', 'winbar'}, -- The segmentthis change applies to.
unhide = false, -- whether to reenable lualine again/
})
<
The arguments show for hide above are default values. Which means even if the
hide function is called without arguments itll work as if these were passed.
So in short to disable lualine completely you can do
>
require('lualine').hide()
<
To enable it again you can do
>
require('lualine').hide({unhide=true})
<
WIKI ~ WIKI ~
Check out the wiki <https://github.com/nvim-lualine/lualine.nvim/wiki> for more Check out the wiki <https://github.com/nvim-lualine/lualine.nvim/wiki> for more

View File

@ -17,6 +17,9 @@ local timers = {
stl_timer = vim.loop.new_timer(), stl_timer = vim.loop.new_timer(),
tal_timer = vim.loop.new_timer(), tal_timer = vim.loop.new_timer(),
wb_timer = vim.loop.new_timer(), wb_timer = vim.loop.new_timer(),
halt_stl_refresh = false, -- mutex ?
halt_tal_refresh = false,
halt_wb_refresh = false,
} }
local last_focus = {} local last_focus = {}
@ -400,7 +403,7 @@ local function refresh(opts)
end end
-- update them -- update them
if vim.tbl_contains(opts.place, 'statusline') then if not timers.halt_stl_refresh and vim.tbl_contains(opts.place, 'statusline') then
for _, win in ipairs(wins) do for _, win in ipairs(wins) do
refresh_real_curwin = config.options.globalstatus and last_focus[curtab] or win refresh_real_curwin = config.options.globalstatus and last_focus[curtab] or win
local stl_cur = vim.api.nvim_win_call(refresh_real_curwin, M.statusline) local stl_cur = vim.api.nvim_win_call(refresh_real_curwin, M.statusline)
@ -410,7 +413,7 @@ local function refresh(opts)
end end
end end
end end
if vim.tbl_contains(opts.place, 'winbar') then if not timers.halt_wb_refresh and vim.tbl_contains(opts.place, 'winbar') then
for _, win in ipairs(wins) do for _, win in ipairs(wins) do
refresh_real_curwin = config.options.globalstatus and last_focus[curtab] or win refresh_real_curwin = config.options.globalstatus and last_focus[curtab] or win
if vim.api.nvim_win_get_height(win) > 1 then if vim.api.nvim_win_get_height(win) > 1 then
@ -422,7 +425,7 @@ local function refresh(opts)
end end
end end
end end
if vim.tbl_contains(opts.place, 'tabline') then if not timers.halt_tal_refresh and vim.tbl_contains(opts.place, 'tabline') then
refresh_real_curwin = curwin refresh_real_curwin = curwin
local tbl_cur = vim.api.nvim_win_call(curwin, tabline) local tbl_cur = vim.api.nvim_win_call(curwin, tabline)
local tbl_last = modules.nvim_opts.get_cache('tabline', { global = true }) local tbl_last = modules.nvim_opts.get_cache('tabline', { global = true })
@ -436,10 +439,12 @@ local function refresh(opts)
end end
--- Sets &tabline option to lualine --- Sets &tabline option to lualine
local function set_tabline() ---@param hide boolean|nil if should hide tabline
local function set_tabline(hide)
vim.loop.timer_stop(timers.tal_timer) vim.loop.timer_stop(timers.tal_timer)
timers.halt_tal_refresh = true
vim.cmd([[augroup lualine_tal_refresh | exe "autocmd!" | augroup END]]) vim.cmd([[augroup lualine_tal_refresh | exe "autocmd!" | augroup END]])
if next(config.tabline) ~= nil then if not hide and next(config.tabline) ~= nil then
vim.loop.timer_start( vim.loop.timer_start(
timers.tal_timer, timers.tal_timer,
0, 0,
@ -455,6 +460,7 @@ local function set_tabline()
'lualine_tal_refresh' 'lualine_tal_refresh'
) )
modules.nvim_opts.set('showtabline', 2, { global = true }) modules.nvim_opts.set('showtabline', 2, { global = true })
timers.halt_tal_refresh = false
else else
modules.nvim_opts.restore('tabline', { global = true }) modules.nvim_opts.restore('tabline', { global = true })
modules.nvim_opts.restore('showtabline', { global = true }) modules.nvim_opts.restore('showtabline', { global = true })
@ -463,10 +469,12 @@ end
--- Sets &statusline option to lualine --- Sets &statusline option to lualine
--- adds auto command to redraw lualine on VimResized event --- adds auto command to redraw lualine on VimResized event
local function set_statusline() ---@param hide boolean|nil if should hide statusline
local function set_statusline(hide)
vim.loop.timer_stop(timers.stl_timer) vim.loop.timer_stop(timers.stl_timer)
timers.halt_stl_refresh = true
vim.cmd([[augroup lualine_stl_refresh | exe "autocmd!" | augroup END]]) vim.cmd([[augroup lualine_stl_refresh | exe "autocmd!" | augroup END]])
if next(config.sections) ~= nil or next(config.inactive_sections) ~= nil then if not hide and (next(config.sections) ~= nil or next(config.inactive_sections) ~= nil) then
if vim.go.statusline == '' then if vim.go.statusline == '' then
modules.nvim_opts.set('statusline', '%#Normal#', { global = true }) modules.nvim_opts.set('statusline', '%#Normal#', { global = true })
end end
@ -503,6 +511,7 @@ local function set_statusline()
'lualine_stl_refresh' 'lualine_stl_refresh'
) )
end end
timers.halt_stl_refresh = false
else else
modules.nvim_opts.restore('statusline', { global = true }) modules.nvim_opts.restore('statusline', { global = true })
for _, win in ipairs(vim.api.nvim_list_wins()) do for _, win in ipairs(vim.api.nvim_list_wins()) do
@ -513,10 +522,12 @@ local function set_statusline()
end end
--- Sets &winbar option to lualine --- Sets &winbar option to lualine
local function set_winbar() ---@param hide boolean|nil if should unset winbar
local function set_winbar(hide)
vim.loop.timer_stop(timers.wb_timer) vim.loop.timer_stop(timers.wb_timer)
timers.halt_wb_refresh = true
vim.cmd([[augroup lualine_wb_refresh | exe "autocmd!" | augroup END]]) vim.cmd([[augroup lualine_wb_refresh | exe "autocmd!" | augroup END]])
if next(config.winbar) ~= nil or next(config.inactive_winbar) ~= nil then if not hide and (next(config.winbar) ~= nil or next(config.inactive_winbar) ~= nil) then
vim.loop.timer_start( vim.loop.timer_start(
timers.stl_timer, timers.stl_timer,
0, 0,
@ -531,6 +542,7 @@ local function set_winbar()
"call v:lua.require'lualine'.refresh({'kind': 'tabpage', 'place': ['winbar'], 'trigger': 'autocmd'})", "call v:lua.require'lualine'.refresh({'kind': 'tabpage', 'place': ['winbar'], 'trigger': 'autocmd'})",
'lualine_wb_refresh' 'lualine_wb_refresh'
) )
timers.halt_wb_refresh = false
elseif vim.fn.has('nvim-0.8') == 1 then elseif vim.fn.has('nvim-0.8') == 1 then
modules.nvim_opts.restore('winbar', { global = true }) modules.nvim_opts.restore('winbar', { global = true })
for _, win in ipairs(vim.api.nvim_list_wins()) do for _, win in ipairs(vim.api.nvim_list_wins()) do
@ -539,6 +551,34 @@ local function set_winbar()
end end
end end
---@alias LualineHideOptsPlace
---| 'statusline'
---| 'tabline'
---| 'winbar'
---@class LualineHideOpts
---@field place LualineHideOptsPlace[]
---@field unhide boolean
---@param opts LualineHideOpts
local function hide(opts)
if opts == nil then
opts = {}
end
opts = vim.tbl_extend('keep', opts, {
place = {'statusline', 'tabline', 'winbar'},
unhide = false,
})
local hide_fn = {
statusline = set_statusline,
tabline = set_tabline,
winbar = set_winbar,
}
for _, place in ipairs(opts.place) do
if hide_fn[place] then
hide_fn[place](not opts.unhide)
end
end
end
-- lualine.setup function -- lualine.setup function
--- sets new user config --- sets new user config
--- This function doesn't load components/theme etc... They are done before --- This function doesn't load components/theme etc... They are done before
@ -573,6 +613,7 @@ M = {
get_config = modules.config_module.get_config, get_config = modules.config_module.get_config,
refresh = refresh, refresh = refresh,
winbar = status_dispatch('winbar'), winbar = status_dispatch('winbar'),
hide = hide,
} }
return M return M

View File

@ -20,9 +20,9 @@ local M = {}
---@field set any ---@field set any
---@alias LualineNvimOptCacheOpt table<string, LualineNvimOptCacheOptStore> ---@alias LualineNvimOptCacheOpt table<string, LualineNvimOptCacheOptStore>
---@class LualineNvimOptCache ---@class LualineNvimOptCache
---@field global LualineNvimOptCacheOpt[] ---@field global LualineNvimOptCacheOptStore[]
---@field buffer table<number, LualineNvimOptCacheOpt[]> ---@field buffer table<number, LualineNvimOptCacheOptStore[]>
---@field window table<number, LualineNvimOptCacheOpt[]> ---@field window table<number, LualineNvimOptCacheOptStore[]>
---@type LualineNvimOptCache ---@type LualineNvimOptCache
local options = { global = {}, buffer = {}, window = {} } local options = { global = {}, buffer = {}, window = {} }
@ -42,8 +42,10 @@ local function set_opt(name, val, getter_fn, setter_fn, cache_tbl)
cache_tbl[name] = {} cache_tbl[name] = {}
end end
if cache_tbl[name].set ~= cur then if cache_tbl[name].set ~= cur then
if type(cur) ~= 'string' or not cur:find('lualine') then
cache_tbl[name].prev = cur cache_tbl[name].prev = cur
end end
end
cache_tbl[name].set = val cache_tbl[name].set = val
setter_fn(name, val) setter_fn(name, val)
end end
@ -83,7 +85,11 @@ end
function M.restore(name, opts) function M.restore(name, opts)
if opts == nil or opts.global then if opts == nil or opts.global then
if options.global[name] ~= nil and options.global[name].prev ~= nil then if options.global[name] ~= nil and options.global[name].prev ~= nil then
vim.api.nvim_set_option(name, options.global[name].prev) local restore_to = options.global[name].prev
if type(restore_to) == 'string' and restore_to:find('lualine') then
restore_to = ''
end
vim.api.nvim_set_option(name, restore_to)
end end
elseif opts.buffer then elseif opts.buffer then
if if
@ -91,7 +97,11 @@ function M.restore(name, opts)
and options.buffer[opts.buffer][name] ~= nil and options.buffer[opts.buffer][name] ~= nil
and options.buffer[opts.buffer][name].prev ~= nil and options.buffer[opts.buffer][name].prev ~= nil
then then
vim.api.nvim_buf_set_option(opts.buffer, name, options.buffer[opts.buffer][name].prev) local restore_to = options.buffer[opts.buffer][name].prev
if type(restore_to) == 'string' and restore_to:find('lualine') then
restore_to = ''
end
vim.api.nvim_buf_set_option(opts.buffer, name, restore_to)
end end
elseif opts.window then elseif opts.window then
if if
@ -99,7 +109,11 @@ function M.restore(name, opts)
and options.window[opts.window][name] ~= nil and options.window[opts.window][name] ~= nil
and options.window[opts.window][name].prev ~= nil and options.window[opts.window][name].prev ~= nil
then then
vim.api.nvim_win_set_option(opts.window, name, options.window[opts.window][name].prev) local restore_to = options.window[opts.window][name].prev
if type(restore_to) == 'string' and restore_to:find('lualine') then
restore_to = ''
end
vim.api.nvim_win_set_option(opts.window, name, restore_to)
end end
end end
end end