From 76c4049b9f7487f77a5a7679d79296e009ecec1e Mon Sep 17 00:00:00 2001 From: Ayman Bagabas Date: Tue, 24 May 2022 09:59:23 -0400 Subject: [PATCH] fix: simplify ui session struct --- ui/pages/repo/repo.go | 12 ++++++------ ui/pages/selection/selection.go | 15 +++++++++------ ui/session/session.go | 20 ------------------- ui/ui.go | 34 ++++++++++++++++++++++----------- 4 files changed, 38 insertions(+), 43 deletions(-) delete mode 100644 ui/session/session.go diff --git a/ui/pages/repo/repo.go b/ui/pages/repo/repo.go index 7fac9417c32bb57a11559d99a61a435dbdeec37d..71674baa3ab07f421cce66453319d3e83c43213b 100644 --- a/ui/pages/repo/repo.go +++ b/ui/pages/repo/repo.go @@ -7,12 +7,12 @@ import ( "github.com/charmbracelet/bubbles/key" tea "github.com/charmbracelet/bubbletea" "github.com/charmbracelet/lipgloss" + "github.com/charmbracelet/soft-serve/config" ggit "github.com/charmbracelet/soft-serve/git" "github.com/charmbracelet/soft-serve/ui/common" "github.com/charmbracelet/soft-serve/ui/components/statusbar" "github.com/charmbracelet/soft-serve/ui/components/tabs" "github.com/charmbracelet/soft-serve/ui/git" - "github.com/charmbracelet/soft-serve/ui/session" ) type tab int @@ -36,8 +36,8 @@ type RefMsg *ggit.Reference // Repo is a view for a git repository. type Repo struct { - s session.Session common common.Common + cfg *config.Config rs git.GitRepoSource selectedRepo git.GitRepo activeTab tab @@ -48,7 +48,7 @@ type Repo struct { } // New returns a new Repo. -func New(s session.Session, c common.Common) *Repo { +func New(cfg *config.Config, rs git.GitRepoSource, c common.Common) *Repo { sb := statusbar.New(c) // Tabs must match the order of tab constants above. tb := tabs.New(c, []string{"Readme", "Files", "Commits", "Branches", "Tags"}) @@ -66,9 +66,9 @@ func New(s session.Session, c common.Common) *Repo { tags, } r := &Repo{ - s: s, + cfg: cfg, common: c, - rs: s.Source(), + rs: rs, tabs: tb, statusbar: sb, boxes: boxes, @@ -241,7 +241,7 @@ func (r *Repo) headerView() string { if r.selectedRepo == nil { return "" } - cfg := r.s.Config() + cfg := r.cfg name := r.common.Styles.RepoHeaderName.Render(r.selectedRepo.Name()) url := git.RepoURL(cfg.Host, cfg.Port, r.selectedRepo.Repo()) // TODO move this into a style. diff --git a/ui/pages/selection/selection.go b/ui/pages/selection/selection.go index bef7352140594c8ac703ead94692f5b279fcdc35..a3d3345a0f4e80f68653089ca5aea953993b2ecd 100644 --- a/ui/pages/selection/selection.go +++ b/ui/pages/selection/selection.go @@ -6,12 +6,13 @@ import ( "github.com/charmbracelet/bubbles/key" tea "github.com/charmbracelet/bubbletea" "github.com/charmbracelet/lipgloss" + "github.com/charmbracelet/soft-serve/config" "github.com/charmbracelet/soft-serve/ui/common" "github.com/charmbracelet/soft-serve/ui/components/code" "github.com/charmbracelet/soft-serve/ui/components/selector" "github.com/charmbracelet/soft-serve/ui/git" - "github.com/charmbracelet/soft-serve/ui/session" wgit "github.com/charmbracelet/wish/git" + "github.com/gliderlabs/ssh" ) type box int @@ -23,7 +24,8 @@ const ( // Selection is the model for the selection screen/page. type Selection struct { - s session.Session + cfg *config.Config + pk ssh.PublicKey common common.Common readme *code.Code readmeHeight int @@ -32,9 +34,10 @@ type Selection struct { } // New creates a new selection model. -func New(s session.Session, common common.Common) *Selection { +func New(cfg *config.Config, pk ssh.PublicKey, common common.Common) *Selection { sel := &Selection{ - s: s, + cfg: cfg, + pk: pk, common: common, activeBox: selectorBox, // start with the selector focused } @@ -152,8 +155,8 @@ func (s *Selection) FullHelp() [][]key.Binding { func (s *Selection) Init() tea.Cmd { var readmeCmd tea.Cmd items := make([]selector.IdentifiableItem, 0) - cfg := s.s.Config() - pk := s.s.PublicKey() + cfg := s.cfg + pk := s.pk // Put configured repos first for _, r := range cfg.Repos { if r.Private && cfg.AuthRepo(r.Repo, pk) < wgit.AdminAccess { diff --git a/ui/session/session.go b/ui/session/session.go deleted file mode 100644 index fb55cc4ce710d62b3bb48ae7df4f6294395e4727..0000000000000000000000000000000000000000 --- a/ui/session/session.go +++ /dev/null @@ -1,20 +0,0 @@ -package session - -import ( - tea "github.com/charmbracelet/bubbletea" - appCfg "github.com/charmbracelet/soft-serve/config" - "github.com/charmbracelet/soft-serve/ui/git" - "github.com/gliderlabs/ssh" -) - -// Session is a interface representing a UI session. -type Session interface { - // Send sends a message to the parent Bubble Tea program. - Send(tea.Msg) - // Config returns the app configuration. - Config() *appCfg.Config - // PublicKey returns the public key of the user. - PublicKey() ssh.PublicKey - Session() ssh.Session - Source() git.GitRepoSource -} diff --git a/ui/ui.go b/ui/ui.go index 7bbf72048b17dff9f44479e697d673e6a7495299..34e735d06bfebfd1b841bfec196c29313cf311d8 100644 --- a/ui/ui.go +++ b/ui/ui.go @@ -7,6 +7,7 @@ import ( "github.com/charmbracelet/bubbles/key" tea "github.com/charmbracelet/bubbletea" "github.com/charmbracelet/lipgloss" + "github.com/charmbracelet/soft-serve/config" "github.com/charmbracelet/soft-serve/ui/common" "github.com/charmbracelet/soft-serve/ui/components/footer" "github.com/charmbracelet/soft-serve/ui/components/header" @@ -14,7 +15,7 @@ import ( "github.com/charmbracelet/soft-serve/ui/git" "github.com/charmbracelet/soft-serve/ui/pages/repo" "github.com/charmbracelet/soft-serve/ui/pages/selection" - "github.com/charmbracelet/soft-serve/ui/session" + "github.com/gliderlabs/ssh" ) type page int @@ -34,7 +35,9 @@ const ( // UI is the main UI model. type UI struct { - s session.Session + cfg *config.Config + session ssh.Session + rs git.GitRepoSource initialRepo string common common.Common pages []common.Page @@ -46,10 +49,13 @@ type UI struct { } // New returns a new UI model. -func New(s session.Session, c common.Common, initialRepo string) *UI { - h := header.New(c, s.Config().Name) +func New(cfg *config.Config, s ssh.Session, c common.Common, initialRepo string) *UI { + src := &source{cfg.Source} + h := header.New(c, cfg.Name) ui := &UI{ - s: s, + cfg: cfg, + session: s, + rs: src, common: c, pages: make([]common.Page, 2), // selection & repo activePage: selectionPage, @@ -122,8 +128,16 @@ func (ui *UI) SetSize(width, height int) { // Init implements tea.Model. func (ui *UI) Init() tea.Cmd { - ui.pages[selectionPage] = selection.New(ui.s, ui.common) - ui.pages[repoPage] = repo.New(ui.s, ui.common) + ui.pages[selectionPage] = selection.New( + ui.cfg, + ui.session.PublicKey(), + ui.common, + ) + ui.pages[repoPage] = repo.New( + ui.cfg, + ui.rs, + ui.common, + ) ui.SetSize(ui.common.Width, ui.common.Height) cmds := make([]tea.Cmd, 0) cmds = append(cmds, @@ -245,9 +259,8 @@ func (ui *UI) View() string { } func (ui *UI) setRepoCmd(rn string) tea.Cmd { - rs := ui.s.Source() return func() tea.Msg { - for _, r := range rs.AllRepos() { + for _, r := range ui.rs.AllRepos() { if r.Repo() == rn { ui.activePage = repoPage return repo.RepoMsg(r) @@ -258,9 +271,8 @@ func (ui *UI) setRepoCmd(rn string) tea.Cmd { } func (ui *UI) initialRepoCmd(rn string) tea.Cmd { - rs := ui.s.Source() return func() tea.Msg { - for _, r := range rs.AllRepos() { + for _, r := range ui.rs.AllRepos() { if r.Repo() == rn { ui.activePage = repoPage return repo.RepoMsg(r)