summaryrefslogtreecommitdiffstats
path: root/cmd/grv/status_bar_view.go
blob: 2d3514398fdfab148c8eedc29a0d05bed74d2ef8 (plain)
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
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
package main

import (
	"fmt"
	"strings"
	"sync"
	"unicode/utf8"

	log "github.com/Sirupsen/logrus"
)

// The different prompt types grv uses
const (
	PromptText              = ":"
	SearchPromptText        = "/"
	ReverseSearchPromptText = "?"
	FilterPromptText        = "query: "
	BranchNamePromptText    = "branch name: "
	TagNamePromptText       = "tag name: "
)

type promptType int

const (
	ptNone promptType = iota
	ptCommand
	ptSearch
	ptFilter
	ptQuestion
	ptBranchName
	ptTagName
)

// StatusBarView manages the display of the status bar
type StatusBarView struct {
	repoData      RepoData
	channels      Channels
	config        ConfigSetter
	viewState     ViewState
	promptType    promptType
	pendingStatus string
	lock          sync.Mutex
}

// NewStatusBarView creates a new instance
func NewStatusBarView(repoData RepoData, channels Channels, config ConfigSetter) *StatusBarView {
	return &StatusBarView{
		repoData: repoData,
		channels: channels,
		config:   config,
	}
}

// Initialise does nothing
func (statusBarView *StatusBarView) Initialise() (err error) {
	return
}

// Dispose of any resources held by the view
func (statusBarView *StatusBarView) Dispose() {

}

// HandleEvent does nothing
func (statusBarView *StatusBarView) HandleEvent(event Event) (err error) {
	return
}

// HandleAction checks if the status bar view supports the provided action and executes it if so
func (statusBarView *StatusBarView) HandleAction(action Action) (err error) {
	switch action.ActionType {
	case ActionPrompt:
		statusBarView.showCommandPrompt(action)
	case ActionSearchPrompt:
		statusBarView.showSearchPrompt(action, SearchPromptText, ActionSearch)
	case ActionReverseSearchPrompt:
		statusBarView.showSearchPrompt(action, ReverseSearchPromptText, ActionReverseSearch)
	case ActionFilterPrompt:
		statusBarView.showFilterPrompt(action)
	case ActionQuestionPrompt:
		statusBarView.showQuestionPrompt(action)
	case ActionBranchNamePrompt:
		statusBarView.showRefNamePrompt(action, ptBranchName, BranchNamePromptText)
	case ActionTagNamePrompt:
		statusBarView.showRefNamePrompt(action, ptTagName, TagNamePromptText)
	case ActionCustomPrompt:
		statusBarView.showCustomPrompt(action)
	case ActionShowStatus:
		statusBarView.lock.Lock()
		defer statusBarView.lock.Unlock()

		if len(action.Args) > 0 {
			status, ok := action.Args[0].(string)
			if ok {
				statusBarView.pendingStatus = status
				log.Infof("Received status: %v", status)
				statusBarView.channels.UpdateDisplay()
				return
			}
		}

		err = fmt.Errorf("Expected status argument but received: %v", action.Args)
	}

	return
}

func (statusBarView *StatusBarView) showCommandPrompt(action Action) {
	statusBarView.promptType = ptCommand
	input := statusBarView.showPrompt(&PromptArgs{Prompt: PromptText}, action)
	errors := statusBarView.config.Evaluate(input)
	statusBarView.channels.ReportErrors(errors)
	statusBarView.promptType = ptNone
}

func (statusBarView *StatusBarView) showSearchPrompt(action Action, prompt string, actionType ActionType) {
	statusBarView.promptType = ptSearch
	input := statusBarView.showPrompt(&PromptArgs{Prompt: prompt}, action)

	if input == "" {
		statusBarView.channels.DoAction(Action{
			ActionType: ActionClearSearch,
		})
	} else {
		statusBarView.channels.DoAction(Action{
			ActionType: actionType,
			Args:       []interface{}{input},
		})
	}

	statusBarView.promptType = ptNone
}

