fix: same info when starting with several windows

Fix: 14
This commit is contained in:
Olivier Roques
2022-01-27 16:42:36 +01:00
parent da71801866
commit 0913699777
2 changed files with 70 additions and 87 deletions

View File

@@ -3,12 +3,10 @@
-- github.com/ojroques
-------------------- VARIABLES -----------------------------
local fn, cmd, vim = vim.fn, vim.cmd, vim
local g, o, wo = vim.g, vim.o, vim.wo
local fmt = string.format
local common = require('hardline.common')
local bufferline = require('hardline.bufferline')
local custom_colors = require('hardline.themes.custom_colors')
local fmt = string.format
local M = {}
-------------------- OPTIONS -------------------------------
@@ -20,6 +18,19 @@ M.options = {
separator = '|',
},
theme = 'default',
sections = {
{class = 'mode', item = require('hardline.parts.mode').get_item},
{class = 'high', item = require('hardline.parts.git').get_item, hide_width = 100},
{class = 'med', item = require('hardline.parts.filename').get_item},
'%<',
{class = 'med', item = '%='},
{class = 'low', item = require('hardline.parts.wordcount').get_item, hide_width = 100},
{class = 'error', item = require('hardline.parts.lsp').get_error},
{class = 'warning', item = require('hardline.parts.lsp').get_warning},
{class = 'warning', item = require('hardline.parts.whitespace').get_item},
{class = 'high', item = require('hardline.parts.filetype').get_item, hide_width = 80},
{class = 'mode', item = require('hardline.parts.line').get_item},
},
custom_theme = {
text = {gui = "NONE", cterm = "NONE", cterm16 = "NONE"},
normal = {gui = "NONE", cterm = "NONE", cterm16 = "NONE"},
@@ -33,32 +44,8 @@ M.options = {
alt_text = {gui = "NONE", cterm = "NONE", cterm16 = "NONE"},
warning = {gui = "NONE", cterm = "NONE", cterm16 = "NONE"},
},
sections = {
{class = 'mode', item = require('hardline.parts.mode').get_item},
{class = 'high', item = require('hardline.parts.git').get_item, hide = 100},
{class = 'med', item = require('hardline.parts.filename').get_item},
'%<',
{class = 'med', item = '%='},
{class = 'low', item = require('hardline.parts.wordcount').get_item, hide = 100},
{class = 'error', item = require('hardline.parts.lsp').get_error},
{class = 'warning', item = require('hardline.parts.lsp').get_warning},
{class = 'warning', item = require('hardline.parts.whitespace').get_item},
{class = 'high', item = require('hardline.parts.filetype').get_item, hide = 80},
{class = 'mode', item = require('hardline.parts.line').get_item},
},
}
-------------------- SECTION CACHE -------------------------
local cache = {}
local function refresh_cache()
for winid, _ in pairs(cache) do
if fn.win_id2tabwin(winid) == {0, 0} then
cache[winid] = nil
end
end
end
-------------------- SECTION MANAGEMENT --------------------
local function aggregate_sections(sections)
local aggregated, piv = {}, 1
@@ -94,39 +81,38 @@ local function remove_empty_sections(sections)
return vim.tbl_filter(filter, sections)
end
local function load_section(section)
if type(section) == 'string' then
return section
end
if type(section) == 'function' then
return section()
end
if type(section) == 'table' then
return {
class = section.class or 'none',
item = load_section(section.item),
}
end
common.echo('WarningMsg', 'Invalid section.')
return ''
end
local function load_sections(sections)
function load_section(section)
if type(section) == 'string' then
return section
end
if type(section) == 'function' then
return section()
end
if type(section) == 'table' then
return {
class = section.class or 'none',
item = load_section(section.item),
}
end
common.echo('WarningMsg', 'Invalid section.')
return ''
end
return vim.tbl_map(load_section, sections)
end
local function remove_hidden_sections(sections)
local filter = function(s)
return not s.hide or s.hide <= fn.winwidth(0)
local filter = function(section)
return not section.hide or section.hide <= fn.winwidth(0)
end
return vim.tbl_filter(filter, sections)
end
-------------------- SECTION HIGHLIGHTING ------------------
local function get_section_state(section)
local function get_section_state(section, is_active)
if section.class == 'mode' then
if common.is_active() then
local mode = common.modes[fn.mode()] or common.modes['?']
if is_active then
local mode = common.modes[vim.fn.mode()] or common.modes['?']
return mode.state
end
end
@@ -140,41 +126,36 @@ local function get_section_state(section)
end
return state
end
return common.is_active() and 'active' or 'inactive'
return is_active and 'active' or 'inactive'
end
local function highlight_section(section)
if type(section) ~= 'table' then
return section
local function highlight_sections(sections, is_active)
function highlight_section(section)
if type(section) ~= 'table' then
return section
end
if section.class == 'none' then
return section.item
end
local state = get_section_state(section, is_active)
local hlgroup = fmt('Hardline_%s_%s', section.class, state)
if vim.fn.hlexists(hlgroup) == 0 then
return section.item
end
return fmt('%%#%s#%s%%*', hlgroup, section.item)
end
if section.class == 'none' then
return section.item
end
local state = get_section_state(section)
local hlgroup = fmt('Hardline_%s_%s', section.class, state)
if fn.hlexists(hlgroup) == 0 then
return section.item
end
return fmt('%%#%s#%s%%*', hlgroup, section.item)
end
local function highlight_sections(sections)
return vim.tbl_map(highlight_section, sections)
end
-------------------- STATUSLINE ----------------------------
function M.update_statusline()
local sections = cache[g.statusline_winid]
if common.is_active() or not sections then
sections = M.options.sections
sections = remove_hidden_sections(sections)
sections = load_sections(sections)
sections = remove_empty_sections(sections)
sections = aggregate_sections(sections)
cache[g.statusline_winid] = sections
refresh_cache()
end
return table.concat(highlight_sections(sections))
function M.update_statusline(is_active)
local sections = M.options.sections
sections = remove_hidden_sections(sections)
sections = load_sections(sections)
sections = remove_empty_sections(sections)
sections = aggregate_sections(sections)
sections = highlight_sections(sections, is_active)
return table.concat(sections)
end
-------------------- BUFFERLINE ----------------------------
@@ -213,20 +194,26 @@ local function set_hlgroups()
table.insert(a, fmt('%s=%s', k, v))
end
a = table.concat(a, ' ')
cmd(fmt('autocmd VimEnter,ColorScheme * hi %s %s', hlgroup, a))
vim.cmd(fmt('autocmd VimEnter,ColorScheme * hi %s %s', hlgroup, a))
end
end
end
local function set_statusline()
o.showmode = false
o.statusline = [[%!luaeval('require("hardline").update_statusline()')]]
wo.statusline = o.statusline
vim.opt.showmode = false
vim.opt.statusline = [[%{%luaeval('require("hardline").update_statusline(true)')%}]]
vim.cmd([[
augroup hardline
autocmd!
autocmd WinEnter,BufEnter * setlocal statusline=%{%luaeval('require(\"hardline\").update_statusline(true)')%}
autocmd WinLeave,BufLeave * setlocal statusline=%{%luaeval('require(\"hardline\").update_statusline(false)')%}
augroup END
]])
end
local function set_bufferline()
o.showtabline = 2
o.tabline = [[%!luaeval('require("hardline").update_bufferline()')]]
vim.opt.showtabline = 2
vim.opt.tabline = [[%!luaeval('require("hardline").update_bufferline()')]]
end
function M.setup(user_options)