-
Notifications
You must be signed in to change notification settings - Fork 0
/
ddrm-tui.go
195 lines (159 loc) · 3.78 KB
/
ddrm-tui.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
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
//go:build client
// +build client
package main
import (
"fmt"
"os"
"slices"
"github.com/charmbracelet/bubbles/table"
tea "github.com/charmbracelet/bubbletea"
"github.com/charmbracelet/lipgloss"
"github.com/muesli/termenv"
)
// TUI state
type UiModel struct {
recordTable table.Model
}
// TUI msg struct that supports String()
type uiProcessRecords struct {
process bool
}
func (r uiProcessRecords) String() string {
return ""
}
// TUI runtime objects
var (
ddrmTui *tea.Program
uiBaseStyle = lipgloss.NewStyle().
BorderStyle(lipgloss.NormalBorder()).
BorderForeground(lipgloss.Color("240"))
uiTermColor = termenv.EnvColorProfile().Color
helpText = termenv.Style{}.Foreground(uiTermColor("241")).Styled
highlightText = termenv.Style{}.Foreground(uiTermColor("204")).Background(uiTermColor("235")).Styled
)
func (ui UiModel) Init() tea.Cmd {
return nil
}
func (ui UiModel) View() string {
return uiBaseStyle.Render(ui.recordTable.View()) + "\n\n" +
helpText("q: exit • x: toggle altscreen mode ") + highlightText(fmt.Sprint(len(ddrmRecordStates))+" records\n")
}
func (ui UiModel) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
switch msg := msg.(type) {
case tea.KeyMsg:
switch msg.String() {
case "q", "ctrl+c", "esc":
cronScheduler.Shutdown()
return ui, tea.Quit
case "x":
var cmd tea.Cmd
if stateAltScreenMode {
cmd = tea.ExitAltScreen
} else {
cmd = tea.EnterAltScreen
}
stateAltScreenMode = !stateAltScreenMode
return ui, cmd
}
case uiProcessRecords:
return updateUi(), nil
}
return updateUi(), nil
}
func updateUi() UiModel {
columns := []table.Column{
{Title: "-", Width: 1},
{Title: "FQDN", Width: 30},
{Title: "RR", Width: 4},
{Title: "Expected", Width: 20},
{Title: "Currently", Width: 20},
{Title: "✉︎", Width: 1},
{Title: "↓", Width: 1},
{Title: "⚠️", Width: 1},
}
rows := []table.Row{}
// golang doesn't have an ordered map type, so we need to extract the keys,
// sort those, and then index into the record states map with the sorted key
keys := make([]string, 0)
for k := range ddrmRecordStates {
keys = append(keys, k)
}
slices.Sort(keys)
// turn each record state into a row
for _, k := range keys {
rowState := ddrmRecordStates[k]
processing := ""
if rowState.Processing {
processing = "x"
}
email := ""
if rowState.SentEmail {
email = "x"
}
changed := ""
if rowState.Changed {
changed = "x"
}
errored := ""
if rowState.Errored {
errored = "x"
}
prior := ""
if len(rowState.PriorValues) == 1 {
prior = rowState.PriorValues[0]
} else if len(rowState.PriorValues) > 1 {
prior = rowState.PriorValues[0] + ", ..."
}
current := ""
if len(rowState.CurrentValues) == 1 {
current = rowState.CurrentValues[0]
} else if len(rowState.CurrentValues) > 1 {
current = rowState.CurrentValues[0] + ", ..."
}
row := table.Row{
processing,
rowState.FQDN,
string(rowState.Type),
prior,
current,
email,
changed,
errored,
}
rows = append(rows, row)
}
t := table.New(
table.WithColumns(columns),
table.WithRows(rows),
table.WithFocused(true),
table.WithHeight(len(rows)),
)
style := table.DefaultStyles()
style.Header = style.Header.BorderStyle(lipgloss.NormalBorder()).
BorderForeground(lipgloss.Color("240")).
BorderBottom(true).
Bold(false)
style.Selected = style.Selected.Foreground(lipgloss.Color("229")).
Background(lipgloss.Color("57")).
Bold(false)
t.SetStyles(style)
uiModel := UiModel{t}
return uiModel
}
func sendUpdateUIMsg() {
if stateUI {
ddrmTui.Send(uiProcessRecords{process: true})
}
}
func setupTui() {
if stateUI {
ddrmTui = tea.NewProgram(updateUi())
}
}
func runTui() {
if stateUI {
if _, err := ddrmTui.Run(); err != nil {
os.Exit(ddrmExitErrorRunningTUI)
}
}
}