Skip to content

Commit

Permalink
feat(config): allow passing callback function to opts.scope (#12)
Browse files Browse the repository at this point in the history
  • Loading branch information
bekaboo committed Apr 30, 2023
1 parent 1c35515 commit 8f19d5f
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 6 deletions.
22 changes: 20 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -112,8 +112,8 @@ local opts = {
require('deadcolumn').setup(opts) -- Call the setup function
```

- `scope` (string): The scope for showing the colored column, there are several
possible values:
- `scope` (string|function): The scope for showing the colored column, there
are several possible values:

- `'line'`: colored column will be shown based on the length of the current
line.
Expand All @@ -127,6 +127,24 @@ require('deadcolumn').setup(opts) -- Call the setup function
- `'cursor'`: colored column will be shown based on current cursor
position.

- function() -> number: callback function that returns a number as the
length of the row. For example, to show the colored column based on the
longest line in the nearby 100 lines:

```lua
require('deadcolumn').setup({
scope = function()
local max = 0
for i = -50, 50 do
local len = vim.fn.strdisplaywidth(vim.fn.getline(vim.fn.line('.') + i))
if len > max then
max = len
end
return max
end
})
```

- `modes` (table): In which modes to show the colored column.

- `blending` (table): Blending options.
Expand Down
22 changes: 21 additions & 1 deletion doc/deadcolumn.nvim.txt
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ deadcolumn.setup({*opts}) *deadcolumn.options.setup()*
Valid keys for {opts}

*deadcolumn.options.opts.scope*
`scope` string (default "line")
`scope` string|function (default "line")

The scope for showing the colored column.

Expand All @@ -99,6 +99,26 @@ deadcolumn.setup({*opts}) *deadcolumn.options.setup()*

Colored column will be shown based on current cursor position.

5. function() -> number

Callback function that returns a number as the length of the
row. For example, to show the colored column based on the longest
line in the nearby 100 lines: >lua

require('deadcolumn').setup({
scope = function()
local max = 0
for i = -50, 50 do
local len = vim.fn.strdisplaywidth(vim.fn.getline(vim.fn.line('.') + i))
if len > max then
max = len
end
end
return max
end
})
<

*deadcolumn.options.ops.modes*
`modes` table
(default: { "i", "ic", "ix", "R", "Rc", "Rx", "Rv", "Rvc", "Rvx" })
Expand Down
4 changes: 3 additions & 1 deletion lua/deadcolumn/autocmds.lua
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,9 @@ local function redraw_cc()
return
end

local len = scope_len_fn[configs.opts.scope]()
local len = type(configs.opts.scope) == 'string'
and scope_len_fn[configs.opts.scope]()
or configs.opts.scope()
local thresh = configs.opts.blending.threshold
if 0 < thresh and thresh <= 1 then
thresh = math.floor(thresh * cc)
Expand Down
8 changes: 6 additions & 2 deletions lua/deadcolumn/configs.lua
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,12 @@ function M.set_options(user_opts)
end
assert(vim.tbl_islist(M.opts.modes), 'modes must be a list of strings')
assert(
vim.tbl_contains({ 'line', 'buffer', 'visible', 'cursor' }, M.opts.scope),
'scope must be one of "line", "buffer", "visible", "cursor"'
type(M.opts.scope) == 'function'
or vim.tbl_contains(
{ 'line', 'buffer', 'visible', 'cursor' },
M.opts.scope
),
'scope must be a function or one of "line", "buffer", "visible", "cursor"'
)
assert(M.opts.blending.threshold >= 0, 'blending.threshold must be >= 0')
assert(
Expand Down

0 comments on commit 8f19d5f

Please sign in to comment.