From 58261e230db643ff243dfb466ba5f68995b97d35 Mon Sep 17 00:00:00 2001 From: essentialols Date: Thu, 19 Mar 2026 16:41:38 -0700 Subject: [PATCH] fix(tui): correct cursor tracking and persistence for group reorder K/J When reordering groups with K/J, the cursor now tracks to the group's actual position after rebuilding the flat item list. Previously the cursor moved by a fixed offset of 1, which was incorrect when the adjacent group had expanded sessions (the group header would jump over multiple flat items). Also switches group reorder persistence from saveInstances() to the lighter-weight saveGroupState(), since only group Order values change. Fixes #398 --- internal/ui/home.go | 40 ++++++++++++++++++++++++++++++---------- 1 file changed, 30 insertions(+), 10 deletions(-) diff --git a/internal/ui/home.go b/internal/ui/home.go index e350557e..8378ea1d 100644 --- a/internal/ui/home.go +++ b/internal/ui/home.go @@ -4938,14 +4938,24 @@ func (h *Home) handleMainKey(msg tea.KeyMsg) (tea.Model, tea.Cmd) { switch item.Type { case session.ItemTypeGroup: h.groupTree.MoveGroupUp(item.Path) + h.rebuildFlatItems() + // Track cursor to the group's new position (it may have jumped + // over multiple flat items if the sibling above was expanded). + for i, fi := range h.flatItems { + if fi.Type == session.ItemTypeGroup && fi.Path == item.Path { + h.cursor = i + break + } + } + h.saveGroupState() case session.ItemTypeSession: h.groupTree.MoveSessionUp(item.Session) + h.rebuildFlatItems() + if h.cursor > 0 { + h.cursor-- + } + h.saveInstances() } - h.rebuildFlatItems() - if h.cursor > 0 { - h.cursor-- - } - h.saveInstances() } return h, nil @@ -4956,14 +4966,24 @@ func (h *Home) handleMainKey(msg tea.KeyMsg) (tea.Model, tea.Cmd) { switch item.Type { case session.ItemTypeGroup: h.groupTree.MoveGroupDown(item.Path) + h.rebuildFlatItems() + // Track cursor to the group's new position (it may have jumped + // over multiple flat items if the sibling below was expanded). + for i, fi := range h.flatItems { + if fi.Type == session.ItemTypeGroup && fi.Path == item.Path { + h.cursor = i + break + } + } + h.saveGroupState() case session.ItemTypeSession: h.groupTree.MoveSessionDown(item.Session) + h.rebuildFlatItems() + if h.cursor < len(h.flatItems)-1 { + h.cursor++ + } + h.saveInstances() } - h.rebuildFlatItems() - if h.cursor < len(h.flatItems)-1 { - h.cursor++ - } - h.saveInstances() } return h, nil