-
Notifications
You must be signed in to change notification settings - Fork 0
/
configModel.go
121 lines (103 loc) · 2.79 KB
/
configModel.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
package tui
import (
"github.com/buonotti/apisense/errors"
"github.com/charmbracelet/bubbles/key"
"github.com/charmbracelet/bubbles/table"
tea "github.com/charmbracelet/bubbletea"
"github.com/charmbracelet/lipgloss"
"strconv"
)
var (
selectedConfig = 0
updateSelectConfigRows = true
)
type configModel struct {
keymap keymap
err error
table table.Model
editConfigModel tea.Model
}
func ConfigModel() tea.Model {
t := table.New(
table.WithColumns(getConfigColumns()),
table.WithRows(getConfigRows()),
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 configModel{
keymap: DefaultKeyMap,
err: nil,
table: t,
editConfigModel: SelectConfigModel(),
}
}
func getConfigColumns() []table.Column {
return []table.Column{
{Title: "", Width: 3},
{Title: "Config", Width: 30},
{Title: "Description", Width: 42},
}
}
func getConfigRows() []table.Row {
rows := []table.Row{
{"0", "env", "Edit env values"},
{"1", "daemon", "Configure daemon"},
{"2", "ssh", "Configure ssh"},
{"3", "api", "Configure api"},
{"4", "tui", "Configure tui"},
}
return rows
}
func (c configModel) Init() tea.Cmd {
return nil
}
func (c configModel) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
var cmd tea.Cmd
var cmdModel tea.Cmd
if choiceConfigModel != "" {
switch msg := msg.(type) {
case tea.KeyMsg:
switch {
case key.Matches(msg, c.keymap.back):
if choiceConfigModel == "configModel" {
choiceConfigModel = ""
choiceMainMenu = ""
}
case key.Matches(msg, c.keymap.quit):
return c, tea.Quit
case key.Matches(msg, c.keymap.choose):
i, err := strconv.Atoi(c.table.SelectedRow()[0])
errors.CheckErr(err)
if choiceConfigModel != "configModel" {
c.editConfigModel, cmdModel = c.editConfigModel.Update(msg)
c.table, cmd = c.table.Update(msg)
return c, tea.Batch(cmd, cmdModel)
}
selectedConfig = i
updateSelectConfigRows = true
choiceConfigModel = "selectConfigModel"
c.table, cmd = c.table.Update(msg)
return c, cmd
}
case errMsg:
c.err = msg
errors.CheckErr(c.err)
}
c.editConfigModel, cmdModel = c.editConfigModel.Update(msg)
c.table, cmd = c.table.Update(msg)
return c, tea.Batch(cmd, cmdModel)
}
return c, nil
}
func (c configModel) View() string {
if choiceConfigModel != "configModel" {
return styleContentCenter.Copy().MarginLeft(1).MarginRight(1).BorderStyle(lipgloss.RoundedBorder()).Render(c.editConfigModel.View() + "\n")
}
return styleContentCenter.Copy().MarginLeft(1).MarginRight(1).BorderStyle(lipgloss.RoundedBorder()).Render(c.table.View() + "\n")
}