Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add selectioncount formatting options #1116

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -658,6 +658,26 @@ sections = {
}
```

#### selectioncount component options

```lua
sections = {
lualine_a = {
{
'selectioncount',
format = {
-- `%b`: bytes, `%c`: chars, `%l`: lines
single_line_no_multibyte = '[%c]',
single_line_multibyte = '[%c-%b]',
multi_line_no_multibyte = '[%c / %l]',
multi_line_multibyte = '[%c-%b / %l]',
visual_block_mode = '[%cx%l]',
}
}
}
}
```

#### searchcount component options

```lua
Expand Down
74 changes: 63 additions & 11 deletions lua/lualine/components/selectioncount.lua
Original file line number Diff line number Diff line change
@@ -1,16 +1,68 @@
local function selectioncount()
local mode = vim.fn.mode(true)
local line_start, col_start = vim.fn.line('v'), vim.fn.col('v')
local line_end, col_end = vim.fn.line('.'), vim.fn.col('.')
if mode:match('') then
return string.format('%dx%d', math.abs(line_start - line_end) + 1, math.abs(col_start - col_end) + 1)
elseif mode:match('V') or line_start ~= line_end then
return math.abs(line_start - line_end) + 1
elseif mode:match('v') then
return math.abs(col_start - col_end) + 1
local M = require('lualine.component'):extend()

local default_format = {
single_line_no_multibyte = '[%c]',
single_line_multibyte = '[%c-%b]',
multi_line_no_multibyte = '[%c / %l]',
multi_line_multibyte = '[%c-%b / %l]',
visual_block_mode = '[%cx%l]',
}

function M:init(options)
M.super.init(self, options)
self.format = vim.tbl_extend('keep', self.options.format or {}, default_format)
end

function M:update_status()
local mode = vim.fn.mode()
local lines = math.abs(vim.fn.line('v') - vim.fn.line('.')) + 1
if mode == 'v' or mode == 'V' then
local wc = vim.fn.wordcount()
local is_multibyte = wc.visual_chars ~= wc.visual_bytes
local is_multiline = lines > 1
local chars = wc.visual_chars
if is_multiline then
if is_multibyte then
local bytes = wc.visual_bytes
return self.format.multi_line_multibyte
:gsub("^%%b", bytes):gsub("([^%%])%%b", "%1"..bytes)
:gsub("^%%c", chars):gsub("([^%%])%%c", "%1"..chars)
:gsub("^%%l", lines):gsub("([^%%])%%l", "%1"..lines)
else
return self.format.multi_line_no_multibyte
:gsub("^%%c", chars):gsub("([^%%])%%c", "%1"..chars)
:gsub("^%%l", lines):gsub("([^%%])%%l", "%1"..lines)
end
else
if is_multibyte then
local bytes = wc.visual_bytes
return self.format.single_line_multibyte
:gsub("^%%b", bytes):gsub("([^%%])%%b", "%1"..bytes)
:gsub("^%%c", chars):gsub("([^%%])%%c", "%1"..chars)
else
return self.format.single_line_no_multibyte
:gsub("^%%c", chars):gsub("([^%%])%%c", "%1"..chars)
end
end
elseif mode == '' then
local cols = vim.fn.virtcol('.') - vim.fn.virtcol('v')
local line, col
if cols >= 0 then
line = vim.fn.getline('v')
col = vim.fn.charcol('v') - 1
else
line = vim.fn.getline('.')
col = vim.fn.charcol('.') - 1
cols = -cols
end
local char1width = vim.fn.strwidth(vim.fn.strcharpart(line, col, 1))
local chars = cols+char1width
return self.format.visual_block_mode
:gsub("^%%c", chars):gsub("([^%%])%%c", "%1"..chars)
:gsub("^%%l", lines):gsub("([^%%])%%l", "%1"..lines)
else
return ''
end
end

return selectioncount
return M
Loading