Skip to content

Commit

Permalink
fix(ui): cleanup statusbar and misc msgs
Browse files Browse the repository at this point in the history
  • Loading branch information
aymanbagabas committed Oct 23, 2023
1 parent 60c1981 commit 0f8dc11
Show file tree
Hide file tree
Showing 9 changed files with 56 additions and 69 deletions.
40 changes: 14 additions & 26 deletions cmd/soft/browse.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
package main

import (
"io"
"os"
"path/filepath"
"time"

Expand Down Expand Up @@ -208,32 +206,34 @@ 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()).
Height(m.c.Height -
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 {
Expand All @@ -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.
Expand Down
4 changes: 2 additions & 2 deletions server/ssh/ui.go
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down
13 changes: 4 additions & 9 deletions server/ui/pages/repo/files.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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:
Expand All @@ -245,22 +243,19 @@ 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:
switch {
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):
Expand All @@ -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 {
Expand All @@ -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 = ""
Expand Down Expand Up @@ -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]
Expand Down
22 changes: 11 additions & 11 deletions server/ui/pages/repo/log.go
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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:
Expand All @@ -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,
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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
Expand Down
2 changes: 2 additions & 0 deletions server/ui/pages/repo/readme.go
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
Expand Down
3 changes: 2 additions & 1 deletion server/ui/pages/repo/refs.go
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand All @@ -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:
Expand Down
2 changes: 1 addition & 1 deletion server/ui/pages/repo/refsitem.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)),
),
Expand Down
36 changes: 17 additions & 19 deletions server/ui/pages/repo/repo.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package repo

import (
"fmt"
"strings"

"github.com/charmbracelet/bubbles/help"
"github.com/charmbracelet/bubbles/key"
Expand All @@ -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"
)
Expand All @@ -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 {
Expand Down Expand Up @@ -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)
Expand All @@ -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)
Expand All @@ -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 {
Expand Down Expand Up @@ -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)
Expand All @@ -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 {
Expand Down Expand Up @@ -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 = ""
Expand Down Expand Up @@ -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 {
Expand All @@ -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().
Expand Down
3 changes: 3 additions & 0 deletions server/ui/styles/styles.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,7 @@ type Styles struct {
}
ItemSelector lipgloss.Style
Paginator lipgloss.Style
Selector lipgloss.Style
}

Tree struct {
Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit 0f8dc11

Please sign in to comment.