a0cc4ea7ceb72b97b5b9add695603aff450617ea

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

Message

fix: reset tree after switching refs

Fixes: https://github.com/charmbracelet/soft-serve/issues/86

Diff

This diff is truncated to protect this page.

  1diff --git a/internal/tui/bubbles/git/bubble.go b/internal/tui/bubbles/git/bubble.go
  2index aa27c2f6b9f6b604acfdf04e8826943a7d1f2e28..81a17447bb6b538c1e4337df80d95f618138efff 100644
  3--- a/internal/tui/bubbles/git/bubble.go
  4+++ b/internal/tui/bubbles/git/bubble.go
  5@@ -85,23 +85,14 @@ func (b *Bubble) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
  6 				cmds = append(cmds, cmd)
  7 			}
  8 		}
  9+	case refs.RefMsg:
 10+		b.state = treePage
 11 	}
 12 	m, cmd := b.boxes[b.state].Update(msg)
 13 	b.boxes[b.state] = m
 14 	if cmd != nil {
 15 		cmds = append(cmds, cmd)
 16 	}
 17-
 18-	switch msg := msg.(type) {
 19-	case tea.KeyMsg:
 20-		switch msg.String() {
 21-		case "enter":
 22-			if b.state == refsPage {
 23-				b.state = treePage
 24-				cmds = append(cmds, b.boxes[b.state].Init())
 25-			}
 26-		}
 27-	}
 28 	return b, tea.Batch(cmds...)
 29 }
 30 
 31diff --git a/internal/tui/bubbles/git/refs/bubble.go b/internal/tui/bubbles/git/refs/bubble.go
 32index 211025847848251876d714aa8bb45abfb4bbc981..b296bcc224a08ebf8f981aeacdcd238e4a37c399 100644
 33--- a/internal/tui/bubbles/git/refs/bubble.go
 34+++ b/internal/tui/bubbles/git/refs/bubble.go
 35@@ -12,6 +12,8 @@ import (
 36 	"github.com/go-git/go-git/v5/plumbing"
 37 )
 38 
 39+type RefMsg = *plumbing.Reference
 40+
 41 type item struct {
 42 	*plumbing.Reference
 43 }
 44@@ -95,8 +97,11 @@ func NewBubble(repo types.Repo, style *style.Styles, width, widthMargin, height,
 45 	return b
 46 }
 47 
 48-func (b *Bubble) SetBranch(ref *plumbing.Reference) {
 49-	b.repo.SetReference(ref)
 50+func (b *Bubble) SetBranch(ref *plumbing.Reference) (tea.Model, tea.Cmd) {
 51+	return b, func() tea.Msg {
 52+		b.repo.SetReference(ref)
 53+		return RefMsg(ref)
 54+	}
 55 }
 56 
 57 func (b *Bubble) Init() tea.Cmd {
 58@@ -155,7 +160,7 @@ func (b *Bubble) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
 59 		case "enter", "right", "l":
 60 			if b.list.Index() >= 0 {
 61 				ref := b.list.SelectedItem().(item).Reference
 62-				b.SetBranch(ref)
 63+				return b.SetBranch(ref)
 64 			}
 65 		}
 66 	}
 67diff --git a/internal/tui/bubbles/git/tree/bubble.go b/internal/tui/bubbles/git/tree/bubble.go
 68index 9a60fb80a63338769da913e073412ae04dbc203b..f07584b8be565626a5ae5f0732487f00ec78587e 100644
 69--- a/internal/tui/bubbles/git/tree/bubble.go
 70+++ b/internal/tui/bubbles/git/tree/bubble.go
 71@@ -13,6 +13,7 @@ import (
 72 	tea "github.com/charmbracelet/bubbletea"
 73 	gansi "github.com/charmbracelet/glamour/ansi"
 74 	"github.com/charmbracelet/lipgloss"
 75+	"github.com/charmbracelet/soft-serve/internal/tui/bubbles/git/refs"
 76 	"github.com/charmbracelet/soft-serve/internal/tui/bubbles/git/types"
 77 	vp "github.com/charmbracelet/soft-serve/internal/tui/bubbles/git/viewport"
 78 	"github.com/charmbracelet/soft-serve/internal/tui/style"
 79@@ -35,16 +36,16 @@ const (
 80 )
 81 
 82 type item struct {
 83-	*object.TreeEntry
 84-	*object.File
 85+	entry *object.TreeEntry
 86+	file  *object.File
 87 }
 88 
 89 func (i item) Name() string {
 90-	return i.TreeEntry.Name
 91+	return i.entry.Name
 92 }
 93 
 94 func (i item) Mode() filemode.FileMode {
 95-	return i.TreeEntry.Mode
 96+	return i.entry.Mode
 97 }
 98 
 99 func (i item) FilterValue() string { return i.Name() }
100@@ -84,8 +85,8 @@ func (d itemDelegate) Render(w io.Writer, m list.Model, index int, listItem list
101 		name = s.TreeFileDir.Render(name)
102 	}
103 	size := ""
104-	if i.File != nil {
105-		size = humanize.Bytes(uint64(i.File.Size))
106+	if i.file != nil {
107+		size = humanize.Bytes(uint64(i.file.Size))
108 	}
109 	var cs lipgloss.Style
110 	mode, _ := i.Mode().ToOSFileMode()
111@@ -151,21 +152,23 @@ func NewBubble(repo types.Repo, style *style.Styles, width, widthMargin, height,
112 		widthMargin:  widthMargin,
113 		heightMargin: heightMargin,
114 		list:         l,
115-		path:         "",
116 		state:        treeState,
117-		lastSelected: []int{},
118 	}
119 	b.SetSize(width, height)
120 	return b
121 }
122 
123-func (b *Bubble) Init() tea.Cmd {
124+func (b *Bubble) reset() tea.Cmd {
125 	b.path = ""
126-	b.list.Select(0)
127 	b.state = treeState
128+	b.lastSelected = make([]int, 0)
129 	return b.updateItems()
130 }
131 
132+func (b *Bubble) Init() tea.Cmd {
133+	return b.reset()
134+}
135+
136 func (b *Bubble) SetSize(width, height int) {
137 	b.width = width
138 	b.height = height
139@@ -191,12 +194,10 @@ func (b *Bubble) updateItems() tea.Cmd {
140 		if err != nil {
141 			break
142 		}
143-		i := item{
144-			TreeEntry: &e,
145-		}
146+		i := item{entry: &e}
147 		if e.Mode.IsFile() {
148 			if f, err := t.TreeEntryFile(&e); err == nil {
149-				i.File = f
150+				i.file = f
151 			}
152 		}
153 		its = append(its, i)
154@@ -220,9 +221,7 @@ func (b *Bubble) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
155 	case tea.KeyMsg:
156 		switch msg.String() {
157 		case "T":
158-			b.state = treeState
159-			b.path = ""
160-			cmds = append(cmds, b.updateItems())
161+			return b, b.reset()
162 		case "enter", "right", "l":
163 			if b.state == treeState {
164 				index := b.list.Index()
165@@ -234,7 +233,7 @@ func (b *Bubble) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
166 					cmds = append(cmds, b.updateItems())
167 				} else {
168 					b.lastSelected = append(b.lastSelected, index)
169-					cmds = append(cmds, b.loadFile())
170+					cmds = append(cmds, b.loadFile(item))