From 5f37fbfa837dfee7ecd30f388b271f4a71c0a9e0 Mon Sep 17 00:00:00 2001
From: Luke Kershaw <35707277+l-kershaw@users.noreply.github.com>
Date: Fri, 10 Dec 2021 19:08:24 +0000
Subject: feat: layout `anchor` (#1582)
* feat: add `anchor` option to some `layout_strategies`
* tests: improve tests for `resolve_height/width`
---
lua/telescope/config/resolve.lua | 29 +++++++++++++++++++++++++++++
1 file changed, 29 insertions(+)
(limited to 'lua/telescope/config/resolve.lua')
diff --git a/lua/telescope/config/resolve.lua b/lua/telescope/config/resolve.lua
index 9875565..3b47025 100644
--- a/lua/telescope/config/resolve.lua
+++ b/lua/telescope/config/resolve.lua
@@ -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.
+--- 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":
+--- the picker will remain in the middle of the screen.
+--- - Compass directions:
+--- 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:
--
--
cgit v1.2.3