diff --git a/doc/telescope.txt b/doc/telescope.txt index 15d957a..a12bb09 100644 --- a/doc/telescope.txt +++ b/doc/telescope.txt @@ -1947,14 +1947,17 @@ Provides "resolver functions" to allow more customisable inputs for options. resolver.resolve_height() *telescope.resolve.resolve_height()* Converts input to a function that returns the height. The input must take - one of four forms: + one of five forms: 1. 0 <= number < 1 This means total height as a percentage. 2. 1 <= number This means total height as a fixed number. 3. function Must have signature: function(self, max_columns, max_lines): number - 4. table of the form: {padding = `foo`} + 4. table of the form: { val, max = ..., min = ... } + 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`} where `foo` has one of the previous three forms. 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}, @@ -1967,14 +1970,17 @@ resolver.resolve_height() *telescope.resolve.resolve_height()* resolver.resolve_width() *telescope.resolve.resolve_width()* Converts input to a function that returns the width. The input must take - one of four forms: + one of five forms: 1. 0 <= number < 1 This means total width as a percentage. 2. 1 <= number This means total width as a fixed number. 3. function Must have signature: function(self, max_columns, max_lines): number - 4. table of the form: {padding = `foo`} + 4. table of the form: { val, max = ..., min = ... } + 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`} where `foo` has one of the previous three forms. 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/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
--- This means total height as a percentage. --- 2. 1 <= number
@@ -170,7 +187,10 @@ end --- 3. function
--- Must have signature: --- function(self, max_columns, max_lines): number ---- 4. table of the form: {padding = `foo`}
+--- 4. table of the form: { val, max = ..., min = ... }
+--- 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`}
--- where `foo` has one of the previous three forms.
--- 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
--- This means total width as a percentage. --- 2. 1 <= number
@@ -196,7 +216,10 @@ end --- 3. function
--- Must have signature: --- function(self, max_columns, max_lines): number ---- 4. table of the form: {padding = `foo`}
+--- 4. table of the form: { val, max = ..., min = ... }
+--- 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`}
--- where `foo` has one of the previous three forms.
--- 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