From 66995d17eea2a45a4e8d88a7739db18c13b91bf7 Mon Sep 17 00:00:00 2001 From: Robin Jarry Date: Mon, 10 Apr 2023 16:56:04 +0200 Subject: dialog: avoid panic when window is too small Avoid negative offsets and limit height to the parent context height. Fixes: https://todo.sr.ht/~rjarry/aerc/142 Reported-by: Akspecs Signed-off-by: Robin Jarry Tested-by: Koni Marti --- widgets/selector.go | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) (limited to 'widgets') diff --git a/widgets/selector.go b/widgets/selector.go index deefdfbf..fb8c8094 100644 --- a/widgets/selector.go +++ b/widgets/selector.go @@ -206,8 +206,20 @@ func (gp *SelectorDialog) ContextHeight() (func(int) int, func(int) int) { totalHeight := 2 // title + empty line totalHeight += strings.Count(gp.prompt, "\n") + 1 totalHeight += 2 // empty line + selector - start := func(h int) int { return h/2 - totalHeight/2 } - height := func(h int) int { return totalHeight } + start := func(h int) int { + s := h/2 - totalHeight/2 + if s < 0 { + s = 0 + } + return s + } + height := func(h int) int { + if totalHeight > h { + return h + } else { + return totalHeight + } + } return start, height } -- cgit