summaryrefslogtreecommitdiff
path: root/state.lua
diff options
context:
space:
mode:
Diffstat (limited to 'state.lua')
-rw-r--r--state.lua18
1 files changed, 10 insertions, 8 deletions
diff --git a/state.lua b/state.lua
index 8bf4ddd..41c317c 100644
--- a/state.lua
+++ b/state.lua
@@ -85,8 +85,9 @@ end
function menu_mt:set_cursor(pos, lines, opts)
local pos = math.max(1, math.min(pos, #self.options))
local top = self.view_top
+ local margin = 0
- if not lines then
+ if not lines or lines < 2 then
top = pos
goto update
end
@@ -95,15 +96,16 @@ function menu_mt:set_cursor(pos, lines, opts)
top = pos - math.floor((lines - 1) / 2)
elseif opts and opts.keep_offset then
top = top + pos - self.cursor
+ elseif opts and opts.margin then
+ margin = math.max(0, math.min(
+ opts.margin,
+ math.floor((lines - 1) / 2),
+ self.cursor - self.view_top,
+ self.view_top + lines - 1 - self.cursor))
end
- -- move view to keep selected option visible
- if pos < top then
- top = pos
- elseif pos > top + lines - 1 then
- top = pos - lines + 1
- end
-
+ -- keep selected option visible and view in bounds
+ top = math.max(pos - lines + 1 + margin, math.min(top, pos - margin))
top = math.max(1, math.min(top, #self.options - lines + 1))
::update::