func (statusBarView *StatusBarView) showFilterPrompt(action Action) {
	statusBarView.promptType = ptFilter
	input := statusBarView.showPrompt(&PromptArgs{Prompt: FilterPromptText}, action)

	if input != "" {
		statusBarView.channels.DoAction(Action{
			ActionType: ActionAddFilter,
			Args:       []interface{}{input},
		})
	}

	statusBarView.promptType = ptNone
}

func (statusBarView *StatusBarView) showQuestionPrompt(action Action) {
	if len(action.Args) == 0 {
		log.Errorf("Expected to find ActionQuestionPromptArgs arg but found none")
		return
	}

	args, ok := action.Args[0].(ActionQuestionPromptArgs)
	if !ok {
		log.Errorf("Expected to find type ActionQuestionPromptArgs but found %T", action.Args[0])
		return
	}

	validAnswers := make(map[string]string)

	promptText := fmt.Sprintf("%v (%v)", args.question, strings.Join(args.answers, "|"))

	if args.defaultAnswer != "" {
		promptText = fmt.Sprintf("%v (default=%v)", promptText, args.defaultAnswer)
		validAnswers[""] = args.defaultAnswer
	}

	promptText = fmt.Sprintf(" %v? ", promptText)

	maxAnswerLength := 0

	for _, answer := range args.answers {
		validAnswers[answer] = answer

		answerLength := len([]rune(answer))
		if answerLength > maxAnswerLength {
			maxAnswerLength = answerLength
		}
	}

	promptArgs := PromptArgs{
		Prompt:         promptText,
		NumCharsToRead: maxAnswerLength,
	}

	statusBarView.promptType = ptQuestion

	for {
		answer := statusBarView.showPrompt(&promptArgs, action)

		if validAnswer, isValidAnswer := validAnswers[answer]; isValidAnswer {
			if args.onAnswer != nil {
				args.onAnswer(validAnswer)
			}

			break
		} else if answer == "" {
			break
		}
	}

	statusBarView.promptType = ptNone
}

func (statusBarView *StatusBarView) showRefNamePrompt(action Action, promptType promptType, promptText string) {
	if len(action.Args) == 0 {
		log.Errorf("Expected ActionType argument")
		return
	}

	nextAction, ok := action.Args[0].(ActionType)
	if !ok {
		log.Errorf("Expected ActionType argument but found %T", action.Args[0])
		return
	}

	statusBarView.promptType = promptType
	input := statusBarView.showPrompt(&PromptArgs{Prompt: promptText}, action)

	if input != "" {
		statusBarView.channels.DoAction(Action{
			ActionType: nextAction,
			Args:       []interface{}{input},
		})
	}

	statusBarView.promptType = ptNone
}

func (statusBarView *StatusBarView) showCustomPrompt(action Action) {
	if len(action.Args) == 0 {
		log.Errorf("Expected ActionCustomPromptArgs argument")
		return
	}

	args, ok := action.Args[0].(ActionCustomPromptArgs)
	if !ok {
		log.Errorf("Expected ActionCustomPromptArgs argument but found %T", action.Args[0])
		return
	}

	input := statusBarView.showPrompt(&PromptArgs{Prompt: args.prompt}, action)

	args.inputHandler(input)
}

func (statusBarView *StatusBarView) showPrompt(promptArgs *PromptArgs, action Action) string {
	for _, arg := range action.Args {
		if actionPromptArgs, ok := arg.(ActionPromptArgs); ok {
			if actionPromptArgs.terminated {
				return actionPromptArgs.keys
			}

			promptArgs.InitialBufferText = actionPromptArgs.keys
			break
		}
	}

	return Prompt(promptArgs)
}

// OnStateChange updates the active state of this view
func (statusBarView *StatusBarView) OnStateChange(viewState ViewState) {
	statusBarView.lock.Lock()
	defer statusBarView.lock.Unlock()

	statusBarView.viewState = viewState
}

// ViewID returns the view ID of the status bar view
func (statusBarView *StatusBarView) ViewID() ViewID {
	return ViewStatusBar
}

// Render generates and draws the status view to the provided window
// If the readline prompt is active then this is drawn
func (statusBarView *StatusBarView) Render(win RenderWindow) (err error) {
	statusBarView.lock.Lock</