-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmodel.go
130 lines (113 loc) · 3.28 KB
/
model.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
package tuispeak
import (
"io"
"github.com/charmbracelet/bubbles/list"
tea "github.com/charmbracelet/bubbletea"
"github.com/charmbracelet/lipgloss"
)
type Question struct {
q string
}
func (q Question) FilterValue() string {
return q.q
}
func (q Question) Title() string {
return q.q
}
func (q Question) Description() string {
return ""
}
type Model struct {
lists []list.Model
choices []Container
currentPositionsInLists []int
speaker io.Writer
focusedStyle lipgloss.Style
unfocusedStyle lipgloss.Style
currentlyFocusedList int
}
type Container struct {
Title string
Questions []string
}
func NewModel(containers []Container, speaker io.Writer, focusedStyle lipgloss.Style, unfocusedStyle lipgloss.Style) Model {
lists := make([]list.Model, len(containers))
defaultListPositions := make([]int, len(containers))
for i, container := range containers {
// TODO foreach container, ccreate a list of questions.
lists[i] = list.New([]list.Item{}, list.NewDefaultDelegate(), 50, 20)
lists[i].SetShowHelp(false)
lists[i].Title = container.Title
// TODO create slice of list.Item, set them.
listOfQuestions := make([]list.Item, len(container.Questions))
for j, question := range container.Questions {
listOfQuestions[j] = Question{
q: question,
}
}
lists[i].SetItems(listOfQuestions)
defaultListPositions[0] = 0
}
return Model{
lists: lists,
choices: containers,
speaker: speaker,
focusedStyle: focusedStyle,
unfocusedStyle: unfocusedStyle,
currentPositionsInLists: defaultListPositions,
currentlyFocusedList: 0,
}
}
// Implement the init method.
func (m Model) Init() tea.Cmd {
return nil
}
// Update the model only.
func (m Model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
var cmd tea.Cmd
switch msg := msg.(type) {
case tea.WindowSizeMsg:
h, v := m.focusedStyle.GetFrameSize()
m.lists[m.currentlyFocusedList].SetSize(msg.Width-h, msg.Height-v)
case tea.KeyMsg:
switch msg.String() {
case "s", "enter":
m.speaker.Write([]byte(m.choices[m.currentlyFocusedList].Questions[m.currentPositionsInLists[m.currentlyFocusedList]]))
m.speaker.Write([]byte("\n"))
case "j", "up":
if m.currentPositionsInLists[m.currentlyFocusedList] < len(m.choices[0].Questions)-1 {
m.currentPositionsInLists[m.currentlyFocusedList]++
}
case "k", "down":
if m.currentPositionsInLists[m.currentlyFocusedList] > 0 {
m.currentPositionsInLists[m.currentlyFocusedList]--
}
case "h", "left":
if m.currentlyFocusedList > 0 {
m.currentlyFocusedList--
}
case "l", "right":
if m.currentlyFocusedList < len(m.lists)-1 {
m.currentlyFocusedList++
}
case "q", "x":
return m, tea.Quit
case "n", "tab":
return NewQueryModel(`test`, m.speaker, m), nil
}
}
m.lists[m.currentlyFocusedList], cmd = m.lists[m.currentlyFocusedList].Update(msg)
return m, cmd
}
// Render the model.
func (m Model) View() string {
views := make([]string, len(m.choices))
for i, list := range m.lists {
if i == m.currentlyFocusedList {
views[i] = m.focusedStyle.Render(list.View())
continue
}
views[i] = m.unfocusedStyle.Render(list.View())
}
return lipgloss.JoinHorizontal(lipgloss.Left, views...)
}