feat: layout anchor (#1582)

* feat: add `anchor` option to some `layout_strategies`

* tests: improve tests for `resolve_height/width`
This commit is contained in:
Luke Kershaw
2021-12-10 19:08:24 +00:00
committed by GitHub
parent 5e5351ef13
commit 5f37fbfa83
4 changed files with 223 additions and 7 deletions

View File

@@ -213,6 +213,35 @@ resolver.resolve_width = function(val)
error("invalid configuration option for width:" .. tostring(val))
end
--- Calculates the adjustment required to move the picker from the middle of the screen to
--- an edge or corner. <br>
--- The `anchor` can be any of the following strings:
--- - "", "CENTER", "NW", "N", "NE", "E", "SE", "S", "SW", "W"
--- The anchors have the following meanings:
--- - "" or "CENTER":<br>
--- the picker will remain in the middle of the screen.
--- - Compass directions:<br>
--- the picker will move to the corresponding edge/corner
--- e.g. "NW" -> "top left corner", "E" -> "right edge", "S" -> "bottom edge"
resolver.resolve_anchor_pos = function(anchor, p_width, p_height, max_columns, max_lines)
anchor = anchor:upper()
local pos = { 0, 0 }
if anchor == "CENTER" then
return pos
end
if anchor:find "W" then
pos[1] = math.ceil((p_width - max_columns) / 2) + 1
elseif anchor:find "E" then
pos[1] = math.ceil((max_columns - p_width) / 2) - 1
end
if anchor:find "N" then
pos[2] = math.ceil((p_height - max_lines) / 2) + 1
elseif anchor:find "S" then
pos[2] = math.ceil((max_lines - p_height) / 2) - 1
end
return pos
end
-- Win option always returns a table with preview, results, and prompt.
-- It handles many different ways. Some examples are as follows:
--