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

feat: support decimal number in (0,1) range as size #322

Merged
merged 1 commit into from
Jan 4, 2024
Merged
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
9 changes: 8 additions & 1 deletion lua/nui/layout/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,9 @@ You can also pass a table to set them separately.
For `percentage string`, `size` is calculated according to the option `relative`.
If `relative` is set to `"buf"` or `"cursor"`, window size is considered.

Decimal `number` in `(0,1)` range is treated similar to `percentage string`. For
example: `0.5` is same as `"50%"`.

**Examples**

```lua
Expand All @@ -157,6 +160,10 @@ size = 50,
size = "50%",
```

```lua
size = 0.5,
```

```lua
size = {
width = 80,
Expand All @@ -167,7 +174,7 @@ size = {
```lua
size = {
width = "80%",
height = "60%",
height = 0.6,
},
```

Expand Down
9 changes: 8 additions & 1 deletion lua/nui/popup/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -291,6 +291,9 @@ You can also pass a table to set them separately.
For `percentage string`, `size` is calculated according to the option `relative`.
If `relative` is set to `"buf"` or `"cursor"`, window size is considered.

Decimal `number` in `(0,1)` range is treated similar to `percentage string`. For
example: `0.5` is same as `"50%"`.

**Examples**

```lua
Expand All @@ -301,6 +304,10 @@ size = 50,
size = "50%",
```

```lua
size = 0.5,
```

```lua
size = {
width = 80,
Expand All @@ -311,7 +318,7 @@ size = {
```lua
size = {
width = "80%",
height = "60%",
height = 0.6,
},
```

Expand Down
1 change: 1 addition & 0 deletions lua/nui/utils/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ function utils.parse_number_input(v)
parsed.value = tonumber(string.sub(v, 1, #v - 1)) / 100
else
parsed.value = tonumber(v)
parsed.is_percentage = parsed.value and 0 < parsed.value and parsed.value < 1
end

return parsed
Expand Down
103 changes: 78 additions & 25 deletions tests/nui/popup/init_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,25 @@ local function percent(number, percentage)
return math.floor(number * percentage / 100)
end

local function assert_size(popup, size, border_size)
if border_size and type(border_size) ~= "table" then
border_size = {
width = size.width + 2,
height = size.height + 2,
}
end

local win_config = vim.api.nvim_win_get_config(popup.winid)
eq(win_config.width, size.width)
eq(win_config.height, size.height)

if popup.border.winid then
local border_win_config = vim.api.nvim_win_get_config(popup.border.winid)
eq(border_win_config.width, border_size.width)
eq(border_win_config.height, border_size.height)
end
end

describe("nui.popup", function()
local popup

Expand Down Expand Up @@ -94,6 +113,59 @@ describe("nui.popup", function()
eq(popup.ns_id > 0, true)
end)

describe("o.size", function()
it("supports integer", function()
popup = Popup({
position = 0,
size = {
height = 8,
width = 20,
},
})

popup:mount()

assert_size(popup, {
height = 8,
width = 20,
})
end)

it("supports decimal number in (0,1)", function()
popup = Popup({
position = 0,
size = {
height = 0.6,
width = 0.8,
},
})

popup:mount()

assert_size(popup, {
height = percent(popup._.container_info.size.height, 60),
width = percent(popup._.container_info.size.width, 80),
})
end)

it("supports percentage string", function()
popup = Popup({
position = 0,
size = {
height = "60%",
width = "80%",
},
})

popup:mount()

assert_size(popup, {
height = percent(popup._.container_info.size.height, 60),
width = percent(popup._.container_info.size.width, 80),
})
end)
end)

h.describe_flipping_feature("lua_keymap", "method :map", function()
it("works before :mount", function()
local callback = spy.new(function() end)
Expand Down Expand Up @@ -545,25 +617,6 @@ describe("nui.popup", function()
end)

describe("method :update_layout", function()
local function assert_size(size, border_size)
if border_size and type(border_size) ~= "table" then
border_size = {
width = size.width + 2,
height = size.height + 2,
}
end

local win_config = vim.api.nvim_win_get_config(popup.winid)
eq(win_config.width, size.width)
eq(win_config.height, size.height)

if popup.border.winid then
local border_win_config = vim.api.nvim_win_get_config(popup.border.winid)
eq(border_win_config.width, border_size.width)
eq(border_win_config.height, border_size.height)
end
end

local function assert_position(position, container_winid)
container_winid = container_winid or vim.api.nvim_get_current_win()

Expand Down Expand Up @@ -653,7 +706,7 @@ describe("nui.popup", function()

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

assert_size(size)
assert_size(popup, size)

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

popup:update_layout({ size = new_size })

assert_size(new_size)
assert_size(popup, new_size)
end)

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

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

assert_size(size, true)
assert_size(popup, size, true)
h.popup.assert_border_lines({
size = size,
border = { style = style },
Expand All @@ -705,7 +758,7 @@ describe("nui.popup", function()

popup:update_layout({ size = new_size })

assert_size(new_size, true)
assert_size(popup, new_size, true)
h.popup.assert_border_lines({
size = new_size,
border = { style = style },
Expand Down Expand Up @@ -808,7 +861,7 @@ describe("nui.popup", function()

popup:mount()

assert_size({
assert_size(popup, {
width = percent(container_size.width, 50),
height = percent(container_size.height, 50),
})
Expand All @@ -829,7 +882,7 @@ describe("nui.popup", function()

popup:update_layout()

assert_size({
assert_size(popup, {
width = percent(container_size.width, 50),
height = percent(container_size.height, 50),
})
Expand Down