From 2291b833afd092cd5c64e8497c3c435268fc75cd Mon Sep 17 00:00:00 2001 From: Ayman Bagabas Date: Fri, 6 Oct 2023 12:24:40 -0400 Subject: [PATCH] fix(ui): cleanup statusbar and misc msgs --- cmd/soft/browse.go | 40 +++++++++++--------------------- server/ssh/ui.go | 4 ++-- server/ui/pages/repo/files.go | 13 ++++------- server/ui/pages/repo/log.go | 22 +++++++++--------- server/ui/pages/repo/readme.go | 2 ++ server/ui/pages/repo/refs.go | 3 ++- server/ui/pages/repo/refsitem.go | 2 +- server/ui/pages/repo/repo.go | 36 ++++++++++++++-------------- server/ui/styles/styles.go | 3 +++ 9 files changed, 56 insertions(+), 69 deletions(-) diff --git a/cmd/soft/browse.go b/cmd/soft/browse.go index 9b346b417..009c4de62 100644 --- a/cmd/soft/browse.go +++ b/cmd/soft/browse.go @@ -1,8 +1,6 @@ package main import ( - "io" - "os" "path/filepath" "time" @@ -208,22 +206,20 @@ func (m *model) Update(msg tea.Msg) (tea.Model, tea.Cmd) { // View implements tea.Model. func (m *model) View() string { - view := m.m.View() + style := m.c.Styles.App.Copy() + wm, hm := style.GetHorizontalFrameSize(), style.GetVerticalFrameSize() if m.showFooter { - view = lipgloss.JoinVertical(lipgloss.Left, view, m.f.View()) + hm += m.f.Height() } + var view string switch m.state { + case startState: + view = m.m.View() case errorState: - appStyle := m.c.Styles.App.Copy() - wm, hm := appStyle.GetHorizontalFrameSize(), appStyle.GetVerticalFrameSize() - if m.showFooter { - hm += m.f.Height() - } - err := m.c.Styles.ErrorTitle.Render("Bummer") err += m.c.Styles.ErrorBody.Render(m.error.Error()) - return m.c.Styles.Error.Copy(). + view = m.c.Styles.Error.Copy(). Width(m.c.Width - wm - m.c.Styles.ErrorBody.GetHorizontalFrameSize()). @@ -231,9 +227,13 @@ func (m *model) View() string { hm - m.c.Styles.Error.GetVerticalFrameSize()). Render(err) - default: - return m.c.Zone.Scan(m.c.Styles.App.Render(view)) } + + if m.showFooter { + view = lipgloss.JoinVertical(lipgloss.Top, view, m.f.View()) + } + + return m.c.Zone.Scan(style.Render(view)) } type repository struct { @@ -244,19 +244,7 @@ var _ proto.Repository = repository{} // Description implements proto.Repository. func (r repository) Description() string { - fp := filepath.Join(r.r.Path, "description") - f, err := os.Open(fp) - if err != nil { - return "" - } - - defer f.Close() // nolint: errcheck - bts, err := io.ReadAll(f) - if err != nil { - return "" - } - - return string(bts) + return "" } // ID implements proto.Repository. diff --git a/server/ssh/ui.go b/server/ssh/ui.go index 329e95c8d..87c73ad71 100644 --- a/server/ssh/ui.go +++ b/server/ssh/ui.go @@ -280,10 +280,10 @@ func (ui *UI) View() string { view = "Unknown state :/ this is a bug!" } if ui.activePage == selectionPage { - view = lipgloss.JoinVertical(lipgloss.Left, ui.header.View(), view) + view = lipgloss.JoinVertical(lipgloss.Top, ui.header.View(), view) } if ui.showFooter { - view = lipgloss.JoinVertical(lipgloss.Left, view, ui.footer.View()) + view = lipgloss.JoinVertical(lipgloss.Top, view, ui.footer.View()) } return ui.common.Zone.Scan( ui.common.Styles.App.Render(view), diff --git a/server/ui/pages/repo/files.go b/server/ui/pages/repo/files.go index d05be2978..5103baacd 100644 --- a/server/ui/pages/repo/files.go +++ b/server/ui/pages/repo/files.go @@ -219,7 +219,6 @@ func (f *Files) Update(msg tea.Msg) (tea.Model, tea.Cmd) { case FileItemsMsg: cmds = append(cmds, f.selector.SetItems(msg), - updateStatusBarCmd, ) f.activeView = filesViewFiles if f.cursor >= 0 { @@ -231,7 +230,6 @@ func (f *Files) Update(msg tea.Msg) (tea.Model, tea.Cmd) { f.currentContent = msg cmds = append(cmds, f.code.SetContent(msg.content, msg.ext), - updateStatusBarCmd, ) f.code.GotoTop() case selector.SelectMsg: @@ -245,9 +243,6 @@ func (f *Files) Update(msg tea.Msg) (tea.Model, tea.Cmd) { cmds = append(cmds, f.selectFileCmd) } } - case BackMsg: - f.path = filepath.Dir(f.path) - cmds = append(cmds, f.deselectItemCmd()) case tea.KeyMsg: switch f.activeView { case filesViewFiles: @@ -255,12 +250,12 @@ func (f *Files) Update(msg tea.Msg) (tea.Model, tea.Cmd) { case key.Matches(msg, f.common.KeyMap.SelectItem): cmds = append(cmds, f.selector.SelectItemCmd) case key.Matches(msg, f.common.KeyMap.BackItem): - cmds = append(cmds, backCmd) + cmds = append(cmds, f.deselectItemCmd()) } case filesViewContent: switch { case key.Matches(msg, f.common.KeyMap.BackItem): - cmds = append(cmds, backCmd) + cmds = append(cmds, f.deselectItemCmd()) case key.Matches(msg, f.common.KeyMap.Copy): cmds = append(cmds, copyCmd(f.currentContent.content, "File contents copied to clipboard")) case key.Matches(msg, lineNo): @@ -270,6 +265,7 @@ func (f *Files) Update(msg tea.Msg) (tea.Model, tea.Cmd) { } } case tea.WindowSizeMsg: + f.SetSize(msg.Width, msg.Height) switch f.activeView { case filesViewFiles: if f.repo != nil { @@ -284,8 +280,6 @@ func (f *Files) Update(msg tea.Msg) (tea.Model, tea.Cmd) { } } } - case selector.ActiveMsg: - cmds = append(cmds, updateStatusBarCmd) case EmptyRepoMsg: f.ref = nil f.path = "" @@ -452,6 +446,7 @@ func (f *Files) selectFileCmd() tea.Msg { } func (f *Files) deselectItemCmd() tea.Cmd { + f.path = filepath.Dir(f.path) index := 0 if len(f.lastSelected) > 0 { index = f.lastSelected[len(f.lastSelected)-1] diff --git a/server/ui/pages/repo/log.go b/server/ui/pages/repo/log.go index c010264dc..ede919630 100644 --- a/server/ui/pages/repo/log.go +++ b/server/ui/pages/repo/log.go @@ -211,7 +211,6 @@ func (l *Log) Update(msg tea.Msg) (tea.Model, tea.Cmd) { if i != nil { l.activeCommit = i.(LogItem).Commit } - cmds = append(cmds, updateStatusBarCmd) case tea.KeyMsg, tea.MouseMsg: switch l.activeView { case logViewCommits: @@ -242,22 +241,17 @@ func (l *Log) Update(msg tea.Msg) (tea.Model, tea.Cmd) { case tea.KeyMsg: switch { case key.Matches(kmsg, l.common.KeyMap.BackItem): - cmds = append(cmds, backCmd) + l.goBack() } } } - case BackMsg: - if l.activeView == logViewDiff { - l.activeView = logViewCommits - l.selectedCommit = nil - cmds = append(cmds, updateStatusBarCmd) - } + case GoBackMsg: + l.goBack() case selector.ActiveMsg: switch sel := msg.IdentifiableItem.(type) { case LogItem: l.activeCommit = sel.Commit } - cmds = append(cmds, updateStatusBarCmd) case selector.SelectMsg: switch sel := msg.IdentifiableItem.(type) { case LogItem: @@ -280,10 +274,10 @@ func (l *Log) Update(msg tea.Msg) (tea.Model, tea.Cmd) { ) l.vp.GotoTop() l.activeView = logViewDiff - cmds = append(cmds, updateStatusBarCmd) case footer.ToggleFooterMsg: cmds = append(cmds, l.updateCommitsCmd) case tea.WindowSizeMsg: + l.SetSize(msg.Width, msg.Height) if l.selectedCommit != nil && l.currentDiff != nil { l.vp.SetContent( lipgloss.JoinVertical(lipgloss.Top, @@ -311,7 +305,6 @@ func (l *Log) Update(msg tea.Msg) (tea.Model, tea.Cmd) { l.selector.Select(0) cmds = append(cmds, l.setItems([]selector.IdentifiableItem{}), - updateStatusBarCmd, ) case spinner.TickMsg: if l.activeView == logViewLoading && l.spinner.ID() == msg.ID { @@ -401,6 +394,13 @@ func (l *Log) StatusBarInfo() string { } } +func (l *Log) goBack() { + if l.activeView == logViewDiff { + l.activeView = logViewCommits + l.selectedCommit = nil + } +} + func (l *Log) countCommitsCmd() tea.Msg { if l.ref == nil { return nil diff --git a/server/ui/pages/repo/readme.go b/server/ui/pages/repo/readme.go index 396a6d7de..a1d8218d4 100644 --- a/server/ui/pages/repo/readme.go +++ b/server/ui/pages/repo/readme.go @@ -98,6 +98,8 @@ func (r *Readme) Update(msg tea.Msg) (tea.Model, tea.Cmd) { case RefMsg: r.ref = msg cmds = append(cmds, r.Init()) + case tea.WindowSizeMsg: + r.SetSize(msg.Width, msg.Height) case EmptyRepoMsg: cmds = append(cmds, r.code.SetContent(defaultEmptyRepoMsg(r.common.Config(), diff --git a/server/ui/pages/repo/refs.go b/server/ui/pages/repo/refs.go index 125e940f7..3b7be378a 100644 --- a/server/ui/pages/repo/refs.go +++ b/server/ui/pages/repo/refs.go @@ -124,6 +124,8 @@ func (r *Refs) Update(msg tea.Msg) (tea.Model, tea.Cmd) { case RefMsg: r.ref = msg cmds = append(cmds, r.Init()) + case tea.WindowSizeMsg: + r.SetSize(msg.Width, msg.Height) case RefItemsMsg: if r.refPrefix == msg.prefix { cmds = append(cmds, r.selector.SetItems(msg.items)) @@ -138,7 +140,6 @@ func (r *Refs) Update(msg tea.Msg) (tea.Model, tea.Cmd) { case RefItem: r.activeRef = sel.Reference } - cmds = append(cmds, updateStatusBarCmd) case selector.SelectMsg: switch i := msg.IdentifiableItem.(type) { case RefItem: diff --git a/server/ui/pages/repo/refsitem.go b/server/ui/pages/repo/refsitem.go index 85e96857d..333ffc393 100644 --- a/server/ui/pages/repo/refsitem.go +++ b/server/ui/pages/repo/refsitem.go @@ -178,7 +178,7 @@ func (d RefItemDelegate) Render(w io.Writer, m list.Model, index int, listItem l d.common.Zone.Mark( i.ID(), st.Base.Render( - lipgloss.JoinVertical(lipgloss.Top, + lipgloss.JoinHorizontal(lipgloss.Top, truncate.String(selector+ref+desc+hash, uint(m.Width()-horizontalFrameSize)), ), diff --git a/server/ui/pages/repo/repo.go b/server/ui/pages/repo/repo.go index 1c6f2e298..de3f27110 100644 --- a/server/ui/pages/repo/repo.go +++ b/server/ui/pages/repo/repo.go @@ -2,6 +2,7 @@ package repo import ( "fmt" + "strings" "github.com/charmbracelet/bubbles/help" "github.com/charmbracelet/bubbles/key" @@ -12,6 +13,7 @@ import ( "github.com/charmbracelet/soft-serve/server/proto" "github.com/charmbracelet/soft-serve/server/ui/common" "github.com/charmbracelet/soft-serve/server/ui/components/footer" + "github.com/charmbracelet/soft-serve/server/ui/components/selector" "github.com/charmbracelet/soft-serve/server/ui/components/statusbar" "github.com/charmbracelet/soft-serve/server/ui/components/tabs" ) @@ -29,14 +31,11 @@ type EmptyRepoMsg struct{} // CopyURLMsg is a message to copy the URL of the current repository. type CopyURLMsg struct{} -// UpdateStatusBarMsg updates the status bar. -type UpdateStatusBarMsg struct{} - // RepoMsg is a message that contains a git.Repository. type RepoMsg proto.Repository // nolint:revive -// BackMsg is a message to go back to the previous view. -type BackMsg struct{} +// GoBackMsg is a message to go back to the previous view. +type GoBackMsg struct{} // CopyMsg is a message to indicate copied text. type CopyMsg struct { @@ -148,12 +147,10 @@ func (r *Repo) Update(msg tea.Msg) (tea.Model, tea.Cmd) { // This will set the selected repo in each pane's model. r.updateModels(msg), ) - r.setStatusBarInfo() case RefMsg: r.ref = msg cmds = append(cmds, r.updateModels(msg)) r.state = readyState - r.setStatusBarInfo() case tabs.SelectTabMsg: r.activeTab = int(msg) t, cmd := r.tabs.Update(msg) @@ -163,7 +160,6 @@ func (r *Repo) Update(msg tea.Msg) (tea.Model, tea.Cmd) { } case tabs.ActiveTabMsg: r.activeTab = int(msg) - r.setStatusBarInfo() case tea.KeyMsg, tea.MouseMsg: t, cmd := r.tabs.Update(msg) r.tabs = t.(*tabs.Tabs) @@ -188,11 +184,10 @@ func (r *Repo) Update(msg tea.Msg) (tea.Model, tea.Cmd) { case tea.MouseRight: switch { case r.common.Zone.Get("repo-main").InBounds(msg): - cmds = append(cmds, backCmd) + cmds = append(cmds, goBackCmd) } } } - r.setStatusBarInfo() case CopyMsg: txt := msg.Text if cfg := r.common.Config(); cfg != nil { @@ -245,8 +240,6 @@ func (r *Repo) Update(msg tea.Msg) (tea.Model, tea.Cmd) { break } } - case UpdateStatusBarMsg: - r.setStatusBarInfo() } active := r.panes[r.activeTab] m, cmd := active.Update(msg) @@ -255,6 +248,15 @@ func (r *Repo) Update(msg tea.Msg) (tea.Model, tea.Cmd) { cmds = append(cmds, cmd) } + // Update the status bar on these events + // Must come after we've updated the active tab + switch msg.(type) { + case RepoMsg, RefMsg, tabs.ActiveTabMsg, tea.KeyMsg, tea.MouseMsg, + FileItemsMsg, FileContentMsg, selector.ActiveMsg, LogItemsMsg, + GoBackMsg, LogDiffMsg, EmptyRepoMsg: + r.setStatusBarInfo() + } + s, cmd := r.statusbar.Update(msg) r.statusbar = s.(*statusbar.Model) if cmd != nil { @@ -310,7 +312,7 @@ func (r *Repo) headerView() string { name = r.selectedRepo.Name() } name = r.common.Styles.Repo.HeaderName.Render(name) - desc := r.selectedRepo.Description() + desc := strings.TrimSpace(r.selectedRepo.Description()) if desc == "" { desc = name name = "" @@ -394,8 +396,8 @@ func copyCmd(text, msg string) tea.Cmd { } } -func backCmd() tea.Msg { - return BackMsg{} +func goBackCmd() tea.Msg { + return GoBackMsg{} } func switchTabCmd(m common.TabComponent) tea.Cmd { @@ -404,10 +406,6 @@ func switchTabCmd(m common.TabComponent) tea.Cmd { } } -func updateStatusBarCmd() tea.Msg { - return UpdateStatusBarMsg{} -} - func renderLoading(c common.Common, s spinner.Model) string { msg := fmt.Sprintf("%s loading…", s.View()) return c.Styles.SpinnerContainer.Copy(). diff --git a/server/ui/styles/styles.go b/server/ui/styles/styles.go index 39fe5211c..1e2eaef0f 100644 --- a/server/ui/styles/styles.go +++ b/server/ui/styles/styles.go @@ -104,6 +104,7 @@ type Styles struct { } ItemSelector lipgloss.Style Paginator lipgloss.Style + Selector lipgloss.Style } Tree struct { @@ -385,6 +386,8 @@ func DefaultStyles() *Styles { s.Ref.Paginator = s.Log.Paginator.Copy() + s.Ref.Selector = lipgloss.NewStyle() + s.Tree.Selector = s.Tree.Normal.FileName.Copy(). Width(1). Foreground(selectorColor)