Skip to content

Commit

Permalink
feat: add components selection_count
Browse files Browse the repository at this point in the history
It counts the length of the text selection
work on visual mode and has a same behaviour as vim.

```lua
local vim_comps = require('windline.components.vim')
{ vim_comps.selection_count("(%s)"),{'red', 'NormalBg'} }

```
  • Loading branch information
zztrieuzz committed Sep 29, 2023
1 parent f58634d commit e35fd60
Showing 1 changed file with 16 additions and 1 deletion.
17 changes: 16 additions & 1 deletion lua/windline/components/vim.lua
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ M.search_count = function(opt)
return ''
end

if result.incomplete == 1 then -- timed out
if result.incomplete == 1 then -- timed out
return ' ?/?? '
elseif result.incomplete == 2 then -- max count exceeded
if result.total > result.maxcount and result.current > result.maxcount then
Expand All @@ -67,4 +67,19 @@ M.search_count = function(opt)
return string.format(' %d/%d ', result.current or '', result.total or '')
end
end

local state = _G.WindLine.state
M.selection_count = function(format)
return function()
if state.mode[2] == 'Visual' then
local _, ls, cs = unpack(vim.fn.getpos('v'))
local _, le, ce = unpack(vim.fn.getpos('.'))
if le - ls ~= 0 then
return string.format(format or " %s ", math.abs(le - ls) + 1)
end
return string.format(format or " %s ", math.abs(ce - cs) + 1)
end
end
end

return M

0 comments on commit e35fd60

Please sign in to comment.