summaryrefslogtreecommitdiff
path: root/lua
diff options
context:
space:
mode:
authorSimon Hauser <Simon-Hauser@outlook.de>2022-06-26 11:37:51 +0200
committerSimon Hauser <simon.hauser@helsinki-systems.de>2022-06-30 14:01:52 +0200
commitd1f3e12a353d4d04baa1f9096a0b720c713c37ee (patch)
tree0129bad11e367cff072341f2eb639ff1e2c4d9f1 /lua
parent25b1bc8f17e1b658551cf2435fa2070dc96edc4e (diff)
feat: add min max boundary to width, hight resolver (#2002)
Diffstat (limited to 'lua')
-rw-r--r--lua/telescope/config/resolve.lua37
-rw-r--r--lua/tests/automated/resolver_spec.lua8
2 files changed, 38 insertions, 7 deletions
diff --git a/lua/telescope/config/resolve.lua b/lua/telescope/config/resolve.lua
index 6281a51..ad8936e 100644
--- a/lua/telescope/config/resolve.lua
+++ b/lua/telescope/config/resolve.lua
@@ -127,9 +127,6 @@ end] = function(selector, val)
end
end
--- Tables TODO:
--- ... {70, max}
-
-- function:
-- Function must have same signature as get_window_layout
-- function(self, max_columns, max_lines): number
@@ -141,6 +138,26 @@ end] = function(_, val)
return val
end
+_resolve_map[function(val)
+ return type(val) == "table" and val[1] >= 0 and val[1] < 1 and val["max"] ~= nil
+end] =
+ function(selector, val)
+ return function(...)
+ local selected = select(selector, ...)
+ return math.min(math.floor(val[1] * selected), val["max"])
+ end
+ end
+
+_resolve_map[function(val)
+ return type(val) == "table" and val[1] >= 0 and val[1] < 1 and val["min"] ~= nil
+end] =
+ function(selector, val)
+ return function(...)
+ local selected = select(selector, ...)
+ return math.max(math.floor(val[1] * selected), val["min"])
+ end
+ end
+
-- Add padding option
_resolve_map[function(val)
return type(val) == "table" and val["padding"] ~= nil
@@ -162,7 +179,7 @@ end] = function(selector, val)
end
--- Converts input to a function that returns the height.
---- The input must take one of four forms:
+--- The input must take one of five forms:
--- 1. 0 <= number < 1 <br>
--- This means total height as a percentage.
--- 2. 1 <= number <br>
@@ -170,7 +187,10 @@ end
--- 3. function <br>
--- Must have signature:
--- function(self, max_columns, max_lines): number
---- 4. table of the form: {padding = `foo`} <br>
+--- 4. table of the form: { val, max = ..., min = ... } <br>
+--- val has to be in the first form 0 <= val < 1 and only one is given,
+--- `min` or `max` as fixed number
+--- 5. table of the form: {padding = `foo`} <br>
--- where `foo` has one of the previous three forms. <br>
--- The height is then set to be the remaining space after padding.
--- For example, if the window has height 50, and the input is {padding = 5},
@@ -188,7 +208,7 @@ resolver.resolve_height = function(val)
end
--- Converts input to a function that returns the width.
---- The input must take one of four forms:
+--- The input must take one of five forms:
--- 1. 0 <= number < 1 <br>
--- This means total width as a percentage.
--- 2. 1 <= number <br>
@@ -196,7 +216,10 @@ end
--- 3. function <br>
--- Must have signature:
--- function(self, max_columns, max_lines): number
---- 4. table of the form: {padding = `foo`} <br>
+--- 4. table of the form: { val, max = ..., min = ... } <br>
+--- val has to be in the first form 0 <= val < 1 and only one is given,
+--- `min` or `max` as fixed number
+--- 5. table of the form: {padding = `foo`} <br>
--- where `foo` has one of the previous three forms. <br>
--- The width is then set to be the remaining space after padding.
--- For example, if the window has width 100, and the input is {padding = 5},
diff --git a/lua/tests/automated/resolver_spec.lua b/lua/tests/automated/resolver_spec.lua
index 0251ec0..f30a323 100644
--- a/lua/tests/automated/resolver_spec.lua
+++ b/lua/tests/automated/resolver_spec.lua
@@ -75,6 +75,14 @@ describe("telescope.config.resolve", function()
end
end)
+ it("should handle percentages with min/max boundary", function()
+ eq(20, resolve.resolve_width { 0.1, min = 20 }(nil, 40, 120))
+ eq(30, resolve.resolve_height { 0.1, min = 20 }(nil, 40, 300))
+
+ eq(24, resolve.resolve_width { 0.4, max = 80 }(nil, 60, 60))
+ eq(80, resolve.resolve_height { 0.4, max = 80 }(nil, 60, 300))
+ end)
+
it("should handle fixed size", function()
local fixed = { 5, 8, 13, 21, 34 }
for _, s in ipairs(test_sizes) do