-
Notifications
You must be signed in to change notification settings - Fork 24
/
list.go
1137 lines (863 loc) · 26.1 KB
/
list.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
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
package list
import (
_ "embed"
"fmt"
"io"
"strconv"
"strings"
"time"
"clx/app"
"github.com/charmbracelet/bubbles/viewport"
"clx/reader"
"clx/browser"
"clx/bubble/list/message"
"clx/bubble/ranking"
"clx/cli"
"clx/constants/category"
"clx/constants/style"
"clx/favorites"
"clx/header"
"clx/help"
"clx/history"
"clx/hn"
"clx/hn/services/hybrid"
"clx/hn/services/mock"
"clx/item"
"clx/screen"
"clx/settings"
"clx/tree"
"clx/validator"
"github.com/charmbracelet/bubbles/paginator"
"github.com/charmbracelet/bubbles/spinner"
tea "github.com/charmbracelet/bubbletea"
"github.com/charmbracelet/lipgloss"
)
const (
numberOfCategories = 5
)
// Item is an item that appears in the list.
// type Item interface{}
// ItemDelegate encapsulates the general functionality for all list items. The
// benefit to separating this logic from the item itself is that you can change
// the functionality of items without changing the actual items themselves.
//
// Note that if the delegate also implements help.KeyMap delegate-related
// help items will be added to the help view.
type ItemDelegate interface {
// Render renders the item's view.
Render(w io.Writer, m Model, index int, item *item.Item)
// Height is the height of the list item.
Height() int
// Spacing is the size of the horizontal gap between list items in cells.
Spacing() int
// Update is the update loop for items. All messages in the list's update
// loop will pass through here except when the user is setting a filter.
// Use this method to perform item-level updates appropriate to this
// delegate.
Update(msg tea.Msg, m *Model) tea.Cmd
}
// Model contains the state of this component.
type Model struct {
showTitle bool
showStatusBar bool
disableInput bool
Title string
Styles Styles
spinner spinner.Model
showSpinner bool
width int
height int
Paginator paginator.Model
cursor int
onStartup bool
isVisible bool
onAddToFavoritesPrompt bool
onRemoveFromFavoritesPrompt bool
StatusMessageLifetime time.Duration
statusMessage string
statusMessageTimer *time.Timer
category int
categoryToDisplay int
items [][]*item.Item
delegate ItemDelegate
history history.History
config *settings.Config
service hn.Service
favorites *favorites.Favorites
isOnHelpScreen bool
viewport viewport.Model
}
func (m *Model) FetchFrontPageStories() tea.Cmd {
return func() tea.Msg {
itemsToFetch := m.getNumberOfItemsToFetch(m.category)
stories := m.service.FetchItems(itemsToFetch, category.FrontPage)
m.items[category.FrontPage] = stories
return message.FetchingFinished{}
}
}
func (m *Model) getNumberOfItemsToFetch(cat int) int {
switch cat {
case category.FrontPage:
return m.Paginator.PerPage * 3
case category.New:
return m.Paginator.PerPage * 3
case category.Ask:
return m.Paginator.PerPage
case category.Show:
return m.Paginator.PerPage
default:
return m.Paginator.PerPage
}
}
func New(delegate ItemDelegate, config *settings.Config, favorites *favorites.Favorites, width, height int) Model {
styles := DefaultStyles()
sp := spinner.New()
sp.Spinner = getSpinner()
sp.Style = styles.Spinner
p := paginator.New()
p.Type = paginator.Dots
p.ActiveDot = styles.ActivePaginationDot.String()
p.InactiveDot = styles.InactivePaginationDot.String()
p.UseHLKeys = false
p.UseJKKeys = false
p.UseLeftRightKeys = false
p.UsePgUpPgDownKeys = false
p.UseUpDownKeys = false
bufferCategory := 1
items := make([][]*item.Item, numberOfCategories+bufferCategory)
m := Model{
showTitle: true,
showStatusBar: true,
Styles: styles,
Title: "List",
StatusMessageLifetime: time.Second,
width: width,
height: height,
delegate: delegate,
history: getHistory(config.DebugMode, config.DoNotMarkSubmissionsAsRead),
items: items,
Paginator: p,
spinner: sp,
onStartup: true,
isVisible: true,
disableInput: true,
config: config,
service: getService(config.DebugMode),
favorites: favorites,
}
m.updatePagination()
return m
}
func getHistory(debugMode bool, doNotMarkAsRead bool) history.History {
if debugMode {
return history.NewMockHistory()
}
if doNotMarkAsRead {
return history.NewNonPersistentHistory()
}
return history.NewPersistentHistory()
}
func getService(debugMode bool) hn.Service {
if debugMode {
return mock.Service{}
}
return &hybrid.Service{}
}
// SetShowTitle shows or hides the title bar.
func (m *Model) SetShowTitle(v bool) {
m.showTitle = v
m.updatePagination()
}
func (m *Model) SetIsVisible(v bool) {
m.isVisible = v
}
// SetShowStatusBar shows or hides the view that displays metadata about the
// list, such as item counts.
func (m *Model) SetShowStatusBar(v bool) {
m.showStatusBar = v
m.updatePagination()
}
// ShowStatusBar returns whether or not the status bar is set to be rendered.
func (m Model) ShowStatusBar() bool {
return m.showStatusBar
}
// Set the items available in the list. This returns a command.
func (m *Model) SetItems(i []*item.Item) tea.Cmd {
var cmd tea.Cmd
m.items[m.category] = i
m.updatePagination()
return cmd
}
// Select selects the given index of the list and goes to its respective page.
func (m *Model) Select(index int) {
m.Paginator.Page = index / m.Paginator.PerPage
m.cursor = index % m.Paginator.PerPage
}
// VisibleItems returns the total items available to be shown.
func (m Model) VisibleItems() []*item.Item {
return m.items[m.category]
}
// SelectedItems returns the current selected item in the list.
func (m Model) SelectedItem() *item.Item {
i := m.Index()
items := m.VisibleItems()
if i < 0 || len(items) == 0 || len(items) <= i {
// return nil
return &item.Item{}
}
return items[i]
}
// Index returns the index of the currently selected item as it appears in the
// entire slice of items.
func (m Model) Index() int {
return m.Paginator.Page*m.Paginator.PerPage + m.cursor
}
// Cursor returns the index of the cursor on the current page.
func (m Model) Cursor() int {
return m.cursor
}
// CursorUp moves the cursor up. This can also move the state to the previous
// page.
func (m *Model) CursorUp() {
m.cursor--
// If we're at the top, stop
if m.cursor < 0 {
m.cursor = 0
return
}
return
}
// CursorDown moves the cursor down. This can also advance the state to the
// next page.
func (m *Model) CursorDown() {
itemsOnPage := m.Paginator.ItemsOnPage(len(m.VisibleItems()))
m.cursor++
// If we're at the end, stop
if m.cursor < itemsOnPage {
return
}
m.cursor = itemsOnPage - 1
}
func (m *Model) getNextCategory() int {
isAtLastCategory := m.category == m.getNumberOfCategories()-1
if isAtLastCategory {
return category.FrontPage
}
return m.category + 1
}
func (m *Model) getNumberOfCategories() int {
if m.favorites.HasItems() {
return numberOfCategories
}
return numberOfCategories - 1
}
func (m *Model) getPrevCategory() int {
isAtFirstCategory := m.category == category.FrontPage
if isAtFirstCategory && m.favorites.HasItems() {
return category.Favorites
}
if isAtFirstCategory {
return category.Show
}
return m.category - 1
}
func (m *Model) ToggleSpinner() tea.Cmd {
if !m.showSpinner {
return m.StartSpinner()
}
m.StopSpinner()
return nil
}
func (m *Model) StartSpinner() tea.Cmd {
// Hack: I can't get the spinner to reset properly. As a workaround, we
// instantiate a new spinner each time we want to show it.
m.spinner = spinner.New()
m.spinner.Spinner = getSpinner()
m.spinner.Style = DefaultStyles().Spinner
m.showSpinner = true
return m.spinner.Tick
}
func (m *Model) StopSpinner() {
m.showSpinner = false
}
func (m *Model) NewStatusMessage(s string) tea.Cmd {
m.statusMessage = s
if m.statusMessageTimer != nil {
m.statusMessageTimer.Stop()
}
m.statusMessageTimer = time.NewTimer(m.StatusMessageLifetime)
// Wait for timeout
return func() tea.Msg {
<-m.statusMessageTimer.C
return message.StatusMessageTimeout{}
}
}
func (m *Model) NewStatusMessageWithDuration(s string, d time.Duration) tea.Cmd {
m.statusMessage = lipgloss.NewStyle().
Foreground(style.GetUnselectedItemFg()).
Render(s)
if m.statusMessageTimer != nil {
m.statusMessageTimer.Stop()
}
m.statusMessageTimer = time.NewTimer(d)
// Wait for timeout
return func() tea.Msg {
<-m.statusMessageTimer.C
return message.StatusMessageTimeout{}
}
}
func (m *Model) SetPermanentStatusMessage(s string, faint bool) {
m.statusMessage = lipgloss.NewStyle().
Foreground(style.GetUnselectedItemFg()).
Faint(faint).
Render(s)
}
// SetSize sets the width and height of this component.
func (m *Model) SetSize(width, height int) {
m.setSize(width, height)
}
func (m *Model) setSize(width, height int) {
m.width = width
m.height = height
m.updatePagination()
}
// Update pagination according to the amount of items for the current state.
func (m *Model) updatePagination() {
index := m.Index()
availHeight := m.height
if m.showTitle {
availHeight -= lipgloss.Height(m.titleView())
}
if m.showStatusBar {
availHeight -= lipgloss.Height(m.statusView())
}
m.Paginator.PerPage = max(1, availHeight/(m.delegate.Height()+m.delegate.Spacing()))
if pages := len(m.VisibleItems()); pages < 1 {
m.Paginator.SetTotalPages(1)
} else {
m.Paginator.SetTotalPages(pages)
}
// Restore index
m.Paginator.Page = index / m.Paginator.PerPage
m.cursor = index % m.Paginator.PerPage
// Make sure the page stays in bounds
if m.Paginator.Page >= m.Paginator.TotalPages-1 {
m.Paginator.Page = max(0, m.Paginator.TotalPages-1)
}
}
func (m *Model) hideStatusMessage() {
m.statusMessage = ""
if m.statusMessageTimer != nil {
m.statusMessageTimer.Stop()
}
}
func (m Model) Update(msg tea.Msg) (Model, tea.Cmd) {
windowSizeMsg, isWindowSizeMsg := msg.(tea.WindowSizeMsg)
// 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.
if m.onStartup && !isWindowSizeMsg {
return m, nil
}
if m.onStartup && isWindowSizeMsg {
h, v := lipgloss.NewStyle().GetFrameSize()
m.SetSize(windowSizeMsg.Width-h, windowSizeMsg.Height-v)
var cmds []tea.Cmd
spinnerCmd := m.StartSpinner()
cmds = append(cmds, spinnerCmd)
m.SetOnStartup(false)
m.items[category.Favorites] = m.favorites.GetItems()
fetchCmd := m.FetchFrontPageStories()
cmds = append(cmds, fetchCmd)
heightOfHeaderAndStatusLine := 2
m.viewport = viewport.New(windowSizeMsg.Width, windowSizeMsg.Height-heightOfHeaderAndStatusLine)
m.viewport.YPosition = 2
m.viewport.HighPerformanceRendering = false
content := lipgloss.NewStyle().
Width(windowSizeMsg.Width).
AlignHorizontal(lipgloss.Center).
SetString(help.GetHelpScreen(m.config.EnableNerdFonts))
m.viewport.SetContent(content.String())
return m, tea.Batch(cmds...)
}
var cmds []tea.Cmd
switch msg := msg.(type) {
case spinner.TickMsg:
newSpinnerModel, cmd := m.spinner.Update(msg)
m.spinner = newSpinnerModel
if m.showSpinner {
cmds = append(cmds, cmd)
}
case message.FetchingFinished:
m.StopSpinner()
h, v := lipgloss.NewStyle().GetFrameSize()
m.setSize(screen.GetTerminalWidth()-h, screen.GetTerminalHeight()-v)
m.disableInput = false
return m, nil
case message.StatusMessageTimeout:
m.hideStatusMessage()
case message.AddToFavorites:
m.favorites.Add(msg.Item)
m.items[category.Favorites] = m.favorites.GetItems()
m.favorites.Write()
m.updatePagination()
case tea.WindowSizeMsg:
h, v := lipgloss.NewStyle().GetFrameSize()
m.SetSize(msg.Width-h, msg.Height-v)
headerHeight := lipgloss.Height("")
footerHeight := lipgloss.Height("")
verticalMarginHeight := headerHeight + footerHeight
m.viewport.Width = msg.Width
m.viewport.Height = msg.Height - verticalMarginHeight
m.width = msg.Width
m.height = msg.Height
content := lipgloss.NewStyle().
Width(msg.Width).
AlignHorizontal(lipgloss.Center).
SetString(help.GetHelpScreen(m.config.EnableNerdFonts))
m.viewport.SetContent(content.String())
return m, nil
case message.EnteringCommentSection:
lastVisited := m.history.GetLastVisited(msg.Id)
m.history.MarkAsReadAndWriteToDisk(msg.Id, msg.CommentCount)
story := m.service.FetchComments(msg.Id)
if m.category == category.Favorites {
m.favorites.UpdateStoryAndWriteToDisk(story)
}
commentTree := tree.Print(story, m.config, m.width, lastVisited)
command := cli.Less(commentTree, m.config)
return m, tea.ExecProcess(command, func(err error) tea.Msg {
return message.EditorFinishedMsg{Err: err}
})
case message.EnteringReaderMode:
errorMessage := validator.GetErrorMessage(msg.Title, msg.Domain)
if errorMessage != "" {
cmds = append(cmds, m.NewStatusMessageWithDuration(errorMessage, time.Second*3))
cmds = append(cmds, func() tea.Msg {
return message.EditorFinishedMsg{Err: nil}
})
return m, tea.Batch(cmds...)
}
article, err := reader.GetArticle(msg.Url, msg.Title, m.config.CommentWidth, m.config.IndentationSymbol)
if err != nil {
panic(err)
}
command := cli.Less(article, m.config)
return m, tea.ExecProcess(command, func(err error) tea.Msg {
return message.EditorFinishedMsg{Err: err}
})
case message.EditorFinishedMsg:
m.SetIsVisible(true)
m.SetDisabledInput(false)
case message.ChangeCategory:
return m, func() tea.Msg {
itemsToFetch := m.getNumberOfItemsToFetch(msg.Category)
stories := m.service.FetchItems(itemsToFetch, msg.Category)
m.items[msg.Category] = stories
return message.CategoryFetchingFinished{Category: msg.Category, Cursor: msg.Cursor}
}
case message.CategoryFetchingFinished:
m.Paginator.Page = 0
m.SetDisabledInput(false)
m.StopSpinner()
m.category = msg.Category
itemsOnPage := m.Paginator.ItemsOnPage(len(m.VisibleItems()))
m.cursor = min(msg.Cursor, itemsOnPage-1)
m.updatePagination()
}
if m.isOnHelpScreen {
return m.updateHelpScreen(msg)
}
cmds = append(cmds, m.handleBrowsing(msg))
return m, tea.Batch(cmds...)
}
func (m Model) updateHelpScreen(msg tea.Msg) (Model, tea.Cmd) {
var (
cmd tea.Cmd
cmds []tea.Cmd
)
switch msg := msg.(type) {
case tea.KeyMsg:
if k := msg.String(); k == "ctrl+c" || k == "q" || k == "esc" || k == "i" || k == "?" {
m.isOnHelpScreen = false
return m, nil
}
case tea.WindowSizeMsg:
h, v := lipgloss.NewStyle().GetFrameSize()
m.SetSize(msg.Width-h, msg.Height-v)
headerHeight := lipgloss.Height("")
footerHeight := lipgloss.Height("")
verticalMarginHeight := headerHeight + footerHeight
m.viewport.Width = msg.Width
m.viewport.Height = msg.Height - verticalMarginHeight
m.width = msg.Width
m.height = msg.Height
content := lipgloss.NewStyle().
Width(msg.Width).
AlignHorizontal(lipgloss.Center).
SetString(help.GetHelpScreen(m.config.EnableNerdFonts))
m.viewport.SetContent(content.String())
return m, viewport.Sync(m.viewport)
}
// Handle keyboard and mouse events in the viewport
m.viewport, cmd = m.viewport.Update(msg)
cmds = append(cmds, cmd)
return m, tea.Batch(cmds...)
}
func (m *Model) updateCursor() {
m.cursor = min(m.cursor, m.Paginator.ItemsOnPage(len(m.VisibleItems()))-1)
}
func (m *Model) categoryHasStories(cat int) bool {
return len(m.items[cat]) != 0
}
func (m *Model) changeToCategory(cat int) {
m.category = cat
m.categoryToDisplay = m.category
m.Paginator.Page = 0
m.cursor = min(m.cursor, len(m.items[m.category])-1)
m.updatePagination()
}
func (m *Model) handleBrowsing(msg tea.Msg) tea.Cmd {
var cmds []tea.Cmd
numItems := len(m.VisibleItems())
switch msg := msg.(type) {
case tea.KeyMsg:
switch {
case msg.String() == "i" || msg.String() == "?":
m.isOnHelpScreen = true
return nil
case m.onAddToFavoritesPrompt && msg.String() == "y":
m.onAddToFavoritesPrompt = false
m.disableInput = false
addToFavorites := func() tea.Msg {
return message.AddToFavorites{Item: m.SelectedItem()}
}
cmds = append(cmds, addToFavorites)
cmds = append(cmds, m.NewStatusMessageWithDuration("Item added", time.Second*2))
return tea.Batch(cmds...)
case m.onRemoveFromFavoritesPrompt && msg.String() == "y":
m.onRemoveFromFavoritesPrompt = false
m.disableInput = false
//
m.favorites.Remove(m.Index())
m.items[category.Favorites] = m.favorites.GetItems()
m.favorites.Write()
//
isOnLastItem := m.Index() == len(m.items[category.Favorites])
hasOnlyOneItem := len(m.items[category.Favorites]) == 0
itemRemovedMessage := "Item removed"
if hasOnlyOneItem {
m.cursor = 0
cmds = append(cmds, func() tea.Msg {
return message.ChangeCategory{Category: category.FrontPage, Cursor: m.cursor}
})
cmds = append(cmds, m.NewStatusMessageWithDuration(itemRemovedMessage, time.Second*2))
return tea.Batch(cmds...)
}
if isOnLastItem {
m.cursor = m.cursor - 1
}
m.updatePagination()
return m.NewStatusMessageWithDuration(itemRemovedMessage, time.Second*2)
case m.onAddToFavoritesPrompt || m.onRemoveFromFavoritesPrompt:
m.onAddToFavoritesPrompt = false
m.onRemoveFromFavoritesPrompt = false
m.disableInput = false
m.hideStatusMessage()
return nil
case m.disableInput && msg.String() != "r":
return nil
case msg.String() == "q" || msg.String() == "esc" || msg.String() == "ctrl+c":
return tea.Quit
case msg.String() == "up" || msg.String() == "k":
m.CursorUp()
return nil
case msg.String() == "down" || msg.String() == "j":
m.CursorDown()
return nil
case msg.String() == "left" || msg.String() == "h":
m.Paginator.PrevPage()
m.updateCursor()
return nil
case msg.String() == "right" || msg.String() == "l":
m.Paginator.NextPage()
m.updateCursor()
return nil
case msg.String() == "tab":
nextCat := m.getNextCategory()
if m.categoryHasStories(nextCat) {
m.changeToCategory(nextCat)
return nil
}
m.SetDisabledInput(true)
startSpinnerCmd := m.StartSpinner()
m.categoryToDisplay = nextCat
changeCatCmd := func() tea.Msg {
return message.ChangeCategory{Category: nextCat, Cursor: m.cursor}
}
cmds = append(cmds, startSpinnerCmd)
cmds = append(cmds, changeCatCmd)
return tea.Batch(cmds...)
case msg.String() == "shift+tab":
prevCat := m.getPrevCategory()
if m.categoryHasStories(prevCat) {
m.changeToCategory(prevCat)
return nil
}
m.SetDisabledInput(true)
startSpinnerCmd := m.StartSpinner()
m.categoryToDisplay = prevCat
changeCatCmd := func() tea.Msg {
return message.ChangeCategory{Category: prevCat, Cursor: m.cursor}
}
cmds = append(cmds, startSpinnerCmd)
cmds = append(cmds, changeCatCmd)
return tea.Batch(cmds...)
case msg.String() == "g":
m.cursor = 0
return nil
case msg.String() == "G":
m.cursor = m.Paginator.ItemsOnPage(numItems) - 1
return nil
case msg.String() == "o":
if m.SelectedItem().URL == "" {
url := "https://news.ycombinator.com/item?id=" + strconv.Itoa(m.SelectedItem().ID)
browser.Open(url)
return nil
}
browser.Open(m.SelectedItem().URL)
return nil
case msg.String() == "c":
url := "https://news.ycombinator.com/item?id=" + strconv.Itoa(m.SelectedItem().ID)
browser.Open(url)
return nil
case msg.String() == "r" && m.category != category.Favorites:
currentCategory := m.category
currentPage := m.Paginator.Page
m.items[category.Buffer] = m.items[m.category]
m.category = category.Buffer
m.Paginator.Page = 0
m.cursor = min(m.cursor, len(m.items[m.category])-1)
m.updatePagination()
m.items[category.FrontPage] = []*item.Item{}
m.items[category.New] = []*item.Item{}
m.items[category.Ask] = []*item.Item{}
m.items[category.Show] = []*item.Item{}
m.SetDisabledInput(true)
m.cursor = 0
m.Paginator.Page = currentPage
changeCatCmd := func() tea.Msg {
return message.ChangeCategory{Category: currentCategory, Cursor: m.cursor}
}
cmds = append(cmds, m.StartSpinner())
cmds = append(cmds, changeCatCmd)
return tea.Batch(cmds...)
case msg.String() == "f" || msg.String() == "V":
m.SetPermanentStatusMessage(getAddItemConfirmationMessage(), false)
m.onAddToFavoritesPrompt = true
m.disableInput = true
return nil
case msg.String() == "x" && m.category == category.Favorites:
m.SetPermanentStatusMessage(getRemoveItemConfirmationMessage(), false)
m.onRemoveFromFavoritesPrompt = true
m.disableInput = true
return nil
case msg.String() == "enter":
m.SetIsVisible(false)
m.SetDisabledInput(true)
cmd := func() tea.Msg {
return message.EnteringCommentSection{
Id: m.SelectedItem().ID,
CommentCount: m.SelectedItem().CommentsCount,
}
}
return cmd
case msg.String() == " ":
m.SetIsVisible(false)
m.SetDisabledInput(true)
return func() tea.Msg {
return message.EnteringReaderMode{
Url: m.SelectedItem().URL,
Title: m.SelectedItem().Title,
Domain: m.SelectedItem().Domain,
}
}
}
}
cmd := m.delegate.Update(msg, m)
cmds = append(cmds, cmd)
// Keep the index in bounds when paginating
itemsOnPage := m.Paginator.ItemsOnPage(len(m.VisibleItems()))
if m.cursor > itemsOnPage-1 {
m.cursor = max(0, itemsOnPage-1)
}
return tea.Batch(cmds...)
}
func (m *Model) showHelpScreen() tea.Cmd {
helpScreen := help.GetHelpScreen(m.config.EnableNerdFonts)
command := cli.Less(helpScreen, m.config)
return tea.ExecProcess(command, func(err error) tea.Msg {
return message.EditorFinishedMsg{Err: err}
})
}
// View renders the component.
func (m Model) View() string {
if m.isOnHelpScreen {
return fmt.Sprintf("%s\n%s\n%s", header.GetHeader(m.categoryToDisplay, m.favorites.HasItems(), m.width),
m.viewport.View(),
m.statusAndPaginationView())
}
var (
sections []string
availHeight = m.height
)
if !m.isVisible {
return ""
}
if m.showTitle {
v := m.titleView()
sections = append(sections, v)
availHeight -= lipgloss.Height(v)
}
if m.showStatusBar {
v := m.statusView()
availHeight -= lipgloss.Height(v)
}
content := lipgloss.NewStyle().Height(availHeight).Render(m.populatedView())
rankings := ranking.GetRankings(false, m.Paginator.PerPage, len(m.items[m.category]), m.cursor,
m.Paginator.Page, m.Paginator.TotalPages)
rankingsAndContent := lipgloss.JoinHorizontal(lipgloss.Top, rankings, content)
sections = append(sections, rankingsAndContent)
if m.showStatusBar {
v := m.statusAndPaginationView()
sections = append(sections, v)
}
return lipgloss.JoinVertical(lipgloss.Left, sections...)
}
func (m Model) titleView() string {
return header.GetHeader(m.categoryToDisplay, m.favorites.HasItems(), m.width) + "\n"
}
func (m Model) statusAndPaginationView() string {
centerContent := ""
rightContent := ""
if m.isOnHelpScreen {
centerContent = lipgloss.NewStyle().Faint(true).Render(
"github.com/bensadeh/circumflex • version " + app.Version)
} else if m.showSpinner {
centerContent = m.spinnerView()
} else {
centerContent = m.statusMessage
}
if m.isOnHelpScreen {
rightContent = "hlp"
} else {