From a9850510e32e93888cea730cf2f31104e608ce45 Mon Sep 17 00:00:00 2001 From: Slug-Boi Date: Mon, 25 Nov 2024 22:17:07 +0100 Subject: [PATCH] fix: crash when no groups defined and opening the group panel --- src/cmd/tui/tui_groups.go | 18 +++++++++++++----- 1 file changed, 13 insertions(+), 5 deletions(-) diff --git a/src/cmd/tui/tui_groups.go b/src/cmd/tui/tui_groups.go index f94a317..2293219 100644 --- a/src/cmd/tui/tui_groups.go +++ b/src/cmd/tui/tui_groups.go @@ -53,7 +53,7 @@ func newModel() mainModel { slices.Sort(content) // check if terminal 0 is a terminal - var w,h int + var w, h int var err error if ok := term.IsTerminal(0); ok { // calculate term size @@ -63,14 +63,14 @@ func newModel() mainModel { } } - // 25 is a magic number in terms of height but is roughly based on the element height + // 25 is a magic number in terms of height but is roughly based on the element height // of the group squares if h > 25 { lines = 2 } else { lines = 1 } - + // if the terminal size is 0, then skip if w > 0 { // 30 is a magic number don't question it @@ -78,7 +78,7 @@ func newModel() mainModel { } cap = max(1, cap) - if cap * 2 > len(content) { + if cap*2 > len(content) { cap = len(content) } @@ -89,6 +89,10 @@ func newModel() mainModel { p.PerPage = cap * lines p.SetTotalPages(len(content)) + if len(content) == 0 { + content = append(content, "No groups found") + } + m := mainModel{content: content, paginator: p} return m } @@ -138,7 +142,11 @@ func (m mainModel) Update(msg tea.Msg) (tea.Model, tea.Cmd) { } // Adrian is a fucking genius thanks for the idea :) - m.paginator.Page = m.index / (cap * lines) + if m.index == 0 { + m.paginator.Page = 0 + } else { + m.paginator.Page = m.index / (cap * lines) + } cmds = append(cmds, cmd) return m, tea.Batch(cmds...)