Skip to content

Commit 67adea4

Browse files
committed
(mini.notify) FEATURE: implement config.window.max_width_share.
1 parent 0f26ce5 commit 67adea4

8 files changed

+157
-6
lines changed

doc/mini-notify.txt

+9-1
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,9 @@ Default values:
119119
-- Floating window config
120120
config = {},
121121
122+
-- Maximum window width as share (between 0 and 1) of available columns
123+
max_width_share = 0.382,
124+
122125
-- Value of 'winblend' option
123126
winblend = 25,
124127
},
@@ -188,7 +191,8 @@ or a callable returning such table (will be called with identifier of
188191
window's buffer already showing notifications). It should have the same
189192
structure as in |nvim_open_win()|. It has the following default values
190193
which show notifications in the upper right corner with upper limit on width:
191-
- `width` is chosen to fit buffer content but not more than 38.2% of 'columns'.
194+
- `width` is chosen to fit buffer content but at most `window.max_width_share`
195+
share of 'columns'.
192196
To have higher maximum width, use function in `config.window` which computes
193197
dimensions inside of it (based on buffer content).
194198
- `height` is chosen to fit buffer content with enabled 'wrap' (assuming
@@ -197,6 +201,10 @@ which show notifications in the upper right corner with upper limit on width:
197201
- `border` is "single".
198202
- `zindex` is 999 to be as much on top as reasonably possible.
199203

204+
`window.max_width_share` defines maximum window width as a share of 'columns'.
205+
Should be a number between 0 (not included) and 1.
206+
Default: 0.382.
207+
200208
`window.winblend` defines 'winblend' value for notification window.
201209
Default: 25.
202210

lua/mini/notify.lua

+17-5
Original file line numberDiff line numberDiff line change
@@ -175,7 +175,8 @@ end
175175
--- window's buffer already showing notifications). It should have the same
176176
--- structure as in |nvim_open_win()|. It has the following default values
177177
--- which show notifications in the upper right corner with upper limit on width:
178-
--- - `width` is chosen to fit buffer content but not more than 38.2% of 'columns'.
178+
--- - `width` is chosen to fit buffer content but at most `window.max_width_share`
179+
--- share of 'columns'.
179180
--- To have higher maximum width, use function in `config.window` which computes
180181
--- dimensions inside of it (based on buffer content).
181182
--- - `height` is chosen to fit buffer content with enabled 'wrap' (assuming
@@ -184,6 +185,10 @@ end
184185
--- - `border` is "single".
185186
--- - `zindex` is 999 to be as much on top as reasonably possible.
186187
---
188+
--- `window.max_width_share` defines maximum window width as a share of 'columns'.
189+
--- Should be a number between 0 (not included) and 1.
190+
--- Default: 0.382.
191+
---
187192
--- `window.winblend` defines 'winblend' value for notification window.
188193
--- Default: 25.
189194
MiniNotify.config = {
@@ -212,6 +217,9 @@ MiniNotify.config = {
212217
-- Floating window config
213218
config = {},
214219

220+
-- Maximum window width as share (between 0 and 1) of available columns
221+
max_width_share = 0.382,
222+
215223
-- Value of 'winblend' option
216224
winblend = 25,
217225
},
@@ -553,6 +561,7 @@ H.setup_config = function(config)
553561
['lsp_progress.enable'] = { config.lsp_progress.enable, 'boolean' },
554562
['lsp_progress.duration_last'] = { config.lsp_progress.duration_last, 'number' },
555563
['window.config'] = { config.window.config, is_table_or_callable, 'table or callable' },
564+
['window.max_width_share'] = { config.window.max_width_share, 'number' },
556565
['window.winblend'] = { config.window.winblend, 'number' },
557566
})
558567

@@ -692,7 +701,7 @@ H.buffer_refresh = function(buf_id, notif_arr)
692701
end
693702
end
694703

695-
H.buffer_default_dimensions = function(buf_id)
704+
H.buffer_default_dimensions = function(buf_id, max_width_share)
696705
local line_widths = vim.tbl_map(vim.fn.strdisplaywidth, vim.api.nvim_buf_get_lines(buf_id, 0, -1, true))
697706

698707
-- Compute width so as to fit all lines
@@ -701,7 +710,9 @@ H.buffer_default_dimensions = function(buf_id)
701710
width = math.max(width, l_w)
702711
end
703712
-- - Limit from above for better visuals
704-
width = math.min(width, math.floor(0.382 * vim.o.columns))
713+
max_width_share = math.min(math.max(max_width_share, 0), 1)
714+
local max_width = math.max(math.floor(max_width_share * vim.o.columns), 1)
715+
width = math.min(width, max_width)
705716

706717
-- Compute height based on the width so as to fit all lines with 'wrap' on
707718
local height = 0
@@ -734,14 +745,15 @@ H.window_compute_config = function(buf_id, is_for_open)
734745
local max_height = vim.o.lines - vim.o.cmdheight - (has_tabline and 1 or 0) - (has_statusline and 1 or 0)
735746
local max_width = vim.o.columns
736747

748+
local config_win = H.get_config().window
737749
local default_config = { relative = 'editor', style = 'minimal', noautocmd = is_for_open, zindex = 999 }
738750
default_config.anchor, default_config.col, default_config.row = 'NE', vim.o.columns, has_tabline and 1 or 0
739-
default_config.width, default_config.height = H.buffer_default_dimensions(buf_id)
751+
default_config.width, default_config.height = H.buffer_default_dimensions(buf_id, config_win.max_width_share)
740752
default_config.border = 'single'
741753
-- Don't allow focus to not disrupt window navigation
742754
default_config.focusable = false
743755

744-
local win_config = H.get_config().window.config
756+
local win_config = config_win.config
745757
if vim.is_callable(win_config) then win_config = win_config(buf_id) end
746758
local config = vim.tbl_deep_extend('force', default_config, win_config or {})
747759

readmes/mini-notify.md

+3
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,9 @@ Here are code snippets for some common installation methods (use only one):
176176
-- Floating window config
177177
config = {},
178178

179+
-- Maximum window width as share (between 0 and 1) of available columns
180+
max_width_share = 0.382,
181+
179182
-- Value of 'winblend' option
180183
winblend = 25,
181184
},
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
--|---------|---------|---------|---------|-----
2+
01| ┌─────────────────────────────────┐
3+
02|~ │12:34:56 │ A very-very-very-very-│
4+
03|~ │very long notification │
5+
04|~ └─────────────────────────────────┘
6+
05|~
7+
06|~
8+
07|~
9+
08|~
10+
09|~
11+
10|~
12+
11|~
13+
12|
14+
15+
--|---------|---------|---------|---------|-----
16+
01|000000000011111111111111111111111111111111111
17+
02|222222222213333333333333333333333333333333331
18+
03|222222222213333333333333333333333333333333331
19+
04|222222222211111111111111111111111111111111111
20+
05|222222222222222222222222222222222222222222222
21+
06|222222222222222222222222222222222222222222222
22+
07|222222222222222222222222222222222222222222222
23+
08|222222222222222222222222222222222222222222222
24+
09|222222222222222222222222222222222222222222222
25+
10|222222222222222222222222222222222222222222222
26+
11|222222222222222222222222222222222222222222222
27+
12|444444444444444444444444444444444444444444444
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
--|---------|---------|---------|---------|-----
2+
01|┌───────────────────────────────────────────┐
3+
02|│12:34:56 │ A very-very-very-very-very long │
4+
03|│notification │
5+
04|└───────────────────────────────────────────┘
6+
05|~
7+
06|~
8+
07|~
9+
08|~
10+
09|~
11+
10|~
12+
11|~
13+
12|
14+
15+
--|---------|---------|---------|---------|-----
16+
01|000000000000000000000000000000000000000000000
17+
02|011111111111111111111111111111111111111111110
18+
03|011111111111111111111111111111111111111111110
19+
04|000000000000000000000000000000000000000000000
20+
05|222222222222222222222222222222222222222222222
21+
06|222222222222222222222222222222222222222222222
22+
07|222222222222222222222222222222222222222222222
23+
08|222222222222222222222222222222222222222222222
24+
09|222222222222222222222222222222222222222222222
25+
10|222222222222222222222222222222222222222222222
26+
11|222222222222222222222222222222222222222222222
27+
12|333333333333333333333333333333333333333333333
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
--|---------|---------|---------|---------|-----
2+
01|┌───────────────────────────────────────────┐
3+
02|│12:34:56 │ A very-very-very-very-very long │
4+
03|│notification │
5+
04|└───────────────────────────────────────────┘
6+
05|~
7+
06|~
8+
07|~
9+
08|~
10+
09|~
11+
10|~
12+
11|~
13+
12|
14+
15+
--|---------|---------|---------|---------|-----
16+
01|000000000000000000000000000000000000000000000
17+
02|011111111111111111111111111111111111111111110
18+
03|011111111111111111111111111111111111111111110
19+
04|000000000000000000000000000000000000000000000
20+
05|222222222222222222222222222222222222222222222
21+
06|222222222222222222222222222222222222222222222
22+
07|222222222222222222222222222222222222222222222
23+
08|222222222222222222222222222222222222222222222
24+
09|222222222222222222222222222222222222222222222
25+
10|222222222222222222222222222222222222222222222
26+
11|222222222222222222222222222222222222222222222
27+
12|333333333333333333333333333333333333333333333
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
--|---------|---------|---------|---------|-----
2+
01| ┌─┐
3+
02|~ │1│
4+
03|~ │2│
5+
04|~ │:│
6+
05|~ │3│
7+
06|~ │4│
8+
07|~ │:│
9+
08|~ │5│
10+
09|~ │6│
11+
10|~ │ │
12+
11|~ └─┘
13+
12|
14+
15+
--|---------|---------|---------|---------|-----
16+
01|000000000000000000000000000000000000000000111
17+
02|222222222222222222222222222222222222222222131
18+
03|222222222222222222222222222222222222222222131
19+
04|222222222222222222222222222222222222222222131
20+
05|222222222222222222222222222222222222222222131
21+
06|222222222222222222222222222222222222222222131
22+
07|222222222222222222222222222222222222222222131
23+
08|222222222222222222222222222222222222222222131
24+
09|222222222222222222222222222222222222222222131
25+
10|222222222222222222222222222222222222222222131
26+
11|222222222222222222222222222222222222222222111
27+
12|444444444444444444444444444444444444444444444

tests/test_notify.lua

+20
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,7 @@ T['setup()']['creates `config` field'] = function()
105105
expect_config('lsp_progress.duration_last', 1000)
106106

107107
expect_config('window.config', {})
108+
expect_config('window.max_width_share', 0.382)
108109
expect_config('window.winblend', 25)
109110
end
110111

@@ -132,6 +133,7 @@ T['setup()']['validates `config` argument'] = function()
132133

133134
expect_config_error({ window = 'a' }, 'window', 'table')
134135
expect_config_error({ window = { config = 'a' } }, 'window.config', 'table or callable')
136+
expect_config_error({ window = { max_width_share = 'a' } }, 'window.max_width_share', 'number')
135137
expect_config_error({ window = { winblend = 'a' } }, 'window.winblend', 'number')
136138
end
137139

@@ -758,6 +760,24 @@ T['Window']['respects `window.config`'] = function()
758760
child.expect_screenshot()
759761
end
760762

763+
T['Window']['respects `window.max_width_share`'] = function()
764+
child.lua('MiniNotify.config.window.max_width_share = 0.75')
765+
add('A very-very-very-very-very long notification')
766+
child.expect_screenshot()
767+
child.lua('MiniNotify.config.window.max_width_share = 1')
768+
refresh()
769+
child.expect_screenshot()
770+
771+
-- Handles out of range values
772+
child.lua('MiniNotify.config.window.max_width_share = 10')
773+
refresh()
774+
child.expect_screenshot()
775+
776+
child.lua('MiniNotify.config.window.max_width_share = 0')
777+
refresh()
778+
child.expect_screenshot()
779+
end
780+
761781
T['Window']['respects `window.winblend`'] = function()
762782
local validate_winblend = function(ref)
763783
local win_id = get_notif_win_id()

0 commit comments

Comments
 (0)