This repository has been archived by the owner on Jun 13, 2022. It is now read-only.
forked from rgburke/grv
/
window_view_factory.go
211 lines (171 loc) · 6.11 KB
/
window_view_factory.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
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
package main
import (
"fmt"
"regexp"
log "github.com/Sirupsen/logrus"
)
// WindowViewFactory provides a generic interface
// for creating view instances
type WindowViewFactory struct {
repoData RepoData
repoController RepoController
channels Channels
config Config
variables GRVVariableSetter
}
var hexRegexp = regexp.MustCompile(`^[[:xdigit:]]+$`)
// NewWindowViewFactory creates a new instance
func NewWindowViewFactory(repoData RepoData, repoController RepoController, channels Channels, config Config, variables GRVVariableSetter) *WindowViewFactory {
return &WindowViewFactory{
repoData: repoData,
repoController: repoController,
channels: channels,
config: config,
variables: variables,
}
}
// CreateWindowView creates a window view instance identified by the provided view id
func (windowViewFactory *WindowViewFactory) CreateWindowView(viewID ViewID) (WindowView, error) {
return windowViewFactory.CreateWindowViewWithArgs(viewID, nil)
}
// CreateWindowViewWithArgs creates a window view instance identified by the provided view id and instantiated with the provided args
func (windowViewFactory *WindowViewFactory) CreateWindowViewWithArgs(viewID ViewID, args []interface{}) (windowView WindowView, err error) {
switch viewID {
case ViewRef:
windowView = windowViewFactory.createRefView()
case ViewCommit:
windowView, err = windowViewFactory.createCommitView(args)
case ViewDiff:
windowView, err = windowViewFactory.createDiffView(args)
case ViewGitStatus:
windowView = windowViewFactory.createGitStatusView()
case ViewGRVVariable:
windowView = windowViewFactory.createGRVVariableView()
case ViewRemote:
windowView = windowViewFactory.createRemoteView()
default:
err = fmt.Errorf("Unsupported view type: %v", viewID)
}
return
}
func (windowViewFactory *WindowViewFactory) createRefView() *RefView {
log.Info("Created RefView instance")
return NewRefView(windowViewFactory.repoData, windowViewFactory.repoController, windowViewFactory.channels,
windowViewFactory.config, windowViewFactory.variables)
}
func (windowViewFactory *WindowViewFactory) createCommitView(args []interface{}) (commitView *CommitView, err error) {
ref, err := windowViewFactory.getRef(args)
if err != nil {
return
}
commitView = NewCommitView(windowViewFactory.repoData, windowViewFactory.repoController, windowViewFactory.channels,
windowViewFactory.config, windowViewFactory.variables)
log.Info("Created CommitView instance")
if ref == nil {
ref = windowViewFactory.repoData.Head()
}
log.Debugf("Providing Ref to CommitView instance %v:%v", ref.Name(), ref.Oid())
err = commitView.OnRefSelect(ref)
return
}
func (windowViewFactory *WindowViewFactory) createDiffView(args []interface{}) (diffView *DiffView, err error) {
ref, err := windowViewFactory.getRef(args)
if err != nil {
return
}
diffView = NewDiffView(windowViewFactory.repoData, windowViewFactory.channels, windowViewFactory.config, windowViewFactory.variables)
log.Info("Created DiffView instance")
if ref != nil {
var commit *Commit
commit, err = windowViewFactory.repoData.Commit(ref.Oid())
if err == nil {
log.Debugf("Providing Commit to DiffView instance %v", commit.oid)
err = diffView.OnCommitSelected(commit)
}
}
return
}
func (windowViewFactory *WindowViewFactory) createGitStatusView() *GitStatusView {
gitStatusView := NewGitStatusView(windowViewFactory.repoData, windowViewFactory.repoController, windowViewFactory.channels,
windowViewFactory.config, windowViewFactory.variables)
if status := windowViewFactory.repoData.Status(); status != nil {
gitStatusView.OnStatusChanged(status)
}
log.Info("Created GitStatusView instance")
return gitStatusView
}
func (windowViewFactory *WindowViewFactory) createGRVVariableView() *GRVVariableView {
log.Info("Created GRVVariableView instance")
return NewGRVVariableView(windowViewFactory.channels, windowViewFactory.config, windowViewFactory.variables)
}
func (windowViewFactory *WindowViewFactory) createRemoteView() *RemoteView {
log.Info("Created GRVVariableView instance")
return NewRemoteView(windowViewFactory.repoData, windowViewFactory.repoController,
windowViewFactory.channels, windowViewFactory.config, windowViewFactory.variables)
}
func (windowViewFactory *WindowViewFactory) getRef(args []interface{}) (ref Ref, err error) {
if len(args) == 0 {
return
}
refName, ok := args[0].(string)
if !ok {
err = fmt.Errorf("Expected refName argument of type string but got type %T", args[0])
return
}
if ref, err = windowViewFactory.repoData.Ref(refName); err == nil {
log.Debugf("Found ref %v for input %v", ref.Name(), refName)
return
}
if hexRegexp.MatchString(refName) {
var commit *Commit
commit, err = windowViewFactory.repoData.CommitByOid(refName)
if err != nil {
err = fmt.Errorf("Invalid oid: %v - %v", refName, err)
return
}
log.Debugf("Found commit %v for input %v", commit.oid, refName)
ref = &HEAD{oid: commit.oid}
} else {
log.Debug("Input is not oid")
}
return
}
// GenerateWindowViewFactoryHelpSection generates a help documentation table of supported views
func GenerateWindowViewFactoryHelpSection(config Config) *HelpSection {
headers := []TableHeader{
{text: "View", themeComponentID: CmpHelpViewSectionTableHeader},
{text: "Args", themeComponentID: CmpHelpViewSectionTableHeader},
}
tableFormatter := NewTableFormatterWithHeaders(headers, config)
tableFormatter.SetGridLines(true)
type viewConstructor struct {
viewID ViewID
args string
}
viewConstructors := []viewConstructor{
{
viewID: ViewCommit,
args: "ref or oid",
},
{
viewID: ViewDiff,
args: "oid",
},
{
viewID: ViewGitStatus,
args: "none",
},
{
viewID: ViewRef,
args: "none",
},
}
tableFormatter.Resize(uint(len(viewConstructors)))
for rowIndex, constructor := range viewConstructors {
tableFormatter.SetCellWithStyle(uint(rowIndex), 0, CmpHelpViewSectionTableRow, "%v", ViewName(constructor.viewID))
tableFormatter.SetCellWithStyle(uint(rowIndex), 1, CmpHelpViewSectionTableRow, "%v", constructor.args)
}
return &HelpSection{
tableFormatter: tableFormatter,
}
}