44package list
55
66import (
7+ "cmp"
78 "fmt"
89 "io"
910 "sort"
@@ -22,6 +23,13 @@ import (
2223 "github.com/charmbracelet/bubbles/v2/textinput"
2324)
2425
26+ func clamp [T cmp.Ordered ](v , low , high T ) T {
27+ if low > high {
28+ low , high = high , low
29+ }
30+ return min (high , max (low , v ))
31+ }
32+
2533// Item is an item that appears in the list.
2634type Item interface {
2735 // FilterValue is the value we use when filtering against this item when
@@ -277,17 +285,15 @@ func (m *Model) SetFilterText(filter string) {
277285 fmm , _ := msg .(FilterMatchesMsg )
278286 m .filteredItems = filteredItems (fmm )
279287 m .filterState = FilterApplied
280- m .Paginator .Page = 0
281- m .cursor = 0
288+ m .GoToStart ()
282289 m .FilterInput .CursorEnd ()
283290 m .updatePagination ()
284291 m .updateKeybindings ()
285292}
286293
287294// SetFilterState allows setting the filtering state manually.
288295func (m * Model ) SetFilterState (state FilterState ) {
289- m .Paginator .Page = 0
290- m .cursor = 0
296+ m .GoToStart ()
291297 m .filterState = state
292298 m .FilterInput .CursorEnd ()
293299 m .FilterInput .Focus ()
@@ -511,14 +517,12 @@ func (m *Model) CursorUp() {
511517 m .cursor --
512518
513519 // If we're at the start, stop
514- if m .cursor < 0 && m .Paginator .Page == 0 {
520+ if m .cursor < 0 && m .Paginator .OnFirstPage () {
515521 // if infinite scrolling is enabled, go to the last item
516522 if m .InfiniteScrolling {
517- m .Paginator .Page = m .Paginator .TotalPages - 1
518- m .cursor = m .Paginator .ItemsOnPage (len (m .VisibleItems ())) - 1
523+ m .GoToEnd ()
519524 return
520525 }
521-
522526 m .cursor = 0
523527 return
524528 }
@@ -530,18 +534,18 @@ func (m *Model) CursorUp() {
530534
531535 // Go to the previous page
532536 m .Paginator .PrevPage ()
533- m .cursor = m .Paginator . ItemsOnPage ( len ( m . VisibleItems ())) - 1
537+ m .cursor = m .maxCursorIndex ()
534538}
535539
536540// CursorDown moves the cursor down. This can also advance the state to the
537541// next page.
538542func (m * Model ) CursorDown () {
539- itemsOnPage := m .Paginator . ItemsOnPage ( len ( m . VisibleItems ()) )
543+ maxCursorIndex := m .maxCursorIndex ( )
540544
541545 m .cursor ++
542546
543- // If we 're at the end, stop
544- if m .cursor < itemsOnPage {
547+ // We 're still within bounds of the current page, so no need to do anything.
548+ if m .cursor <= maxCursorIndex {
545549 return
546550 }
547551
@@ -552,31 +556,40 @@ func (m *Model) CursorDown() {
552556 return
553557 }
554558
555- // During filtering the cursor position can exceed the number of
556- // itemsOnPage. It's more intuitive to start the cursor at the
557- // topmost position when moving it down in this scenario.
558- if m .cursor > itemsOnPage {
559- m .cursor = 0
560- return
561- }
559+ m .cursor = max (0 , maxCursorIndex )
562560
563- m .cursor = itemsOnPage - 1
564-
565- // if infinite scrolling is enabled, go to the first item
561+ // if infinite scrolling is enabled, go to the first item.
566562 if m .InfiniteScrolling {
567- m .Paginator .Page = 0
568- m .cursor = 0
563+ m .GoToStart ()
569564 }
570565}
571566
567+ // GoToStart moves to the first page, and first item on the first page.
568+ func (m * Model ) GoToStart () {
569+ m .Paginator .Page = 0
570+ m .cursor = 0
571+ }
572+
573+ // GoToEnd moves to the last page, and last item on the last page.
574+ func (m * Model ) GoToEnd () {
575+ m .Paginator .Page = max (0 , m .Paginator .TotalPages - 1 )
576+ m .cursor = m .maxCursorIndex ()
577+ }
578+
572579// PrevPage moves to the previous page, if available.
573580func (m * Model ) PrevPage () {
574581 m .Paginator .PrevPage ()
582+ m .cursor = clamp (m .cursor , 0 , m .maxCursorIndex ())
575583}
576584
577585// NextPage moves to the next page, if available.
578586func (m * Model ) NextPage () {
579587 m .Paginator .NextPage ()
588+ m .cursor = clamp (m .cursor , 0 , m .maxCursorIndex ())
589+ }
590+
591+ func (m * Model ) maxCursorIndex () int {
592+ return max (0 , m .Paginator .ItemsOnPage (len (m .VisibleItems ()))- 1 )
580593}
581594
582595// FilterState returns the current filter state.
@@ -668,22 +681,18 @@ func (m *Model) NewStatusMessage(s string) tea.Cmd {
668681 }
669682}
670683
671- // SetSize sets the width and height of this component.
672- func (m * Model ) SetSize (width , height int ) {
673- m .setSize (width , height )
674- }
675-
676684// SetWidth sets the width of this component.
677685func (m * Model ) SetWidth (v int ) {
678- m .setSize (v , m .height )
686+ m .SetSize (v , m .height )
679687}
680688
681689// SetHeight sets the height of this component.
682690func (m * Model ) SetHeight (v int ) {
683- m .setSize (m .width , v )
691+ m .SetSize (m .width , v )
684692}
685693
686- func (m * Model ) setSize (width , height int ) {
694+ // SetSize sets the width and height of this component.
695+ func (m * Model ) SetSize (width , height int ) {
687696 promptWidth := lipgloss .Width (m .Styles .Title .Render (m .FilterInput .Prompt ))
688697
689698 m .width = width
@@ -843,7 +852,6 @@ func (m Model) Update(msg tea.Msg) (Model, tea.Cmd) {
843852// Updates for when a user is browsing the list.
844853func (m * Model ) handleBrowsing (msg tea.Msg ) tea.Cmd {
845854 var cmds []tea.Cmd
846- numItems := len (m .VisibleItems ())
847855
848856 switch msg := msg .(type ) {
849857 case tea.KeyPressMsg :
@@ -869,21 +877,18 @@ func (m *Model) handleBrowsing(msg tea.Msg) tea.Cmd {
869877 m .Paginator .NextPage ()
870878
871879 case key .Matches (msg , m .KeyMap .GoToStart ):
872- m .Paginator .Page = 0
873- m .cursor = 0
880+ m .GoToStart ()
874881
875882 case key .Matches (msg , m .KeyMap .GoToEnd ):
876- m .Paginator .Page = m .Paginator .TotalPages - 1
877- m .cursor = m .Paginator .ItemsOnPage (numItems ) - 1
883+ m .GoToEnd ()
878884
879885 case key .Matches (msg , m .KeyMap .Filter ):
880886 m .hideStatusMessage ()
881887 if m .FilterInput .Value () == "" {
882888 // Populate filter with all items only if the filter is empty.
883889 m .filteredItems = m .itemsAsFilterItems ()
884890 }
885- m .Paginator .Page = 0
886- m .cursor = 0
891+ m .GoToStart ()
887892 m .filterState = Filtering
888893 m .FilterInput .CursorEnd ()
889894 m .FilterInput .Focus ()
@@ -901,11 +906,7 @@ func (m *Model) handleBrowsing(msg tea.Msg) tea.Cmd {
901906 cmd := m .delegate .Update (msg , m )
902907 cmds = append (cmds , cmd )
903908
904- // Keep the index in bounds when paginating
905- itemsOnPage := m .Paginator .ItemsOnPage (len (m .VisibleItems ()))
906- if m .cursor > itemsOnPage - 1 {
907- m .cursor = max (0 , itemsOnPage - 1 )
908- }
909+ m .cursor = clamp (m .cursor , 0 , m .maxCursorIndex ())
909910
910911 return tea .Batch (cmds ... )
911912}
0 commit comments