-
Notifications
You must be signed in to change notification settings - Fork 263
/
error.go
41 lines (33 loc) · 1.05 KB
/
error.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
package controldisplay
import (
"fmt"
"github.com/turbot/go-kit/helpers"
"github.com/turbot/steampipe/pkg/constants"
)
type ErrorRenderer struct {
error error
// screen width
width int
indent string
}
func NewErrorRenderer(err error, width int, indent string) *ErrorRenderer {
return &ErrorRenderer{
error: err,
width: width,
indent: indent,
}
}
func (r ErrorRenderer) Render() string {
status := NewResultStatusRenderer(constants.ControlError)
statusString := status.Render()
statusWidth := helpers.PrintableLength(statusString)
formattedIndent := fmt.Sprintf("%s", ControlColors.Indent(r.indent))
indentWidth := helpers.PrintableLength(formattedIndent)
// figure out how much width we have available for the error message
availableWidth := r.width - statusWidth - indentWidth
errorMessage := helpers.TruncateString(r.error.Error(), availableWidth)
errorString := fmt.Sprintf("%s", ControlColors.StatusError(errorMessage))
// now put these all together
str := fmt.Sprintf("%s%s%s", formattedIndent, statusString, errorString)
return str
}