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 3ac6b08ed7a2ce4afd6b094e520350abb535e9f9..4b84542c47e77147b13be1808880a17729ca715c 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 1f4d20df875384b23e21cc6977fa8e40b4296e6c..6fe7761f9b9c85cabae967b5246d361cb811d363 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 4d55853648b37ee5abd11029cebe33499cb82731..4499d6f0c1fb318e3ffabe62aa32d129de6d9303 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 292128d961f11c5e6f76b1599de912dba645e192..b4acc122a323892efd88972d387fc1824e03ef39 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 6ecbe9aa94ed6eda31b47f8618d4e69671b6d1e9..e977bb916e9479122c891828a80b9c26cc9a76ac 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 e34b30f85b379eeae0b67b9301f3139dc1301382..f6d9c56d84303d66f9e94aa5aa44e51afb367523 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 fb5f0a603c587e2610a86c4a5558246dc011bb2f..535937d4e46bc6cee34ab01aa3b47cf35dbd866f 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 2d1fa69ff6d2e6ea805d4c3d517889312d49819b..37f534a29b1afbac55f6400750a2bf6e902f1eed 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"