vim/lualine: add custom searchcount component

This commit is contained in:
Fernando Schauenburg 2023-08-13 02:33:39 +02:00
parent 6244808688
commit ea9cd3872d
2 changed files with 38 additions and 1 deletions

View file

@ -1,5 +1,7 @@
local M = {} local M = {}
-- filename component
M.filename = require('lualine.component'):extend() M.filename = require('lualine.component'):extend()
function M.filename:init(options) function M.filename:init(options)
@ -35,6 +37,7 @@ function M.filename:update_status(is_focused)
end end
end end
-- mode component
M.mode = require('lualine.component'):extend() M.mode = require('lualine.component'):extend()
@ -67,6 +70,37 @@ function M.mode:update_status(is_focused)
return ' 󰒲 ' return ' 󰒲 '
end 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() M.trailing_whitespace = function()
local pattern = [[\s\+$]] local pattern = [[\s\+$]]
local lineno = vim.fn.search(pattern, 'nwc') local lineno = vim.fn.search(pattern, 'nwc')
@ -84,6 +118,8 @@ M.trailing_whitespace = function()
return result return result
end end
-- modifier to color component when window is in focus
M.colored_if_focused = function(component) M.colored_if_focused = function(component)
if type(component) == 'string' then if type(component) == 'string' then
local c = require('lualine.components.' .. component):extend() local c = require('lualine.components.' .. component):extend()

View file

@ -48,6 +48,7 @@ return {
}, },
cond = function() return vim.o.paste end cond = function() return vim.o.paste end
}, },
searchcount = require('fschauen.lualine').searchcount,
status = { status = {
colored_if_focused(function(_) colored_if_focused(function(_)
local status = '' local status = ''
@ -71,7 +72,7 @@ return {
lualine_a = { C.paste, C.mode }, lualine_a = { C.paste, C.mode },
lualine_b = { C.branch }, lualine_b = { C.branch },
lualine_c = { C.filename, C.status }, lualine_c = { C.filename, C.status },
lualine_x = { C.diagnostics, C.filetype }, lualine_x = { C.diagnostics, C.searchcount, C.filetype },
lualine_y = { C.fileformat, 'progress' }, lualine_y = { C.fileformat, 'progress' },
lualine_z = { 'location', C.trailing_whitespace }, lualine_z = { 'location', C.trailing_whitespace },
} }