summaryrefslogtreecommitdiff
path: root/state.lua
diff options
context:
space:
mode:
Diffstat (limited to 'state.lua')
-rw-r--r--state.lua21
1 files changed, 13 insertions, 8 deletions
diff --git a/state.lua b/state.lua
index 8bf4ddd..da26a9a 100644
--- a/state.lua
+++ b/state.lua
@@ -85,25 +85,30 @@ 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
+ if opts and opts.margin then
+ margin = math.min(opts.margin, math.floor((lines - 1) / 2))
+ end
+
if opts and opts.centre then
top = pos - math.floor((lines - 1) / 2)
elseif opts and opts.keep_offset then
top = top + pos - self.cursor
+ elseif margin > 0 then
+ margin = math.max(0, math.min(
+ margin,
+ 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::