From e35fd60ac90018ebb39e5c9d197e8bb2a9629ece Mon Sep 17 00:00:00 2001 From: zztrieuzz Date: Fri, 29 Sep 2023 08:22:19 +0700 Subject: [PATCH] feat: add components selection_count 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'} } ``` --- lua/windline/components/vim.lua | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/lua/windline/components/vim.lua b/lua/windline/components/vim.lua index da69010..6bfeca6 100644 --- a/lua/windline/components/vim.lua +++ b/lua/windline/components/vim.lua @@ -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 @@ -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