Skip to content

Commit

Permalink
(mini.starter) Update gen_hook.aligning() to respect not-current wi…
Browse files Browse the repository at this point in the history
…ndow dimensions.
  • Loading branch information
echasnovski committed Oct 21, 2022
1 parent 98fa037 commit 8087764
Show file tree
Hide file tree
Showing 5 changed files with 102 additions and 9 deletions.
8 changes: 5 additions & 3 deletions doc/mini-starter.txt
Original file line number Diff line number Diff line change
Expand Up @@ -439,9 +439,11 @@ Return~
Hook generator for aligning content

Output is a content hook which independently aligns content horizontally
and vertically. Basically, this computes left and top pads for
|MiniStarter.gen_hook.padding| such that output lines would appear aligned
in certain way.
and vertically. Window width and height are taken from first window in current
tabpage displaying the starter buffer.

Basically, this computes left and top pads for |MiniStarter.gen_hook.padding|
such that output lines would appear aligned in certain way.

Parameters~
{horizontal} `(string)` One of "left", "center", "right". Default: "left".
Expand Down
17 changes: 11 additions & 6 deletions lua/mini/starter.lua
Original file line number Diff line number Diff line change
Expand Up @@ -693,9 +693,11 @@ end
--- Hook generator for aligning content
---
--- Output is a content hook which independently aligns content horizontally
--- and vertically. Basically, this computes left and top pads for
--- |MiniStarter.gen_hook.padding| such that output lines would appear aligned
--- in certain way.
--- and vertically. Window width and height are taken from first window in current
--- tabpage displaying the starter buffer.
---
--- Basically, this computes left and top pads for |MiniStarter.gen_hook.padding|
--- such that output lines would appear aligned in certain way.
---
---@param horizontal string One of "left", "center", "right". Default: "left".
---@param vertical string One of "top", "center", "bottom". Default: "top".
Expand All @@ -708,17 +710,20 @@ MiniStarter.gen_hook.aligning = function(horizontal, vertical)
local horiz_coef = ({ left = 0, center = 0.5, right = 1.0 })[horizontal]
local vert_coef = ({ top = 0, center = 0.5, bottom = 1.0 })[vertical]

return function(content, _)
return function(content, buf_id)
local win_id = vim.fn.bufwinid(buf_id)
if win_id < 0 then return end

local line_strings = MiniStarter.content_to_lines(content)

-- Align horizontally
-- Don't use `string.len()` to account for multibyte characters
local lines_width = vim.tbl_map(function(l) return vim.fn.strdisplaywidth(l) end, line_strings)
local min_right_space = vim.api.nvim_win_get_width(0) - math.max(unpack(lines_width))
local min_right_space = vim.api.nvim_win_get_width(win_id) - math.max(unpack(lines_width))
local left_pad = math.max(math.floor(horiz_coef * min_right_space), 0)

-- Align vertically
local bottom_space = vim.api.nvim_win_get_height(0) - #line_strings
local bottom_space = vim.api.nvim_win_get_height(win_id) - #line_strings
local top_pad = math.max(math.floor(vert_coef * bottom_space), 0)

return MiniStarter.gen_hook.padding(left_pad, top_pad)(content)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
--|---------|---------|---------|---------|
01| │
02|~ │
03|~ │
04|~ │
05|~ │
06|~ │ AAA
07|<No Name] 1,0-1 All│ aaa
08| │ bbb
09|~ │~
10|~ │~
11|~ │~
12|~ │~
13|~ │~
14|<No Name] 1,0-1 All Starter 7,9 All
15|(mini.starter) Query:

--|---------|---------|---------|---------|
01|0000000000000000000100000000000000000000
02|2222222222222222222100000000000000000000
03|2222222222222222222100000000000000000000
04|2222222222222222222100000000000000000000
05|2222222222222222222100000000000000000000
06|2222222222222222222100000000333000000000
07|4444444444444444444100000000500000000000
08|0000000000000000000100000000500000000000
09|2222222222222222222122222222222222222222
10|2222222222222222222122222222222222222222
11|2222222222222222222122222222222222222222
12|2222222222222222222122222222222222222222
13|2222222222222222222122222222222222222222
14|4444444444444444444466666666666666666666
15|5555555555555551111111111111111111111111
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
--|---------|---------|---------|---------|
01| │
02|~ │
03|~ │
04|~ │
05|~ │
06|~ │ AAA
07|<] 1,│ aaa
08| │ bbb
09|~ │~
10|~ │~
11|~ │~
12|~ │~
13|~ │~
14|<] 1, Starter 7,16 All
15|(mini.starter) Query:

--|---------|---------|---------|---------|
01|0000010000000000000000000000000000000000
02|2222210000000000000000000000000000000000
03|2222210000000000000000000000000000000000
04|2222210000000000000000000000000000000000
05|2222210000000000000000000000000000000000
06|2222210000000000000003330000000000000000
07|4444410000000000000005000000000000000000
08|0000010000000000000005000000000000000000
09|2222212222222222222222222222222222222222
10|2222212222222222222222222222222222222222
11|2222212222222222222222222222222222222222
12|2222212222222222222222222222222222222222
13|2222212222222222222222222222222222222222
14|6666666666666666666666666666666666666666
15|5555555555555551111111111111111111111111
20 changes: 20 additions & 0 deletions tests/test_starter.lua
Original file line number Diff line number Diff line change
Expand Up @@ -736,6 +736,26 @@ T['gen_hook']['aligning()']['handles small windows'] = function()
child.expect_screenshot()
end

T['gen_hook']['aligning()']['has output respecting `buf_id` argument'] = function()
child.set_size(15, 40)
reload_from_strconfig({
content_hooks = [[{ MiniStarter.gen_hook.aligning('center', 'center') }]],
header = [['']],
footer = [['']],
items = ('{ %s, %s }'):format(mock_itemstring('aaa', 'AAA'), mock_itemstring('bbb', 'AAA')),
})

child.cmd('vsplit | split | wincmd l')
child.lua('MiniStarter.open()')
local starter_buf_id = child.api.nvim_get_current_buf()
child.expect_screenshot()

child.cmd('wincmd h')
child.api.nvim_win_set_width(0, 5)
child.lua('MiniStarter.refresh(...)', { starter_buf_id })
child.expect_screenshot()
end

local reload_indexing = function(args)
local itemstrings = {
mock_itemstring('a', 'AAA'),
Expand Down

0 comments on commit 8087764

Please sign in to comment.