Skip to content

Commit

Permalink
feat(selection): page
Browse files Browse the repository at this point in the history
  • Loading branch information
fzdwx committed May 23, 2023
1 parent 8b4cf2e commit ed0facb
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 54 deletions.
9 changes: 9 additions & 0 deletions _examples/single_select/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,19 @@ func main() {
key.WithKeys("enter"),
key.WithHelp("enter", "finish select"),
)
selectKeymap.NextPage = key.NewBinding(
key.WithKeys("q"),
key.WithHelp("q", "next page"),
)
selectKeymap.PrevPage = key.NewBinding(
key.WithKeys("b"),
key.WithHelp("b", "prev page"),
)
selected, err := inf.NewSingleSelect(
options,
singleselect.WithDisableFilter(),
singleselect.WithKeyBinding(selectKeymap),
singleselect.WithPageSize(5),
).Display("Hello world")

if err == nil {
Expand Down
10 changes: 9 additions & 1 deletion components/components.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@ package components
import (
"github.com/charmbracelet/bubbles/key"
"github.com/charmbracelet/bubbles/list"
"github.com/charmbracelet/bubbles/paginator"
"github.com/charmbracelet/bubbles/textinput"
tea "github.com/charmbracelet/bubbletea"
"github.com/charmbracelet/lipgloss"
"github.com/duke-git/lancet/v2/slice"
"github.com/fzdwx/infinite/style"
"github.com/fzdwx/infinite/theme"
Expand Down Expand Up @@ -109,8 +111,13 @@ func NewSelection(choices []string) *Selection {
items := slice.Map[string, SelectionItem](choices, func(idx int, item string) SelectionItem {
return SelectionItem{idx, item}
})

model := paginator.New()
p := paginator.New()
p.Type = paginator.Dots
p.ActiveDot = lipgloss.NewStyle().Foreground(lipgloss.AdaptiveColor{Light: "235", Dark: "252"}).Render("•")
p.InactiveDot = lipgloss.NewStyle().Foreground(lipgloss.AdaptiveColor{Light: "250", Dark: "238"}).Render("•")
c := &Selection{
Paginator: model,
Choices: items,
Selected: make(map[int]bool),
CursorSymbol: SelectionDefaultCursorSymbol,
Expand Down Expand Up @@ -161,6 +168,7 @@ func NewSpinner() *Spinner {
return c
}

// NewSelectionWithList WIP
func NewSelectionWithList[T list.DefaultItem](items []T) *SelectionWithList[T] {
keymap := &SelectionWithListKeyMap{
Choice: key.NewBinding(
Expand Down
72 changes: 19 additions & 53 deletions components/selection.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"fmt"
"github.com/charmbracelet/bubbles/help"
"github.com/charmbracelet/bubbles/key"
"github.com/charmbracelet/bubbles/paginator"
tea "github.com/charmbracelet/bubbletea"
"github.com/duke-git/lancet/v2/mathutil"
"github.com/duke-git/lancet/v2/slice"
Expand Down Expand Up @@ -135,15 +136,12 @@ type Selection struct {
Selected map[int]bool
// Current cursor index in currentChoices
cursor int
// the offset of screen
scrollOffset int
// usually len(currentChoices)
availableChoices int
// currently valid option
currentChoices []SelectionItem
program *tea.Program

Choices []SelectionItem
Paginator paginator.Model
Choices []SelectionItem

Validators []Validator
validatorsErrMsg []string
Expand Down Expand Up @@ -241,17 +239,13 @@ func (s *Selection) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
}

if key.Matches(msg, s.Keymap.NextPage) {
for i := 0; i < s.PageSize; i++ {
s.moveDown()
}
s.Paginator.NextPage()
shouldSkipFiler = true
}
str := msg.String()
_ = str == ""
if key.Matches(msg, s.Keymap.PrevPage) {
for i := 0; i < s.PageSize; i++ {
s.moveUp()
}
s.Paginator.PrevPage()
shouldSkipFiler = true
}

Expand Down Expand Up @@ -327,8 +321,11 @@ func (s *Selection) View() string {
msg.NewLine().Write(s.Header)
}

s.Paginator.PerPage = s.PageSize
s.Paginator.SetTotalPages(len(s.currentChoices))
start, end := s.Paginator.GetSliceBounds(len(s.currentChoices))
// Iterate over our Choices
for i, choice := range s.currentChoices {
for i, choice := range s.currentChoices[start:end] {
val := choice.Val

// Is the CursorSymbol pointing at this choice?
Expand All @@ -348,6 +345,8 @@ func (s *Selection) View() string {
msg.NewLine().Write(s.RowRender(cursorSymbol, hintSymbol, val))
}

msg.NewLine().Write(s.Paginator.View())

if s.ShowHelp {
msg.NewLine().Write(s.Help.View(s.Keymap))
}
Expand Down Expand Up @@ -382,9 +381,7 @@ func (s *Selection) RenderColor() {

// RefreshChoices refresh Choices
func (s *Selection) RefreshChoices() {
var choices []SelectionItem
var filterChoices []SelectionItem
var available, ignored int

// filter choice
if s.shouldFilter() && len(s.FilterInput.Value()) > 0 {
Expand All @@ -394,24 +391,7 @@ func (s *Selection) RefreshChoices() {
filterChoices = s.Choices
}

for _, choice := range filterChoices {
available++

if s.PageSize > 0 && len(choices) >= s.PageSize {
break
}

if (s.PageSize > 0) && (ignored < s.scrollOffset) {
ignored++

continue
}

choices = append(choices, choice)
}

s.currentChoices = choices
s.availableChoices = available
s.currentChoices = filterChoices
}

// viewResult get result
Expand Down Expand Up @@ -519,12 +499,12 @@ func (s *Selection) finish() (tea.Model, tea.Cmd) {

// shouldMoveToTop should move to top?
func (s *Selection) shouldMoveToTop() bool {
return (s.cursor + s.scrollOffset) == (len(s.Choices) - 1)
return s.cursor == (s.PageSize-1) && s.Paginator.OnLastPage()
}

// shouldScrollDown should scroll down?
func (s *Selection) shouldScrollDown() bool {
return s.cursor == len(s.currentChoices)-1 && s.canScrollDown()
return s.cursor == (s.PageSize-1) && s.canScrollDown()
}

// shouldScrollUp should scroll up?
Expand All @@ -535,44 +515,30 @@ func (s *Selection) shouldScrollUp() bool {
// moveToTop move cursor to top
func (s *Selection) moveToTop() {
s.cursor = 0
s.scrollOffset = 0
s.Paginator.Page = 0
s.RefreshChoices()
}

func (s *Selection) scrollUp() {
if s.PageSize <= 0 || s.scrollOffset <= 0 {
return
}

s.cursor = mathutil.Min(len(s.currentChoices)-1, s.cursor+1)
s.scrollOffset--
s.Paginator.PrevPage()
s.RefreshChoices()
}

func (s *Selection) scrollDown() {
if s.PageSize <= 0 || s.scrollOffset+s.PageSize >= s.availableChoices {
if s.PageSize <= 0 {
return
}

s.cursor = mathutil.Max(0, s.cursor-1)
s.scrollOffset++
s.RefreshChoices()
}

func (s *Selection) canScrollDown() bool {
if s.PageSize <= 0 || s.availableChoices <= s.PageSize {
return false
}

if s.scrollOffset+s.PageSize >= len(s.Choices) {
return false
}

return true
return s.Paginator.OnLastPage() == false
}

func (s *Selection) canScrollUp() bool {
return s.scrollOffset > 0
return s.Paginator.Page != 0
}

func (s *Selection) shouldFilter() bool {
Expand Down

0 comments on commit ed0facb

Please sign in to comment.