From e419a938f255ce4f29f5b489c5b911db456c3a57 Mon Sep 17 00:00:00 2001 From: Ayman Bagabas Date: Tue, 5 Dec 2023 13:07:02 -0500 Subject: [PATCH] fix(ui): go back on esc Fixes: https://github.com/charmbracelet/soft-serve/issues/415 --- pkg/ssh/ui.go | 4 +++- pkg/ui/common/component.go | 3 +++ pkg/ui/pages/repo/files.go | 9 +++++++++ pkg/ui/pages/repo/log.go | 10 ++++++++++ pkg/ui/pages/repo/readme.go | 5 +++++ pkg/ui/pages/repo/refs.go | 5 +++++ pkg/ui/pages/repo/repo.go | 12 ++++++++++++ pkg/ui/pages/repo/stash.go | 5 +++++ 8 files changed, 52 insertions(+), 1 deletion(-) diff --git a/pkg/ssh/ui.go b/pkg/ssh/ui.go index 3ac6b08ed..4b84542c4 100644 --- a/pkg/ssh/ui.go +++ b/pkg/ssh/ui.go @@ -196,7 +196,9 @@ func (ui *UI) Update(msg tea.Msg) (tea.Model, tea.Cmd) { ui.common.Zone.Close() return ui, tea.Quit } - case ui.activePage == repoPage && key.Matches(msg, ui.common.KeyMap.Back): + case ui.activePage == repoPage && + ui.pages[ui.activePage].(*repo.Repo).Path() == "" && + key.Matches(msg, ui.common.KeyMap.Back): ui.activePage = selectionPage // Always show the footer on selection page. ui.showFooter = true diff --git a/pkg/ui/common/component.go b/pkg/ui/common/component.go index 1f4d20df8..6fe7761f9 100644 --- a/pkg/ui/common/component.go +++ b/pkg/ui/common/component.go @@ -28,4 +28,7 @@ type TabComponent interface { // TabName returns the name of the tab. TabName() string + + // Path returns the hierarchical path of the tab. + Path() string } diff --git a/pkg/ui/pages/repo/files.go b/pkg/ui/pages/repo/files.go index 4d5585364..4499d6f0c 100644 --- a/pkg/ui/pages/repo/files.go +++ b/pkg/ui/pages/repo/files.go @@ -105,6 +105,15 @@ func NewFiles(common common.Common) *Files { return f } +// Path implements common.TabComponent. +func (f *Files) Path() string { + path := f.path + if path == "." { + return "" + } + return path +} + // TabName returns the tab name. func (f *Files) TabName() string { return "Files" diff --git a/pkg/ui/pages/repo/log.go b/pkg/ui/pages/repo/log.go index 292128d96..b4acc122a 100644 --- a/pkg/ui/pages/repo/log.go +++ b/pkg/ui/pages/repo/log.go @@ -83,6 +83,16 @@ func NewLog(common common.Common) *Log { return l } +// Path implements common.TabComponent. +func (l *Log) Path() string { + switch l.activeView { + case logViewCommits: + return "" + default: + return "diff" // XXX: this is a place holder and doesn't mean anything + } +} + // TabName returns the name of the tab. func (l *Log) TabName() string { return "Commits" diff --git a/pkg/ui/pages/repo/readme.go b/pkg/ui/pages/repo/readme.go index 6ecbe9aa9..e977bb916 100644 --- a/pkg/ui/pages/repo/readme.go +++ b/pkg/ui/pages/repo/readme.go @@ -45,6 +45,11 @@ func NewReadme(common common.Common) *Readme { } } +// Path implements common.TabComponent. +func (r *Readme) Path() string { + return "" +} + // TabName returns the name of the tab. func (r *Readme) TabName() string { return "Readme" diff --git a/pkg/ui/pages/repo/refs.go b/pkg/ui/pages/repo/refs.go index e34b30f85..f6d9c56d8 100644 --- a/pkg/ui/pages/repo/refs.go +++ b/pkg/ui/pages/repo/refs.go @@ -57,6 +57,11 @@ func NewRefs(common common.Common, refPrefix string) *Refs { return r } +// Path implements common.TabComponent. +func (r *Refs) Path() string { + return "" +} + // TabName returns the name of the tab. func (r *Refs) TabName() string { if r.refPrefix == git.RefsHeads { diff --git a/pkg/ui/pages/repo/repo.go b/pkg/ui/pages/repo/repo.go index fb5f0a603..535937d4e 100644 --- a/pkg/ui/pages/repo/repo.go +++ b/pkg/ui/pages/repo/repo.go @@ -104,6 +104,11 @@ func (r *Repo) SetSize(width, height int) { } } +// Path returns the current component path. +func (r *Repo) Path() string { + return r.panes[r.activeTab].Path() +} + func (r *Repo) commonHelp() []key.Binding { b := make([]key.Binding, 0) back := r.common.KeyMap.Back @@ -194,6 +199,13 @@ func (r *Repo) Update(msg tea.Msg) (tea.Model, tea.Cmd) { } } } + switch msg := msg.(type) { + case tea.KeyMsg: + switch { + case key.Matches(msg, r.common.KeyMap.Back): + cmds = append(cmds, goBackCmd) + } + } case CopyMsg: txt := msg.Text if cfg := r.common.Config(); cfg != nil { diff --git a/pkg/ui/pages/repo/stash.go b/pkg/ui/pages/repo/stash.go index 2d1fa69ff..37f534a29 100644 --- a/pkg/ui/pages/repo/stash.go +++ b/pkg/ui/pages/repo/stash.go @@ -64,6 +64,11 @@ func NewStash(common common.Common) *Stash { } } +// Path implements common.TabComponent. +func (s *Stash) Path() string { + return "" +} + // TabName returns the name of the tab. func (s *Stash) TabName() string { return "Stash"