clean

Ayman Bagabas created

Change summary

go.sum                          |  2 --
ui/common/component.go          |  7 +------
ui/pages/repo/repo.go           | 17 ++++++++++++++++-
ui/pages/selection/selection.go | 20 ++++++++++++++++----
ui/ui.go                        | 12 ++++++------
5 files changed, 39 insertions(+), 19 deletions(-)

Detailed changes

go.sum 🔗

@@ -25,8 +25,6 @@ github.com/caarlos0/env/v6 v6.9.1 h1:zOkkjM0F6ltnQ5eBX6IPI41UP/KDGEK7rRPwGCNos8k
 github.com/caarlos0/env/v6 v6.9.1/go.mod h1:hvp/ryKXKipEkcuYjs9mI4bBCg+UI0Yhgm5Zu0ddvwc=
 github.com/caarlos0/sshmarshal v0.0.0-20220308164159-9ddb9f83c6b3 h1:w2ANoiT4ubmh4Nssa3/QW1M7lj3FZkma8f8V5aBDxXM=
 github.com/caarlos0/sshmarshal v0.0.0-20220308164159-9ddb9f83c6b3/go.mod h1:7Pd/0mmq9x/JCzKauogNjSQEhivBclCQHfr9dlpDIyA=
-github.com/charmbracelet/bubbles v0.10.4-0.20220429162018-2a8d463bd11f h1:5mbyuBNzjF1S1pJOGubmjMBNUVO3NmjNsaZPIsFwPUQ=
-github.com/charmbracelet/bubbles v0.10.4-0.20220429162018-2a8d463bd11f/go.mod h1:jOA+DUF1rjZm7gZHcNyIVW+YrBPALKfpGVdJu8UiJsA=
 github.com/charmbracelet/bubbles v0.10.4-0.20220524120539-e1871db6d35e h1:B+COqRjrl6G/X6v7salT4LDoSl+2Kz3g4OuDaoZCMwA=
 github.com/charmbracelet/bubbles v0.10.4-0.20220524120539-e1871db6d35e/go.mod h1:jOA+DUF1rjZm7gZHcNyIVW+YrBPALKfpGVdJu8UiJsA=
 github.com/charmbracelet/bubbletea v0.19.3/go.mod h1:VuXF2pToRxDUHcBUcPmCRUHRvFATM4Ckb/ql1rBl3KA=

ui/common/component.go 🔗

