-
Notifications
You must be signed in to change notification settings - Fork 2
/
tui.go
87 lines (73 loc) · 1.88 KB
/
tui.go
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
package main
import (
tea "github.com/charmbracelet/bubbletea"
"github.com/shurcooL/githubv4"
)
const operationInProgressText = "Preventing quit due to in progress operations. To quit, repeat quit command."
var _ tea.Model = App{}
func newApp(_ *githubv4.Client, query pullRequestQuery, pullRequests []pullRequest) App {
listView := newListView(query, pullRequests)
detailsView := newDetailsView()
return App{
listView: listView,
detailsView: detailsView,
}
}
type App struct {
listView ListView
detailsView DetailsView
isShowingDetails bool
preventedQuitOnce bool
}
func (a App) Init() tea.Cmd {
return tea.Batch(
a.listView.Init(),
)
}
func (a App) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
switch msg.(type) {
case viewPullRequestDetails:
a.isShowingDetails = true
case hidePullRequestDetails:
a.isShowingDetails = false
}
switch msg := msg.(type) {
case tea.KeyMsg:
if msg.String() == "ctrl+c" || msg.String() == "q" {
if a.listView.hasWorkInProgress() && !a.preventedQuitOnce {
a.preventedQuitOnce = true
return a, a.listView.listModel.NewStatusMessage(operationInProgressText)
}
return a, tea.Quit
}
// forward key messages to active view
var cmd tea.Cmd
if a.isShowingDetails {
cmd = a.updateDetailsView(msg)
} else {
cmd = a.updateListView(msg)
}
return a, cmd
default:
// forward other messages to all views
listViewCmd := a.updateListView(msg)
detailsViewCmd := a.updateDetailsView(msg)
return a, tea.Batch(listViewCmd, detailsViewCmd)
}
}
func (a *App) updateListView(msg tea.Msg) tea.Cmd {
var cmd tea.Cmd
a.listView, cmd = a.listView.Update(msg)
return cmd
}
func (a *App) updateDetailsView(msg tea.Msg) tea.Cmd {
var cmd tea.Cmd
a.detailsView, cmd = a.detailsView.Update(msg)
return cmd
}
func (a App) View() string {
if a.isShowingDetails {
return a.detailsView.View()
}
return a.listView.View()
}