summaryrefslogtreecommitdiffstats
path: root/cmd/grv/command_output_view.go
blob: ab4a26f28e3e2b88cf748775ebb47b02a257ee3b (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
package main

import (
	"fmt"
	"sync"

	log "github.com/Sirupsen/logrus"
)

type outputLineType int

const (
	oltNormal outputLineType = iota
	oltCommand
	oltError
	oltSuccess
)

var outputLineThemeComponentIDs = map[outputLineType]ThemeComponentID{
	oltNormal:  CmpCommandOutputNormal,
	oltCommand: CmpCommandOutputCommand,
	oltError:   CmpCommandOutputError,
	oltSuccess: CmpCommandOutputSuccess,
}

type outputLine struct {
	line     string
	lineType outputLineType
}

// CommandOutputProcessor receives the output and status of a command
type CommandOutputProcessor interface {
	AddOutputLine(line string)
	OnCommandExecutionError(err error)
	OnCommandComplete(exitCode int)
}

// CommandOutputView is a view for displaying command output
type CommandOutputView struct {
	*AbstractWindowView
	activeViewPos     ViewPos
	lastViewDimension ViewDimension
	outputLines       []outputLine
	lock              sync.Mutex
}

// NewCommandOutputView creates a new instance
func NewCommandOutputView(command string, channels Channels, config Config, variables GRVVariableSetter) *CommandOutputView {
	commandOutputView := &CommandOutputView{
		activeViewPos: NewViewPosition(),
	}

	commandOutputView.AbstractWindowView = NewAbstractWindowView(commandOutputView, channels, config, variables, &commandOutputView.lock, "output line")

	commandOutputView.addOutputLine(outputLine{
		line:     fmt.Sprintf("$ %v", command),
		lineType: oltCommand,
	})

	return commandOutputView
}

// ViewID returns the ViewID of the command output view
func (commandOutputView *CommandOutputView) ViewID() ViewID {
	return ViewCommandOutput
}

// Render generates the comamnd output view and writes it to the provided window
func (commandOutputView *CommandOutputView) Render(win RenderWindow) (err error) {
	commandOutputView.lock.Lock()
	defer commandOutputView.lock.Unlock()

	commandOutputView.lastViewDimension = win.ViewDimensions()

	winRows := win.Rows() - 2
	viewPos := commandOutputView.viewPos()

	viewRows := commandOutputView.rows()
	viewPos.DetermineViewStartRow(winRows, viewRows)

	viewRowIndex := viewPos.ViewStartRowIndex()
	startColumn := viewPos.ViewStartColumn()

	win.ApplyStyle(CmpCommandOutputNormal)

	for rowIndex := uint(0); rowIndex < winRows && viewRowIndex < viewRows; rowIndex++ {
		outputLine := commandOutputView.outputLines[viewRowIndex]
		themeComponentID := outputLineThemeComponentIDs[outputLine.lineType]

		if err = win.SetRow(rowIndex+1, startColumn, themeComponentID, " %v", outputLine.line); err != nil {
			return
		}

		viewRowIndex++
	}

	if err = win.SetSelectedRow(viewPos.SelectedRowIndex()+1, ViewStateActive); err != nil {
		return
	}

	win.DrawBorderWithStyle(CmpCommandOutputNormal)

	if err = win.SetTitle(CmpCommandOutputTitle, "Command Output"); err != nil {
		return
	}

	if err = win.SetFooter(CmpCommandOutputFooter, "Line %v of %v", viewPos.SelectedRowIndex()+1, viewRows); err != nil {
		return
	}

	return
}

// RenderHelpBar renders a help message for the command output view
func (commandOutputView *CommandOutputView) RenderHelpBar(lineBuilder *LineBuilder) (err error) {
	quitKeys := commandOutputView.config.KeyStrings(ActionRemoveView, ViewHierarchy{ViewCommandOutput, ViewAll})

	if len(quitKeys) > 0 {
		quitKeyText := fmt.Sprintf("Press %v to close command output", quitKeys[len(quitKeys)-1].keystring)
		lineBuilder.AppendWithStyle(CmpHelpbarviewSpecial, " %v", quitKeyText)
	}

	return
}

// AddOutputLine receives a line of command output
func (commandOutputView *CommandOutputView) AddOutputLine(line string) {
	commandOutputView.lock.Lock()
	defer commandOutputView.lock.Unlock()

	commandOutputView.addOutputLine(outputLine{
		line:     line,
		lineType: oltNormal,
	})
}

// OnCommandExecutionError is called when command execution has failed
func (commandOutputView *CommandOutputView) OnCommandExecutionError(err error) {
	commandOutputView.lock.Lock()
	defer commandOutputView.lock.Unlock()

	commandOutputView.addOutputLine(
		outputLine{
			line:     "",
			lineType: oltNormal,
		},
		outputLine{
			line:     fmt.Sprintf("Command execution failed: %v", err),
			lineType: oltError,
		},
	)
}

// OnCommandComplete is called when a command has completed and it's exit status is available
func (commandOutputView *CommandOutputView) OnCommandComplete(exitCode int) {
	commandOutputView.lock.Lock()
	defer commandOutputView.lock.Unlock()

	var lineType outputLineType

	if exitCode == 0 {
		lineType = oltSuccess
	} else {
		lineType = oltError
	}

	commandOutputView.addOutputLine(
		outputLine{
			line:     "",
			lineType: oltNormal,
		},
		outputLine{
			line:     fmt.Sprintf("Command exited with status %v", exitCode),
			lineType: lineType,
		},
	)
}

func (commandOutputView *CommandOutputView) addOutputLine(outputLines ...outputLine) {
	commandOutputView.outputLines = append(commandOutputView.outputLines, outputLines...)
	commandOutputView.activeViewPos.SetActiveRowIndex(commandOutputView.rows() - 1)
	commandOutputView.channels.UpdateDisplay()
}

func (commandOutputView *CommandOutputView) viewPos() ViewPos {
	return commandOutputView.activeViewPos
}

func (commandOutputView *CommandOutputView) rows() uint {
	return uint(len(commandOutputView.outputLines))
}

func (commandOutputView *CommandOutputView) viewDimension() ViewDimension {
	return commandOutputView.lastViewDimension
}

func (commandOutputView *CommandOutputView) onRowSelected(rowIndex uint) (err error) {
	return
}

func (commandOutputView *CommandOutputView) line(lineIndex uint) (line string) {
	if lineIndex < commandOutputView.rows() {
		line = commandOutputView.outputLines[lineIndex].line
	}

	return
}

// HandleAction handles the action if supported
func (commandOutputView *CommandOutputView) HandleAction(action Action) (err error) {
	commandOutputView.lock.Lock()
	defer commandOutputView.lock.Unlock()

	var handled bool
	if handled, err = commandOutputView.AbstractWindowView.HandleAction(action); handled {
		log.Debugf("Action handled by AbstractWindowView")
	} else {
		log.Debugf("Action not handled")
	}

	return
}