-
Notifications
You must be signed in to change notification settings - Fork 0
/
validatorOutputModel.go
109 lines (97 loc) · 2.53 KB
/
validatorOutputModel.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
package tui
import (
"fmt"
"strings"
"github.com/buonotti/apisense/util"
"github.com/buonotti/apisense/validation/pipeline"
"github.com/charmbracelet/bubbles/key"
"github.com/charmbracelet/bubbles/table"
tea "github.com/charmbracelet/bubbletea"
"github.com/charmbracelet/lipgloss"
)
type validatorOutputModel struct {
keymap keymap
table table.Model
selected string
}
func ValidatorOutputModel() tea.Model {
t := table.New(
table.WithColumns(getValidatorOutputColumns()),
table.WithRows(nil),
table.WithFocused(true),
table.WithHeight(7),
)
s := table.DefaultStyles()
s.Selected = s.Selected.
Foreground(lipgloss.Color("#F38BA8")).
Background(lipgloss.Color("#1e1e2e")).
Bold(false)
t.SetStyles(s)
return validatorOutputModel{
keymap: DefaultKeyMap,
table: t,
selected: "",
}
}
func (v validatorOutputModel) Init() tea.Cmd {
return nil
}
func (v validatorOutputModel) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
var cmd tea.Cmd
if choiceReportModel != "resultModel" {
if updateValidatorOutputRows {
t := table.New(
table.WithColumns(getValidatorOutputColumns()),
table.WithRows(validatorOutputRows),
table.WithFocused(true),
table.WithHeight(7),
)
s := table.DefaultStyles()
s.Selected = s.Selected.
Foreground(lipgloss.Color("#F38BA8")).
Background(lipgloss.Color("#1e1e2e")).
Bold(false)
t.SetStyles(s)
v.table = t
updateValidatorOutputRows = false
}
switch msg := msg.(type) {
case tea.KeyMsg:
switch {
case key.Matches(msg, v.keymap.back):
if choiceReportModel == "validatorOutputModel" {
choiceReportModel = "resultModel"
}
case key.Matches(msg, v.keymap.quit):
return v, tea.Quit
}
v.table, cmd = v.table.Update(msg)
return v, cmd
}
}
return v, nil
}
func (v validatorOutputModel) View() string {
return lipgloss.NewStyle().Render(v.table.View())
}
func getValidatorOutputRows(validatorOutputs []pipeline.ValidatorResult) []table.Row {
rows := make([]table.Row, 0)
for i, output := range validatorOutputs {
s := strings.Split(output.Message, ": ")
if len(s) > 1 {
q := util.Join(s[1:], "")
rows = append(rows, table.Row{fmt.Sprintf("%v", i), output.Name, q, string(output.Status)})
} else {
rows = append(rows, table.Row{fmt.Sprintf("%v", i), output.Name, "", string(output.Status)})
}
}
return rows
}
func getValidatorOutputColumns() []table.Column {
return []table.Column{
{Title: "", Width: 3},
{Title: "Validator", Width: 10},
{Title: "Error", Width: 50},
{Title: "Status", Width: 10},
}
}