-
Notifications
You must be signed in to change notification settings - Fork 8
/
play.go
240 lines (187 loc) · 5.44 KB
/
play.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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
package play
import (
"fmt"
"io"
"strings"
"time"
"github.com/atotto/clipboard"
"github.com/charmbracelet/bubbles/help"
"github.com/charmbracelet/bubbles/key"
"github.com/charmbracelet/bubbles/list"
tea "github.com/charmbracelet/bubbletea"
"github.com/charmbracelet/lipgloss"
"github.com/rusinikita/trainer/challenge"
)
type model struct {
w tea.WindowSizeMsg
l list.Model
b keyBindings
challengeName string
questions []challenge.Question
currentQuestion int
question questionModel
learn learn
backHandler BackHandler
help help.Model
}
const copyStatus = "Code has copied to clipboard"
const copyErrStatus = "Code has not copied. Please install xsel, xclip, wl-clipboard or Termux:API"
type BackHandler func(tea.WindowSizeMsg) (tea.Model, tea.Cmd)
func New(c challenge.Challenge, width, height int, backHandler BackHandler) (tea.Model, tea.Cmd) {
// normalize line endings
codeLines := lines(c.DefaultCodeSnippet)
l := list.New(codeLines, itemDelegate{}, width, height)
l.Title = c.Name
l.SetFilteringEnabled(false)
l.SetShowStatusBar(false)
l.KeyMap.NextPage = key.NewBinding(key.WithDisabled())
l.KeyMap.PrevPage = key.NewBinding(key.WithDisabled())
l.SetShowHelp(false)
l.SetShowPagination(false)
l.InfiniteScrolling = true
l.StatusMessageLifetime = 5 * time.Second
copyStatus := copyStatus
err := clipboard.WriteAll(c.DefaultCodeSnippet)
if err != nil {
copyStatus = errorStyle.Render(copyErrStatus)
}
cmd := l.NewStatusMessage(copyStatus)
helpModel := help.New()
m := model{
b: newBindings(),
w: tea.WindowSizeMsg{
Width: width,
Height: height,
},
l: l,
challengeName: c.Name,
questions: c.Questions,
question: newQuestionModel(c.Questions[0], len(c.Questions) == 1),
learn: newLearn(c.LearningAdvise, c.LearningLinks),
backHandler: backHandler,
help: helpModel,
}
return m.updateListSize(), cmd
}
func (m model) Init() tea.Cmd {
return nil
}
func (m model) allQuestionsAnswered() bool {
return m.currentQuestion == len(m.questions)-1 && m.question.questionFullyAnswered()
}
func (m model) headerView() string {
return ""
}
func (m model) footerView() string {
if m.w.Height < 25 {
return errorStyle.Render("Can't show question. Please, increase terminal window height")
}
number := questionNumberStyle.Render(
fmt.Sprintf("Question %d/%d", m.currentQuestion+1, len(m.questions)),
)
learnSwitch := questionNumberStyle.Render("L - 👁 learn info")
line := lipgloss.JoinHorizontal(
lipgloss.Center,
number,
strings.Repeat("─", m.l.Width()-lipgloss.Width(number)-lipgloss.Width(learnSwitch)),
learnSwitch,
)
var bottomView string
if m.learn.Showed() {
bottomView = m.learn.View(m.w.Width)
} else {
bottomView = m.question.View()
}
views := []string{
line,
bottomView,
helpStyle.Render(m.help.View(m.b)),
}
if m.help.ShowAll || m.learn.Showed() {
views = append(views, helpStyle.Render(copyAdvise))
}
return lipgloss.JoinVertical(lipgloss.Top, views...)
}
func (m model) Update(msg tea.Msg) (r tea.Model, cmd tea.Cmd) {
switch msg := msg.(type) {
case tea.KeyMsg:
switch {
case ValidateBindings(msg):
return m, m.l.NewStatusMessage(RenderLayoutErr(msg))
case key.Matches(msg, m.b.Next):
if m.question.questionFullyAnswered() && (!m.question.isLast) {
m.currentQuestion++
m.question = newQuestionModel(
m.questions[m.currentQuestion],
m.currentQuestion == len(m.questions)-1,
)
}
case key.Matches(msg, m.b.Back):
return m.backHandler(m.w)
case key.Matches(msg, m.b.Help):
m.help.ShowAll = !m.help.ShowAll
case key.Matches(msg, m.b.Quit):
return m, tea.Quit
}
case tea.WindowSizeMsg:
m.w = msg
}
m.learn, cmd = m.learn.Update(msg)
m.b.Learn.SetEnabled(!m.learn.Showed())
m.b.CloseLearn.SetEnabled(m.learn.Showed())
if cmd == nil {
m.l, cmd = m.l.Update(msg)
}
if !m.learn.Showed() {
m.question = m.question.Update(msg, m.l.Index())
}
m.l.Title = fmt.Sprintf("%s %d/%d", m.challengeName, m.l.Index(), len(m.l.Items()))
return m.updateListSize(), cmd
}
func (m model) updateListSize() model {
footerHeight := lipgloss.Height(m.footerView())
listMargin := listStyle.GetVerticalFrameSize()
verticalMarginHeight := footerHeight + listMargin
m.l.SetSize(m.w.Width-listStyle.GetHorizontalFrameSize(), m.w.Height-verticalMarginHeight)
return m
}
func (m model) View() string {
return lipgloss.JoinVertical(
lipgloss.Top,
listStyle.Render(m.l.View()),
m.footerView(),
)
}
type codeLine string
func lines(s string) []list.Item {
ss := strings.Split(strings.ReplaceAll(strings.TrimSpace(s), "\r\n", "\n"), "\n")
l := make([]list.Item, 0, len(ss))
for _, s := range ss {
l = append(l, codeLine(strings.ReplaceAll(s, "\t", " ")))
}
return l
}
func (l codeLine) FilterValue() string {
return string(l)
}
type itemDelegate struct{}
func (d itemDelegate) Height() int { return 1 }
func (d itemDelegate) Spacing() int { return 0 }
func (d itemDelegate) Update(_ tea.Msg, _ *list.Model) tea.Cmd { return nil }
func (d itemDelegate) Render(w io.Writer, m list.Model, index int, listItem list.Item) {
i, ok := listItem.(codeLine)
if !ok {
return
}
str := fmt.Sprintf("%d. %s", index, i)
if index < 10 {
str = " " + str
}
fn := itemStyle.Render
if index == m.Index() {
fn = func(s ...string) string {
return selectedItemStyle.Render("> " + strings.Join(s, " "))
}
}
_, _ = fmt.Fprint(w, fn(str))
}