d3669af2fb497c638db3885af61afba048156a69

Author
Ayman Bagabas <ayman.bagabas@gmail.com>
Committer
Ayman Bagabas <ayman.bagabas@gmail.com>
Date

Message

feat: show spinner when loading commits

Diff

This diff is truncated to protect this page.

  1diff --git a/tui/log/bubble.go b/tui/log/bubble.go
  2index 328e338e77eed1975e0510eee997faa3c58f998e..30c35069b9930fdb4b97c27e0d8b77117c9fe655 100644
  3--- a/tui/log/bubble.go
  4+++ b/tui/log/bubble.go
  5@@ -26,14 +26,17 @@ var (
  6 	waitBeforeLoading = time.Millisecond * 300
  7 )
  8 
  9+type itemsMsg struct{}
 10+
 11 type commitMsg *git.Commit
 12 
 13+type countMsg int64
 14+
 15 type sessionState int
 16 
 17 const (
 18 	logState sessionState = iota
 19 	commitState
 20-	loadingState
 21 	errorState
 22 )
 23 
 24@@ -98,6 +101,10 @@ type Bubble struct {
 25 	heightMargin   int
 26 	error          common.ErrMsg
 27 	spinner        spinner.Model
 28+	loading        bool
 29+	loadingStart   time.Time
 30+	selectedCommit *git.Commit
 31+	nextPage       int
 32 }
 33 
 34 func NewBubble(repo common.GitRepo, styles *style.Styles, width, widthMargin, height, heightMargin int) *Bubble {
 35@@ -132,49 +139,45 @@ func NewBubble(repo common.GitRepo, styles *style.Styles, width, widthMargin, he
 36 	return b
 37 }
 38 
 39-func (b *Bubble) reset() tea.Cmd {
 40-	errMsg := func(err error) tea.Cmd {
 41-		return func() tea.Msg { return common.ErrMsg{Err: err} }
 42-	}
 43+func (b *Bubble) countCommits() tea.Msg {
 44 	if b.ref == nil {
 45 		ref, err := b.repo.HEAD()
 46 		if err != nil {
 47-			return errMsg(err)
 48+			return common.ErrMsg{Err: err}
 49 		}
 50 		b.ref = ref
 51 	}
 52 	count, err := b.repo.CountCommits(b.ref)
 53 	if err != nil {
 54-		return errMsg(err)
 55+		return common.ErrMsg{Err: err}
 56 	}
 57-	b.count = count
 58-	b.state = logState
 59-	b.list.Select(0)
 60-	cmd := b.updateItems()
 61-	return cmd
 62+	return countMsg(count)
 63 }
 64 
 65-func (b *Bubble) updateItems() tea.Cmd {
 66+func (b *Bubble) updateItems() tea.Msg {
 67+	if b.count == 0 {
 68+		b.count = int64(b.countCommits().(countMsg))
 69+	}
 70 	count := b.count
 71 	items := make([]list.Item, count)
 72-	b.list.SetItems(items)
 73-	page := b.list.Paginator.Page
 74+	page := b.nextPage
 75 	limit := b.list.Paginator.PerPage
 76 	skip := page * limit
 77+	// CommitsByPage pages start at 1
 78 	cc, err := b.repo.CommitsByPage(b.ref, page+1, limit)
 79 	if err != nil {
 80-		return func() tea.Msg { return common.ErrMsg{Err: err} }
 81+		return common.ErrMsg{Err: err}
 82 	}
 83 	for i, c := range cc {
 84 		idx := i + skip
 85-		if idx >= int(count) {
 86+		if int64(idx) >= count {
 87 			break
 88 		}
 89 		items[idx] = item{c}
 90 	}
 91-	cmd := b.list.SetItems(items)
 92+	b.list.SetItems(items)
 93 	b.SetSize(b.width, b.height)
 94-	return cmd
 95+	return itemsMsg{}
 96 }
 97 
 98 func (b *Bubble) Help() []common.HelpEntry {
 99@@ -203,19 +206,32 @@ func (b *Bubble) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
100 	switch msg := msg.(type) {
101 	case tea.WindowSizeMsg:
102 		b.SetSize(msg.Width, msg.Height)
103-		cmds = append(cmds, b.updateItems())
104+		cmds = append(cmds, b.updateItems)