-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmain.go
More file actions
221 lines (191 loc) · 5.51 KB
/
main.go
File metadata and controls
221 lines (191 loc) · 5.51 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
package main
import (
"fmt"
"os"
"gg/src/diff"
"gg/src/io"
"gg/src/models"
"gg/src/views"
"gg/src/watcher"
tea "github.com/charmbracelet/bubbletea"
)
// processDiffLines processes diff lines and untracked files, returns files, message, view mode, and diff type
func processDiffLines(lines []string, untrackedFiles []string, diffType string) ([]models.FileDiff, string, string, string) {
var files []models.FileDiff
var noDiffMessage string
var viewMode string
// Parse tracked diff files
if len(lines) > 0 {
files = diff.ParseDiffIntoFiles(lines)
}
// Add untracked files
if len(untrackedFiles) > 0 {
untrackedDiffs := diff.CreateUntrackedFileDiffs(untrackedFiles)
files = append(files, untrackedDiffs...)
}
// Determine view mode and message
if len(files) == 0 {
noDiffMessage = "No changes to display"
viewMode = "log" // Default to log view when no diff
diffType = "none"
} else {
viewMode = "diff" // Show diff view when there are files
}
return files, noDiffMessage, viewMode, diffType
}
func main() {
lines, diffType, err := io.ReadDiff()
if err != nil {
fmt.Fprintf(os.Stderr, "Error: %v\n", err)
os.Exit(1)
}
untrackedFiles, err := io.ReadUntrackedFiles()
if err != nil {
fmt.Fprintf(os.Stderr, "Error: %v\n", err)
os.Exit(1)
}
files, noDiffMessage, viewMode, diffType := processDiffLines(lines, untrackedFiles, diffType)
m := models.Model{
Files: files,
ActiveTab: 0,
ViewMode: viewMode,
NoDiffMessage: noDiffMessage,
DiffType: diffType,
AutoReloadEnabled: true, // Enable auto-reload by default
}
p := tea.NewProgram(&appWrapper{Model: m}, tea.WithAltScreen())
if _, err := p.Run(); err != nil {
fmt.Fprintf(os.Stderr, "Error: %v\n", err)
os.Exit(1)
}
}
// RefreshDataMsg contains refreshed git diff data
type RefreshDataMsg struct {
Files []models.FileDiff
NoDiffMessage string
ViewMode string
DiffType string
}
// refreshDiffData reads git diff and untracked files, then returns RefreshDataMsg
func refreshDiffData() tea.Msg {
lines, diffType, err := io.ReadDiff()
if err != nil {
// On error, return empty data
return RefreshDataMsg{
Files: []models.FileDiff{},
NoDiffMessage: "Error reading diff",
ViewMode: "log",
DiffType: "none",
}
}
untrackedFiles, err := io.ReadUntrackedFiles()
if err != nil {
// On error, return empty data
return RefreshDataMsg{
Files: []models.FileDiff{},
NoDiffMessage: "Error reading untracked files",
ViewMode: "log",
DiffType: "none",
}
}
files, noDiffMessage, viewMode, diffType := processDiffLines(lines, untrackedFiles, diffType)
return RefreshDataMsg{
Files: files,
NoDiffMessage: noDiffMessage,
ViewMode: viewMode,
DiffType: diffType,
}
}
// appWrapper wraps the Model to provide the View method
// This avoids circular imports between models and views packages
type appWrapper struct {
models.Model
logTableInit bool
statsTableInit bool
}
func (a *appWrapper) Init() tea.Cmd {
// Start both model init and watcher
return tea.Batch(
a.Model.Init(),
watcher.WatchGitChanges(),
)
}
func (a *appWrapper) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
// Handle git change messages before passing to model
switch msg := msg.(type) {
case watcher.GitChangeMsg:
// Only trigger refresh if auto-reload is enabled
if a.AutoReloadEnabled {
// Trigger refresh and restart watcher
// Use Sequence to ensure refresh completes before watcher restarts
// This forces Bubble Tea to render immediately
return a, tea.Sequence(
tea.Cmd(refreshDiffData),
watcher.WatchGitChanges(),
)
} else {
// Just restart watcher without refreshing
return a, watcher.WatchGitChanges()
}
case RefreshDataMsg:
// Update model with refreshed data
a.Files = msg.Files
a.NoDiffMessage = msg.NoDiffMessage
a.DiffType = msg.DiffType
// Don't change ViewMode - keep user in their current view
a.ActiveTab = 0 // Reset to first tab
// Reinitialize all views with new data
if a.ViewMode == "diff" {
views.UpdateContent(&a.Model)
}
// Reinitialize tables with new data
// Only initialize stats table if there are files to display
if len(a.Files) > 0 {
views.UpdateStatsContent(&a.Model)
a.statsTableInit = true
}
views.UpdateLogContent(&a.Model)
a.logTableInit = true
return a, nil
case models.FilterAppliedMsg:
// Filter was applied, refresh the relevant view
if a.ViewMode == "log" {
views.UpdateLogContent(&a.Model)
} else if a.ViewMode == "stats" {
views.UpdateStatsContent(&a.Model)
} else if a.ViewMode == "diff" {
views.UpdateContent(&a.Model)
}
return a, nil
}
updatedModel, cmd := a.Model.Update(msg)
a.Model = updatedModel.(models.Model)
// Update content after model changes
if a.ViewMode == "diff" {
views.UpdateContent(&a.Model)
} else if a.ViewMode == "log" {
// Update log content when view changed or not initialized
if a.Model.ViewChanged || !a.logTableInit {
views.UpdateLogContent(&a.Model)
a.logTableInit = true
a.Model.ViewChanged = false
}
} else if a.ViewMode == "stats" {
// Only initialize stats table once and only if there are files to display
if !a.statsTableInit && len(a.Files) > 0 {
views.UpdateStatsContent(&a.Model)
a.statsTableInit = true
}
}
return a, cmd
}
func (a *appWrapper) View() string {
switch a.ViewMode {
case "stats":
return views.RenderStatsView(&a.Model)
case "log":
return views.RenderLogView(&a.Model)
default:
return views.RenderDiffView(&a.Model)
}
}