diff --git a/internal/ui/dialog/sessions.go b/internal/ui/dialog/sessions.go index b70e4de585915c08e29d2dec5ed127b89610296a..daeec6e208dee88883b7d86282769093fef67858 100644 --- a/internal/ui/dialog/sessions.go +++ b/internal/ui/dialog/sessions.go @@ -16,11 +16,12 @@ const SessionsID = "session" // Session is a session selector dialog. type Session struct { - width, height int - com *common.Common - help help.Model - list *list.FilterableList - input textinput.Model + width, height int + com *common.Common + help help.Model + list *list.FilterableList + input textinput.Model + selectedSessionInx int keyMap struct { Select key.Binding @@ -33,7 +34,7 @@ type Session struct { var _ Dialog = (*Session)(nil) // NewSessions creates a new Session dialog. -func NewSessions(com *common.Common) (*Session, error) { +func NewSessions(com *common.Common, selectedSessionID string) (*Session, error) { s := new(Session) s.com = com sessions, err := com.App.Sessions.List(context.TODO()) @@ -41,13 +42,19 @@ func NewSessions(com *common.Common) (*Session, error) { return nil, err } + for i, sess := range sessions { + if sess.ID == selectedSessionID { + s.selectedSessionInx = i + break + } + } + help := help.New() help.Styles = com.Styles.DialogHelpStyles() s.help = help s.list = list.NewFilterableList(sessionItems(com.Styles, sessions...)...) s.list.Focus() - s.list.SetSelected(0) s.input = textinput.New() s.input.SetVirtualCursor(false) @@ -85,6 +92,10 @@ func (s *Session) SetSize(width, height int) { s.input.SetWidth(innerWidth - t.Dialog.InputPrompt.GetHorizontalFrameSize() - 1) // (1) cursor padding s.list.SetSize(innerWidth, height-heightOffset) s.help.SetWidth(width) + + // Now that we know the height we can select the selected session and scroll to it. + s.list.SetSelected(s.selectedSessionInx) + s.list.ScrollToSelected() } // ID implements Dialog. diff --git a/internal/ui/model/ui.go b/internal/ui/model/ui.go index ca79415617ebce0babbe881101402d9424743f03..d7df5940c89dfeb595a5a7aa1077fb3d56e1324a 100644 --- a/internal/ui/model/ui.go +++ b/internal/ui/model/ui.go @@ -1440,7 +1440,12 @@ func (m *UI) openSessionsDialog() tea.Cmd { return nil } - dialog, err := dialog.NewSessions(m.com) + selectedSessionID := "" + if m.session != nil { + selectedSessionID = m.session.ID + } + + dialog, err := dialog.NewSessions(m.com, selectedSessionID) if err != nil { return uiutil.ReportError(err) }