vim/lualine: make plugin config self-contained

This commit is contained in:
Fernando Schauenburg 2023-08-17 23:49:36 +02:00
parent 56aa86df56
commit 5da88d464b
2 changed files with 180 additions and 216 deletions

View file

@ -1,151 +0,0 @@
local M = {}
-- filename component
M.filename = require('lualine.component'):extend()
function M.filename:init(options)
M.filename.super.init(self, options)
local color = options.color or {}
local modified = { gui = 'italic' }
self.custom_highlights = { -- [is_focused, modified]
[true] = {
[true] = self:create_hl(vim.tbl_extend('force', color, modified), 'focus_modified'),
[false] = self:create_hl(color, 'focus'),
},
[false] = {
[true] = self:create_hl(modified, 'nofocus_modified'),
[false] = self:create_hl({}, 'nofocus'),
},
}
end
function M.filename:update_status(is_focused)
self.options.color_highlight = self.custom_highlights[is_focused][vim.bo.modified]
local window = require 'fschauen.window'
local path = vim.fn.expand('%:~:.')
if window.is_wide() then
return path
elseif window.is_medium() then
return vim.fn.pathshorten(path) -- only first letter of directories
else
return vim.fn.fnamemodify(path, ':t') -- only tail
end
end
-- mode component
M.mode = require('lualine.component'):extend()
M.mode.map = {
['n'] = '', -- 'Normal ', -- Normal
['no'] = '', -- 'O-Pend ', -- Operator-pending
['ni'] = '', -- 'Normal ', -- Normal via i_CTRL-O
['v'] = '󰒉', -- 'Visual ', -- Visual by character
[''] = '󰩭', -- 'V-Block', -- Visual blockwise
['s'] = '󰒉', -- 'Select ', -- Select by character
[''] = '󰩭', -- 'S-Block', -- Select blockwise
['i'] = '', -- 'Insert ', -- Insert
['r'] = '󰄾', -- 'Replace', -- Replace
['rv'] = '󰶻', -- 'V-Repl ', -- Virtual Replace
['c'] = '', -- 'Command', -- Command-line
['cv'] = '', -- ' Ex ', -- Ex mode
['rm'] = '', -- ' modeore ', -- -- modeORE --
['r?'] = '󰭚', -- 'Confirm', -- :confirm
['!'] = '', -- ' Shell ', -- External command executing
['t'] = '', -- ' Term ', -- Terminal
}
function M.mode:update_status(is_focused)
if is_focused then
local code = vim.api.nvim_get_mode().mode:lower()
local symbol = M.mode.map[code:sub(1, 2)] or M.mode.map[code:sub(1, 1)] or code
return ' ' .. symbol .. ' '
end
return ' 󰒲 '
end
-- searchcount component
M.searchcount = require('lualine.component'):extend()
function M.searchcount:init(options)
M.searchcount.super.init(self, options)
self.options = vim.tbl_extend('keep', self.options or {}, {
maxcount = 999,
timeout = 250,
})
end
function M.searchcount:update_status()
if vim.v.hlsearch == 0 then
return ''
end
local count = vim.fn.searchcount {
maxcount = self.options.maxcount,
timeout = self.options.timeout
}
if next(count) == nil then
return ''
end
local denominator = count.total > count.maxcount and '' or string.format('%d', count.total)
return string.format(' %d/%s', count.current, denominator)
end
-- trailing_whitespace component
M.trailing_whitespace = function()
local pattern = [[\s\+$]]
local lineno = vim.fn.search(pattern, 'nwc')
if lineno == 0 then
return ''
end
local result = '·' .. lineno
local info = vim.fn.searchcount { pattern = pattern }
if info.total then
result = result .. string.format(' Σ%d', info.total)
end
return result
end
-- modifier to color component when window is in focus
M.colored_if_focused = function(component)
if type(component) == 'string' then
local c = require('lualine.components.' .. component):extend()
function c:update_status(is_focused)
self.options.colored = is_focused
return c.super.update_status(self, is_focused)
end
return c
elseif type(component) == 'function' then
local c = require('lualine.component'):extend()
function c:init(options)
c.super.init(self, options)
self.saved_hl = self.options.color_highlight
end
function c:update_status(is_focused)
self.options.color_highlight = is_focused and self.saved_hl or nil
return component(is_focused)
end
return c
end
end
return M

View file

