Skip to content

Commit

Permalink
fix(ui): clean up statusbar logic
Browse files Browse the repository at this point in the history
  • Loading branch information
aymanbagabas committed Oct 23, 2023
1 parent fd02409 commit 60c1981
Show file tree
Hide file tree
Showing 6 changed files with 104 additions and 82 deletions.
47 changes: 26 additions & 21 deletions server/ui/components/statusbar/statusbar.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,8 @@ import (
"github.com/muesli/reflow/truncate"
)

// StatusBarMsg is a message sent to the status bar.
type StatusBarMsg struct { //nolint:revive
Key string
Value string
Info string
Extra string
}

// StatusBar is a status bar model.
type StatusBar struct {
// Model is a status bar model.
type Model struct {
common common.Common
key string
value string
Expand All @@ -25,38 +17,51 @@ type StatusBar struct {
}

// New creates a new status bar component.
func New(c common.Common) *StatusBar {
s := &StatusBar{
func New(c common.Common) *Model {
s := &Model{
common: c,
}
return s
}

// SetSize implements common.Component.
func (s *StatusBar) SetSize(width, height int) {
func (s *Model) SetSize(width, height int) {
s.common.Width = width
s.common.Height = height
}

// SetStatus sets the status bar status.
func (s *Model) SetStatus(key, value, info, extra string) {
if key != "" {
s.key = key
}
if value != "" {
s.value = value
}
if info != "" {
s.info = info
}
if extra != "" {
s.extra = extra
}
}

// Init implements tea.Model.
func (s *StatusBar) Init() tea.Cmd {
func (s *Model) Init() tea.Cmd {
return nil
}

// Update implements tea.Model.
func (s *StatusBar) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
func (s *Model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
switch msg := msg.(type) {
case StatusBarMsg:
s.key = msg.Key
s.value = msg.Value
s.info = msg.Info
s.extra = msg.Extra
case tea.WindowSizeMsg:
s.SetSize(msg.Width, msg.Height)
}
return s, nil
}

// View implements tea.Model.
func (s *StatusBar) View() string {
func (s *Model) View() string {
st := s.common.Styles
w := lipgloss.Width
help := s.common.Zone.Mark(
Expand Down
58 changes: 31 additions & 27 deletions server/ui/pages/repo/files.go
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,7 @@ func (f *Files) Init() tea.Cmd {
f.currentItem = nil
f.activeView = filesViewLoading
f.lastSelected = make([]int, 0)
return tea.Batch(f.spinner.Tick, f.updateFilesCmd())
return tea.Batch(f.spinner.Tick, f.updateFilesCmd)
}

// Update implements tea.Model.
Expand All @@ -219,6 +219,7 @@ 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 @@ -228,15 +229,18 @@ func (f *Files) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
case FileContentMsg:
f.activeView = filesViewContent
f.currentContent = msg
f.code.SetContent(msg.content, msg.ext)
cmds = append(cmds,
f.code.SetContent(msg.content, msg.ext),
updateStatusBarCmd,
)
f.code.GotoTop()
case selector.SelectMsg:
switch sel := msg.IdentifiableItem.(type) {
case FileItem:
f.currentItem = &sel
f.path = filepath.Join(f.path, sel.entry.Name())
if sel.entry.IsTree() {
cmds = append(cmds, f.selectTreeCmd())
cmds = append(cmds, f.selectTreeCmd)
} else {
cmds = append(cmds, f.selectFileCmd)
}
Expand Down Expand Up @@ -269,7 +273,7 @@ func (f *Files) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
switch f.activeView {
case filesViewFiles:
if f.repo != nil {
cmds = append(cmds, f.updateFilesCmd())
cmds = append(cmds, f.updateFilesCmd)
}
case filesViewContent:
if f.currentContent.content != "" {
Expand All @@ -280,6 +284,8 @@ 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 @@ -336,8 +342,8 @@ func (f *Files) SpinnerID() int {
// StatusBarValue returns the status bar value.
func (f *Files) StatusBarValue() string {
p := f.path
if p == "." {
return ""
if p == "." || p == "" {
return " "
}
return p
}
Expand All @@ -354,7 +360,7 @@ func (f *Files) StatusBarInfo() string {
}
}

func (f *Files) updateFilesCmd() tea.Cmd {
func (f *Files) updateFilesCmd() tea.Msg {
files := make([]selector.IdentifiableItem, 0)
dirs := make([]selector.IdentifiableItem, 0)
if f.ref == nil {
Expand All @@ -366,34 +372,32 @@ func (f *Files) updateFilesCmd() tea.Cmd {
}
path := f.path
ref := f.ref
return func() tea.Msg {
t, err := r.TreePath(ref, path)
if err != nil {
return common.ErrorCmd(err)
}
ents, err := t.Entries()
if err != nil {
return common.ErrorCmd(err)
}
ents.Sort()
for _, e := range ents {
if e.IsTree() {
dirs = append(dirs, FileItem{entry: e})
} else {
files = append(files, FileItem{entry: e})
}
t, err := r.TreePath(ref, path)
if err != nil {
return common.ErrorCmd(err)
}
ents, err := t.Entries()
if err != nil {
return common.ErrorCmd(err)
}
ents.Sort()
for _, e := range ents {
if e.IsTree() {
dirs = append(dirs, FileItem{entry: e})
} else {
files = append(files, FileItem{entry: e})
}
return FileItemsMsg(append(dirs, files...))
}
return FileItemsMsg(append(dirs, files...))
}

func (f *Files) selectTreeCmd() tea.Cmd {
func (f *Files) selectTreeCmd() tea.Msg {
if f.currentItem != nil && f.currentItem.entry.IsTree() {
f.lastSelected = append(f.lastSelected, f.selector.Index())
f.cursor = 0
return f.updateFilesCmd()
}
return common.ErrorCmd(errNoFileSelected)
return common.ErrorMsg(errNoFileSelected)
}

func (f *Files) selectFileCmd() tea.Msg {
Expand Down Expand Up @@ -455,7 +459,7 @@ func (f *Files) deselectItemCmd() tea.Cmd {
}
f.cursor = index
f.activeView = filesViewFiles
return f.updateFilesCmd()
return f.updateFilesCmd
}

func (f *Files) setItems(items []selector.IdentifiableItem) tea.Cmd {
Expand Down
9 changes: 8 additions & 1 deletion server/ui/pages/repo/log.go
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,7 @@ 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 @@ -249,12 +250,14 @@ func (l *Log) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
if l.activeView == logViewDiff {
l.activeView = logViewCommits
l.selectedCommit = nil
cmds = append(cmds, updateStatusBarCmd)
}
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 @@ -277,6 +280,7 @@ 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:
Expand Down Expand Up @@ -305,7 +309,10 @@ func (l *Log) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
l.activeCommit = nil
l.selectedCommit = nil
l.selector.Select(0)
cmds = append(cmds, l.setItems([]selector.IdentifiableItem{}))
cmds = append(cmds,
l.setItems([]selector.IdentifiableItem{}),
updateStatusBarCmd,
)
case spinner.TickMsg:
if l.activeView == logViewLoading && l.spinner.ID() == msg.ID {
s, cmd := l.spinner.Update(msg)
Expand Down
10 changes: 6 additions & 4 deletions server/ui/pages/repo/readme.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,8 +99,10 @@ func (r *Readme) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
r.ref = msg
cmds = append(cmds, r.Init())
case EmptyRepoMsg:
r.code.SetContent(defaultEmptyRepoMsg(r.common.Config(),
r.repo.Name()), ".md")
cmds = append(cmds,
r.code.SetContent(defaultEmptyRepoMsg(r.common.Config(),
r.repo.Name()), ".md"),
)
case ReadmeMsg:
r.isLoading = false
r.readmePath = msg.Path
Expand Down Expand Up @@ -139,8 +141,8 @@ func (r *Readme) SpinnerID() int {
// StatusBarValue implements statusbar.StatusBar.
func (r *Readme) StatusBarValue() string {
dir := filepath.Dir(r.readmePath)
if dir == "." {
return ""
if dir == "." || dir == "" {
return " "
}
return dir
}
Expand Down
1 change: 1 addition & 0 deletions server/ui/pages/repo/refs.go
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,7 @@ 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
Loading

0 comments on commit 60c1981

Please sign in to comment.