Skip to content

Commit 35da9ca

Browse files
committed
feat: support decimal number in (0,1) range as size
1 parent 80445d0 commit 35da9ca

File tree

4 files changed

+95
-27
lines changed

4 files changed

+95
-27
lines changed

lua/nui/layout/README.md

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,9 @@ You can also pass a table to set them separately.
147147
For `percentage string`, `size` is calculated according to the option `relative`.
148148
If `relative` is set to `"buf"` or `"cursor"`, window size is considered.
149149

150+
Decimal `number` in `(0,1)` range is treated similar to `percentage string`. For
151+
example: `0.5` is same as `"50%"`.
152+
150153
**Examples**
151154

152155
```lua
@@ -157,6 +160,10 @@ size = 50,
157160
size = "50%",
158161
```
159162

163+
```lua
164+
size = 0.5,
165+
```
166+
160167
```lua
161168
size = {
162169
width = 80,
@@ -167,7 +174,7 @@ size = {
167174
```lua
168175
size = {
169176
width = "80%",
170-
height = "60%",
177+
height = 0.6,
171178
},
172179
```
173180

lua/nui/popup/README.md

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -291,6 +291,9 @@ You can also pass a table to set them separately.
291291
For `percentage string`, `size` is calculated according to the option `relative`.
292292
If `relative` is set to `"buf"` or `"cursor"`, window size is considered.
293293

294+
Decimal `number` in `(0,1)` range is treated similar to `percentage string`. For
295+
example: `0.5` is same as `"50%"`.
296+
294297
**Examples**
295298

296299
```lua
@@ -301,6 +304,10 @@ size = 50,
301304
size = "50%",
302305
```
303306

307+
```lua
308+
size = 0.5,
309+
```
310+
304311
```lua
305312
size = {
306313
width = 80,
@@ -311,7 +318,7 @@ size = {
311318
```lua
312319
size = {
313320
width = "80%",
314-
height = "60%",
321+
height = 0.6,
315322
},
316323
```
317324

lua/nui/utils/init.lua

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ function utils.parse_number_input(v)
6161
parsed.value = tonumber(string.sub(v, 1, #v - 1)) / 100
6262
else
6363
parsed.value = tonumber(v)
64+
parsed.is_percentage = parsed.value and 0 < parsed.value and parsed.value < 1
6465
end
6566

6667
return parsed

tests/nui/popup/init_spec.lua

Lines changed: 78 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,25 @@ local function percent(number, percentage)
1111
return math.floor(number * percentage / 100)
1212
end
1313

14+
local function assert_size(popup, size, border_size)
15+
if border_size and type(border_size) ~= "table" then
16+
border_size = {
17+
width = size.width + 2,
18+
height = size.height + 2,
19+
}
20+
end
21+
22+
local win_config = vim.api.nvim_win_get_config(popup.winid)
23+
eq(win_config.width, size.width)
24+
eq(win_config.height, size.height)
25+
26+
if popup.border.winid then
27+
local border_win_config = vim.api.nvim_win_get_config(popup.border.winid)
28+
eq(border_win_config.width, border_size.width)
29+
eq(border_win_config.height, border_size.height)
30+
end
31+
end
32+
1433
describe("nui.popup", function()
1534
local popup
1635

@@ -94,6 +113,59 @@ describe("nui.popup", function()
94113
eq(popup.ns_id > 0, true)
95114
end)
96115

116+
describe("o.size", function()
117+
it("supports integer", function()
118+
popup = Popup({
119+
position = 0,
120+
size = {
121+
height = 8,
122+
width = 20,
123+
},
124+
})
125+
126+
popup:mount()
127+
128+
assert_size(popup, {
129+
height = 8,
130+
width = 20,
131+
})
132+
end)
133+
134+
it("supports decimal number in (0,1)", function()
135+
popup = Popup({
136+
position = 0,
137+
size = {
138+
height = 0.6,
139+
width = 0.8,
140+
},
141+
})
142+
143+
popup:mount()
144+
145+
assert_size(popup, {
146+
height = percent(popup._.container_info.size.height, 60),
147+
width = percent(popup._.container_info.size.width, 80),
148+
})
149+
end)
150+
151+
it("supports percentage string", function()
152+
popup = Popup({
153+
position = 0,
154+
size = {
155+
height = "60%",
156+
width = "80%",
157+
},
158+
})
159+
160+
popup:mount()
161+
162+
assert_size(popup, {
163+
height = percent(popup._.container_info.size.height, 60),
164+
width = percent(popup._.container_info.size.width, 80),
165+
})
166+
end)
167+
end)
168+
97169
h.describe_flipping_feature("lua_keymap", "method :map", function()
98170
it("works before :mount", function()
99171
local callback = spy.new(function() end)
@@ -545,25 +617,6 @@ describe("nui.popup", function()
545617
end)
546618

547619
describe("method :update_layout", function()
548-
local function assert_size(size, border_size)
549-
if border_size and type(border_size) ~= "table" then
550-
border_size = {
551-
width = size.width + 2,
552-
height = size.height + 2,
553-
}
554-
end
555-
556-
local win_config = vim.api.nvim_win_get_config(popup.winid)
557-
eq(win_config.width, size.width)
558-
eq(win_config.height, size.height)
559-
560-
if popup.border.winid then
561-
local border_win_config = vim.api.nvim_win_get_config(popup.border.winid)
562-
eq(border_win_config.width, border_size.width)
563-
eq(border_win_config.height, border_size.height)
564-
end
565-
end
566-
567620
local function assert_position(position, container_winid)
568621
container_winid = container_winid or vim.api.nvim_get_current_win()
569622

@@ -653,7 +706,7 @@ describe("nui.popup", function()
653706

654707
eq(type(popup.border.winid), "nil")
655708

656-
assert_size(size)
709+
assert_size(popup, size)
657710

658711
local new_size = {
659712
width = size.width + 2,
@@ -662,7 +715,7 @@ describe("nui.popup", function()
662715

663716
popup:update_layout({ size = new_size })
664717

665-
assert_size(new_size)
718+
assert_size(popup, new_size)
666719
end)
667720

668721
it("can change size (w/ complex border)", function()
@@ -688,7 +741,7 @@ describe("nui.popup", function()
688741

689742
eq(type(popup.border.winid), "number")
690743

691-
assert_size(size, true)
744+
assert_size(popup, size, true)
692745
h.popup.assert_border_lines({
693746
size = size,
694747
border = { style = style },
@@ -705,7 +758,7 @@ describe("nui.popup", function()
705758

706759
popup:update_layout({ size = new_size })
707760

708-
assert_size(new_size, true)
761+
assert_size(popup, new_size, true)
709762
h.popup.assert_border_lines({
710763
size = new_size,
711764
border = { style = style },
@@ -808,7 +861,7 @@ describe("nui.popup", function()
808861

809862
popup:mount()
810863

811-
assert_size({
864+
assert_size(popup, {
812865
width = percent(container_size.width, 50),
813866
height = percent(container_size.height, 50),
814867
})
@@ -829,7 +882,7 @@ describe("nui.popup", function()
829882

830883
popup:update_layout()
831884

832-
assert_size({
885+
assert_size(popup, {
833886
width = percent(container_size.width, 50),
834887
height = percent(container_size.height, 50),
835888
})

0 commit comments

Comments
 (0)