@@ -8,11 +8,6 @@ import (
 // Component represents a Bubble Tea model that implements a SetSize function.
 type Component interface {
 	tea.Model
-	SetSize(width, height int)
-}
-
-// Page represents a component that implements help.KeyMap.
-type Page interface {
-	Component
 	help.KeyMap
+	SetSize(width, height int)
 }

ui/pages/repo/repo.go 🔗

@@ -24,8 +24,19 @@ const (
 	commitsTab
 	branchesTab
 	tagsTab
+	lastTab
 )
 
+func (t tab) String() string {
+	return []string{
+		"Readme",
+		"Files",
+		"Commits",
+		"Branches",
+		"Tags",
+	}[t]
+}
+
 // UpdateStatusBarMsg updates the status bar.
 type UpdateStatusBarMsg struct{}
 
@@ -51,8 +62,12 @@ type Repo struct {
 // New returns a new Repo.
 func New(cfg *config.Config, rs git.GitRepoSource, c common.Common) *Repo {
 	sb := statusbar.New(c)
+	ts := make([]string, lastTab)
 	// Tabs must match the order of tab constants above.
-	tb := tabs.New(c, []string{"Readme", "Files", "Commits", "Branches", "Tags"})
+	for i, t := range []tab{readmeTab, filesTab, commitsTab, branchesTab, tagsTab} {
+		ts[i] = t.String()
+	}
+	tb := tabs.New(c, ts)
 	readme := NewReadme(c)
 	log := NewLog(c)
 	files := NewFiles(c)

ui/pages/selection/selection.go 🔗

@@ -20,10 +20,18 @@ import (
 type box int
 
 const (
-	readmeBox box = iota
-	selectorBox
+	selectorBox box = iota
+	readmeBox
+	lastBox
 )
 
+func (b box) String() string {
+	return []string{
+		"Repositories",
+		"About",
+	}[b]
+}
+
 // Selection is the model for the selection screen/page.
 type Selection struct {
 	cfg          *config.Config
@@ -38,7 +46,11 @@ type Selection struct {
 
 // New creates a new selection model.
 func New(cfg *config.Config, pk ssh.PublicKey, common common.Common) *Selection {
-	t := tabs.New(common, []string{"About", "Repositories"})
+	ts := make([]string, lastBox)
+	for i, b := range []box{selectorBox, readmeBox} {
+		ts[i] = b.String()
+	}
+	t := tabs.New(common, ts)
 	t.TabSeparator = lipgloss.NewStyle()
 	t.TabInactive = lipgloss.NewStyle().
 		Bold(true).
@@ -52,7 +64,7 @@ func New(cfg *config.Config, pk ssh.PublicKey, common common.Common) *Selection
 		cfg:       cfg,
 		pk:        pk,
 		common:    common,
-		activeBox: readmeBox, // start with the selector focused
+		activeBox: selectorBox, // start with the selector focused
 		tabs:      t,
 	}
 	readme := code.New(common, "", "")

ui/ui.go 🔗

@@ -40,7 +40,7 @@ type UI struct {
 	rs          git.GitRepoSource
 	initialRepo string
 	common      common.Common
-	pages       []common.Page
+	pages       []common.Component
 	activePage  page
 	state       sessionState
 	header      *header.Header
@@ -57,7 +57,7 @@ func New(cfg *config.Config, s ssh.Session, c common.Common, initialRepo string)
 		session:     s,
 		rs:          src,
 		common:      c,
-		pages:       make([]common.Page, 2), // selection & repo
+		pages:       make([]common.Component, 2), // selection & repo
 		activePage:  selectionPage,
 		state:       startState,
 		header:      h,
@@ -163,7 +163,7 @@ func (ui *UI) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
 		ui.SetSize(msg.Width, msg.Height)
 		for i, p := range ui.pages {
 			m, cmd := p.Update(msg)
-			ui.pages[i] = m.(common.Page)
+			ui.pages[i] = m.(common.Component)
 			if cmd != nil {
 				cmds = append(cmds, cmd)
 			}
@@ -183,6 +183,8 @@ func (ui *UI) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
 				ui.activePage = selectionPage
 			}
 		}
+	case repo.RepoMsg:
+		ui.activePage = repoPage
 	case common.ErrorMsg:
 		ui.error = msg
 		ui.state = errorState
@@ -207,7 +209,7 @@ func (ui *UI) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
 	}
 	if ui.state == loadedState {
 		m, cmd := ui.pages[ui.activePage].Update(msg)
-		ui.pages[ui.activePage] = m.(common.Page)
+		ui.pages[ui.activePage] = m.(common.Component)
 		if cmd != nil {
 			cmds = append(cmds, cmd)
 		}
@@ -262,7 +264,6 @@ func (ui *UI) setRepoCmd(rn string) tea.Cmd {
 	return func() tea.Msg {
 		for _, r := range ui.rs.AllRepos() {
 			if r.Repo() == rn {
-				ui.activePage = repoPage
 				return repo.RepoMsg(r)
 			}
 		}
@@ -274,7 +275,6 @@ func (ui *UI) initialRepoCmd(rn string) tea.Cmd {
 	return func() tea.Msg {
 		for _, r := range ui.rs.AllRepos() {
 			if r.Repo() == rn {
-				ui.activePage = repoPage
 				return repo.RepoMsg(r)
 			}
 		}