@ -9,51 +9,155 @@ M.dependencies = {
M.config = function()
local window = require 'fschauen.window'
local colored_if_focused = require('fschauen.lualine').colored_if_focused
-- custom components
local C = {
diagnostics = {
colored_if_focused('diagnostics'),
local filename = (function()
local C = require('lualine.component'):extend()
function C:init(options)
C.super.init(self, options)
local color = options.color or {}
local modified = { gui = 'italic' }
self.custom_highlights = { -- [is_focused, modified]
[true] = {
[true] = self:create_hl(vim.tbl_extend('force', color, modified), 'focus_modified'),
[false] = self:create_hl(color, 'focus'),
},
diff = {
colored_if_focused('diff'),
symbols = {
added = '',
modified = '',
removed = '',
[false] = {
[true] = self:create_hl(modified, 'nofocus_modified'),
[false] = self:create_hl({}, 'nofocus'),
},
},
branch = {
'branch',
icon = '󰘬',
cond = window.is_medium,
},
fileformat = {
'fileformat',
cond = window.is_medium,
},
filename = {
require('fschauen.lualine').filename,
padding = {
left = 1,
right = 0,
},
},
filetype = {
colored_if_focused('filetype'),
cond = window.is_medium,
},
mode = require('fschauen.lualine').mode,
paste = {
}
end
function C:update_status(is_focused)
self.options.color_highlight = self.custom_highlights[is_focused][vim.bo.modified]
local path = vim.fn.expand('%:~:.')
if window.is_wide() then
return path
elseif window.is_medium() then
return vim.fn.pathshorten(path) -- only first letter of directories
else
return vim.fn.fnamemodify(path, ':t') -- only tail
end
end
return C
end)()
local mode = (function()
local C = require('lualine.component'):extend()
C.map = {
['n'] = '', -- 'Normal ', -- Normal
['no'] = '', -- 'O-Pend ', -- Operator-pending
['ni'] = '', -- 'Normal ', -- Normal via i_CTRL-O
['v'] = '󰒉', -- 'Visual ', -- Visual by character
[''] = '󰩭', -- 'V-Block', -- Visual blockwise
['s'] = '󰒉', -- 'Select ', -- Select by character
[''] = '󰩭', -- 'S-Block', -- Select blockwise
['i'] = '', -- 'Insert ', -- Insert
['r'] = '󰄾', -- 'Replace', -- Replace
['rv'] = '󰶻', -- 'V-Repl ', -- Virtual Replace
['c'] = '', -- 'Command', -- Command-line
['cv'] = '', -- ' Ex ', -- Ex mode
['rm'] = '', -- ' modeore ', -- -- modeORE --
['r?'] = '󰭚', -- 'Confirm', -- :confirm
['!'] = '', -- ' Shell ', -- External command executing
['t'] = '', -- ' Term ', -- Terminal
}
function C:update_status(is_focused)
if not is_focused then return ' 󰒲 ' end
local code = vim.api.nvim_get_mode().mode:lower()
local symbol = C.map[code:sub(1, 2)] or C.map[code:sub(1, 1)] or code
return ' ' .. symbol .. ' '
end
return C
end)()
local searchcount = (function()
local C = require('lualine.component'):extend()
function C:init(options)
C.super.init(self, options)
self.options = vim.tbl_extend('keep', self.options or {}, {
maxcount = 999,
timeout = 250,
})
end
function C:update_status()
if vim.v.hlsearch == 0 then return '' end
local count = vim.fn.searchcount {
maxcount = self.options.maxcount,
timeout = self.options.timeout
}
if next(count) == nil then return '' end
local denominator = count.total > count.maxcount and '' or string.format('%d', count.total)
return string.format(' %d/%s', count.current, denominator)
end
return C
end)()
local trailing_whitespace = function()
local trailing = [[\s\+$]]
local lineno = vim.fn.search(trailing, 'nwc')
if lineno == 0 then return '' end
local result = '' .. lineno
local total = vim.fn.searchcount({ pattern = trailing }).total
if total > 1 then result = result .. string.format(' (%d total)', total) end
return result
end
local colored_if_focused = function(component)
if type(component) == 'string' then
local C = require('lualine.components.' .. component):extend()
function C:update_status(is_focused)
self.options.colored = is_focused
return C.super.update_status(self, is_focused)
end
return C
elseif type(component) == 'function' then
local C = require('lualine.component'):extend()
function C:init(options)
C.super.init(self, options)
self.saved_hl = self.options.color_highlight
end
function C:update_status(is_focused)
self.options.color_highlight = is_focused and self.saved_hl or nil
return component(is_focused)
end
return C
end
end
local paste = {
colored_if_focused(function(has_focus) return has_focus and '' or ' ' end),
color = {
bg = orange,
},
cond = function() return vim.o.paste end
},
searchcount = require('fschauen.lualine').searchcount,
status = {
}
local status = {
colored_if_focused(function(_)
local status = ''
if vim.bo.modified then status = status .. '' end
@ -63,22 +167,33 @@ M.config = function()
color = {
fg = bright,
},
},
trailing_whitespace = {
colored_if_focused(require('fschauen.lualine').trailing_whitespace),
color = {
bg = orange,
},
}
}
local sections = {
lualine_a = { C.paste, C.mode },
lualine_b = { C.branch },
lualine_c = { C.filename, C.status },
lualine_x = { C.diagnostics, C.searchcount, C.filetype },
lualine_y = { C.fileformat, 'progress' },
lualine_z = { 'location', C.trailing_whitespace },
lualine_a = {
paste,
mode
},
lualine_b = {
{ 'branch', icon = '󰘬', cond = window.is_medium },
},
lualine_c = {
filename,
status,
},
lualine_x = {
colored_if_focused('diagnostics'),
searchcount,
{ colored_if_focused('filetype'), cond = window.is_medium },
},
lualine_y = {
{ 'fileformat', cond = window.is_medium },
'progress',
},
lualine_z = {
'location',
{ colored_if_focused(trailing_whitespace), color = { bg = orange } },
},
}
require('lualine').setup {