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

const (
	evMaxErrorDisplayNum = uint(5)
)

// ErrorView contains state for the error view
type ErrorView struct {
	errors []error
}

// NewErrorView creates a new instance of the error view
func NewErrorView() *ErrorView {
	return &ErrorView{}
}

// Initialise does nothing
func (errorView *ErrorView) Initialise() (err error) {
	return
}

// SetErrors sets the errors for the error view to display
func (errorView *ErrorView) SetErrors(errors []error) {
	errorView.errors = errors
}

// DisplayRowsRequired calculates the number of rows on the display required to display the errors the error view currently has set
func (errorView *ErrorView) DisplayRowsRequired() uint {
	errorNum := uint(len(errorView.errors))

	var rowsRequired uint
	switch {
	case errorNum == 0:
		return 0
	case errorNum > evMaxErrorDisplayNum:
		rowsRequired = evMaxErrorDisplayNum + 1
	default:
		rowsRequired = errorNum
	}

	return rowsRequired + 2
}

// Render generates and writes the error view to the provided window
func (errorView *ErrorView) Render(win RenderWindow) (err error) {
	errorNum := uint(len(errorView.errors))
	errorDisplayNum := MinUInt(errorNum, evMaxErrorDisplayNum)

	var lineBuilder *LineBuilder
	for i := uint(1); i < win.Rows()-1 && i-1 < errorDisplayNum; i++ {
		if lineBuilder, err = win.LineBuilder(i, 1); err != nil {
			return err
		}

		err = errorView.errors[i-1]
		lineBuilder.AppendWithStyle(CmpErrorViewErrors, " %v", err)
	}

	if errorDisplayNum < errorNum && win.Rows() >= evMaxErrorDisplayNum+3 {
		if lineBuilder, err = win.LineBuilder(win.Rows()-2, 1); err != nil {
			return
		}

		errorsNotDisplayed := errorNum - errorDisplayNum
		suffix := ""
		if errorsNotDisplayed > 1 {
			suffix += "s"
		}

		lineBuilder.AppendWithStyle(CmpNone, " + %v more error%v", errorsNotDisplayed, suffix)
	}

	win.DrawBorder()

	if err = win.SetTitle(CmpErrorViewTitle, "Errors"); err != nil {
		return
	}

	errorText := "Error"
	if errorNum > 1 {
		errorText += "s"
	}

	err = win.SetFooter(CmpErrorViewFooter, "%v %v", errorNum, errorText)

	return
}

// HandleEvent does nothing
func (errorView *ErrorView) HandleEvent(event Event) (err error) {
	return
}

// HandleAction does nothing
func (errorView *ErrorView) HandleAction(Action) (err error) {
	return
}

// OnStateChange does nothing
func (errorView *ErrorView) OnStateChange(ViewState) {

}

// ViewID returns the view ID of the error view
func (errorView *ErrorView) ViewID() ViewID {
	return ViewError
}

// RenderHelpBar does nothing
func (errorView *ErrorView) RenderHelpBar(*LineBuilder) (err error) {
	return
}