-
Notifications
You must be signed in to change notification settings - Fork 0
/
model.go
335 lines (293 loc) · 9.77 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
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
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
package main
import (
"fmt"
"strconv"
"strings"
log "github.com/dominickp/hn/logger"
"github.com/charmbracelet/bubbles/viewport"
tea "github.com/charmbracelet/bubbletea"
"github.com/charmbracelet/lipgloss"
"github.com/dominickp/hn/client"
"github.com/dominickp/hn/util"
)
type model struct {
choices []string // items on the to-do list
topMenuResponse client.TopMenuResponse
cursor int // which to-do list item our cursor is pointing at
err error // an error to display, if any
nextTopicId int // Next topic ID we should fetch
topicHistoryStack []client.Item // Stack of topics we've visited
ready bool // Whether the program is ready to render
viewport viewport.Model
pageSize int
currentPage int
}
func initialModel() model {
return model{
choices: []string{},
pageSize: 15,
currentPage: 1,
}
}
// getCurrentTopic returns the current topic we're viewing from the top of the stack
func (m model) getCurrentTopic() *client.Item {
if len(m.topicHistoryStack) > 0 {
return &m.topicHistoryStack[len(m.topicHistoryStack)-1]
}
return nil
}
func (m model) Init() tea.Cmd {
return func() tea.Msg {
// Don't draw the top menu until we have the viewport size ready
if m.ready {
return checkTopMenu(m.pageSize, m.currentPage) // Get the top 500 stories and save to our cache
}
return checkNothing()
}
}
func (m model) RedrawPage() tea.Cmd {
return func() tea.Msg {
if m.getCurrentTopic() != nil {
return checkTopic(m.getCurrentTopic().Id)
}
if len(m.topMenuResponse.Items) > 0 {
return checkTopMenuPage(m.topMenuResponse, m.pageSize, m.currentPage)
}
return checkNothing()
}
}
func (m model) InitTopic() tea.Cmd {
return func() tea.Msg {
if m.nextTopicId != 0 {
return checkTopic(m.nextTopicId)
}
return checkNothing()
}
}
// getTopMenuCurrentPageChoices returns the current page of saved top menu items as choices
func getTopMenuCurrentPageChoices(m model) []string {
choices := make([]string, m.pageSize)
start := (m.currentPage - 1) * m.pageSize
end := min(start+m.pageSize, len(m.topMenuResponse.Items))
for i, item := range m.topMenuResponse.Items[start:end] {
choices[i] = fmt.Sprintf("%s %s", util.ScoreStyle.Render(util.PadRight(strconv.Itoa(item.Score), 4)), item.Title)
}
return choices
}
// Update is called when "things happen." Its job is to look at what has happened and return an updated model in
// response. It can also return a Cmd to make more things happen.
func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
var (
cmd tea.Cmd
cmds []tea.Cmd
)
switch msg := msg.(type) {
case topMenuMsg:
// The server returned a top menu response message. Save it to our model.
m.topMenuResponse = client.TopMenuResponse(msg)
m.choices = getTopMenuCurrentPageChoices(m)
m.viewport.SetContent(getContent(m))
return m, nil
case checkTopMenuPageMsg:
// We should just update the choices with the current top menu data we have.
m.choices = getTopMenuCurrentPageChoices(m)
m.viewport.SetContent(getContent(m))
return m, nil
case topicMsg:
item := client.Item(msg)
if m.getCurrentTopic() == nil || m.getCurrentTopic().Id != item.Id {
m.topicHistoryStack = append(m.topicHistoryStack, item)
}
topic := m.getCurrentTopic()
// Set comments as choices
choices := make([]string, len(topic.Comments))
for i, comment := range topic.Comments {
commentText := util.HtmlToText(comment.Text)
replies := ""
if len(comment.Kids) > 0 {
replies = fmt.Sprintf(" (%d replies)", len(comment.Kids))
}
choices[i] = fmt.Sprintf(
"%s\n%s",
util.CommentAuthorStyle.Render(comment.By+replies),
util.CommentTextStyle.Width(m.viewport.Width-util.CommentTextStyle.GetHorizontalMargins()).
Render(commentText),
)
}
m.choices = choices
m.viewport.SetContent(getContent(m))
return m, tea.ClearScreen
case errMsg:
// There was an error. Note it in the model. And tell the runtime
// we're done and want to quit.
m.err = msg
return m, tea.Quit
case tea.WindowSizeMsg:
headerHeight := lipgloss.Height(m.headerView())
footerHeight := lipgloss.Height(m.footerView())
verticalMarginHeight := headerHeight + footerHeight
if !m.ready {
// Since this program is using the full size of the viewport we
// need to wait until we've received the window dimensions before
// we can initialize the viewport. The initial dimensions come in
// quickly, though asynchronously, which is why we wait for them
// here.
m.viewport = viewport.New(msg.Width, msg.Height-verticalMarginHeight)
m.viewport.YPosition = headerHeight
m.viewport.HighPerformanceRendering = false
m.viewport.SetContent(getContent(m))
m.ready = true
// Render the viewport one line below the header.
m.viewport.YPosition = headerHeight + 1
// Updatee the page size to call for more items per page if we can fit them
m.pageSize = m.viewport.Height - 1
return m, tea.Cmd(m.Init())
} else {
m.viewport.Width = msg.Width
m.viewport.Height = msg.Height - verticalMarginHeight
}
case tea.KeyMsg:
// Cool, what was the actual key pressed?
switch msg.String() {
// These keys should exit the program.
case "ctrl+c", "q":
return m, tea.Quit
// The "up" and "k" keys move the cursor up
case "up", "k":
if m.cursor > 0 {
m.cursor--
}
// The "down" and "j" keys move the cursor down
case "down", "j":
if m.cursor < len(m.choices)-1 {
m.cursor++
}
case "right":
if m.getCurrentTopic() == nil {
m.currentPage++
m.cursor = 0
return m, tea.Cmd(m.RedrawPage())
}
case "left":
if m.getCurrentTopic() == nil && m.currentPage > 1 {
m.currentPage--
m.cursor = 0
return m, tea.Cmd(m.RedrawPage())
}
// The "enter" key and the spacebar (a literal space) toggle
// the selected state for the item that the cursor is pointing at.
case "enter", " ":
// Find the item that the cursor is pointing at
var newTopic client.Item
topic := m.getCurrentTopic()
if topic != nil {
m.nextTopicId = topic.Kids[m.cursor]
} else {
// Viewing the top menu, clicking a topic for the first time
itemIndex := (m.currentPage-1)*m.pageSize + m.cursor
newTopic = m.topMenuResponse.Items[itemIndex]
m.nextTopicId = newTopic.Id
}
m.cursor = 0
m.viewport.GotoTop()
return m, tea.Cmd(m.InitTopic())
case "backspace":
if m.getCurrentTopic() != nil {
m.topicHistoryStack = m.topicHistoryStack[:len(m.topicHistoryStack)-1]
} else {
// Let the user use backspace to navigate backwards as well
if m.currentPage > 1 {
m.currentPage--
}
}
m.cursor = 0 // FIXME: Should have two cursors, so we can remember top meny cursor location when nav back
m.viewport.GotoTop()
return m, tea.Cmd(m.RedrawPage())
case "f5":
// Clear cache of top menu items
m.topMenuResponse = client.TopMenuResponse{}
// Go back to page 1
m.currentPage = 1
return m, tea.Cmd(m.Init())
}
}
log.Logger.Printf("Current topic: %v", m.getCurrentTopic())
m.viewport.SetContent(getContent(m))
// Handle keyboard and mouse events in the viewport
m.viewport, cmd = m.viewport.Update(msg)
cmds = append(cmds, cmd)
return m, tea.Batch(cmds...)
}
// getContent returns the content to be displayed in the viewport.
func getContent(m model) string {
var s string = ""
topic := m.getCurrentTopic()
if topic != nil {
// Render topic view
if topic.Title != "" {
s += fmt.Sprintf("%s\n", util.TitleStyle.Render(topic.Title))
}
if topic.By != "" {
byLine := util.TopicAuthorStyle.Render(fmt.Sprintf("By %s (%d comments)", topic.By, len(topic.Kids)))
s += fmt.Sprintf("%s\n", byLine)
}
if topic.Text != "" {
s += fmt.Sprintf(
"%s\n",
util.TopicTextStyle.Width(m.viewport.Width-util.TopicTextStyle.GetHorizontalMargins()).
Render(util.HtmlToText(topic.Text)))
}
if topic.Url != "" {
s += fmt.Sprintf("→ %s\n", util.LinkStyle.Render(topic.Url))
}
s += "\n"
}
// Iterate over our choices
for i, choice := range m.choices {
// Is the cursor pointing at this choice?
cursor := " " // no cursor
if m.cursor == i {
cursor = ">" // cursor!
}
// Render the row
s += fmt.Sprintf("%s %s\n", util.CursorStyle.Render(cursor), choice)
}
return s
}
// View is called when the program wants to render the UI. It returns a string.
func (m model) View() string {
// Send the UI for rendering
if !m.ready {
return "\n Initializing..."
}
return fmt.Sprintf("%s\n%s\n%s", m.headerView(), m.viewport.View(), m.footerView())
}
// headerView returns the header view for the paginated viewport.
func (m model) headerView() string {
title := util.TitleBoxStyle.Render("Hacker News")
// Create a breadcrumb line so people can see where they are in the navigation
breadCrumbLine := "──"
for i, topic := range m.topicHistoryStack {
if i > 0 {
breadCrumbLine += ">"
}
breadCrumbLine += fmt.Sprintf(" %s ", topic.By)
}
line := breadCrumbLine + strings.Repeat("─", max(0, m.viewport.Width-lipgloss.Width(title+breadCrumbLine)))
return lipgloss.JoinHorizontal(lipgloss.Center, title, line)
}
// footerView returns the footer view for the paginated viewport.
func (m model) footerView() string {
navMessage := "Press q to quit, ←/→ to paginate, F5 to refresh."
if m.getCurrentTopic() != nil {
// Topic view
navMessage = "Press q to quit, backspace to go back."
}
navHelpLine := fmt.Sprintf("─── %s ", navMessage)
infoText := util.InfoBoxStyle.Render(fmt.Sprintf("Page %d", m.currentPage))
if m.getCurrentTopic() != nil {
infoText = util.InfoBoxStyle.Render(fmt.Sprintf("%3.f%%", m.viewport.ScrollPercent()*100))
}
line := navHelpLine + strings.Repeat("─", max(0, m.viewport.Width-lipgloss.Width(infoText+navHelpLine)))
return lipgloss.JoinHorizontal(lipgloss.Center, line, infoText)
}