Skip to content
This repository has been archived by the owner on May 31, 2024. It is now read-only.

Commit

Permalink
change dot to arabic paging format
Browse files Browse the repository at this point in the history
Signed-off-by: zychen5186 <brianchen5197@gmail.com>

change dot to arabic paging format

Signed-off-by: zychen5186 <brianchen5197@gmail.com>

change var names

Signed-off-by: zychen5186 <brianchen5197@gmail.com>

fix: lint

Signed-off-by: zychen5186 <brianchen5197@gmail.com>
  • Loading branch information
zychen5186 committed Apr 23, 2024
1 parent 2bb1095 commit 44e2676
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 22 deletions.
2 changes: 1 addition & 1 deletion cmd/get/execution.go
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ func getExecutionFunc(ctx context.Context, args []string, cmdCtx cmdCore.Command
logger.Infof(ctx, "Retrieved %v executions", len(executionList.Executions))

if config.GetConfig().Interactive {
bubbletea.BubbleteaPaginator(executionColumns, getCallBack(ctx, cmdCtx))
bubbletea.Paginator(executionColumns, getCallBack(ctx, cmdCtx))
return nil
}

Expand Down
11 changes: 3 additions & 8 deletions pkg/bubbletea/bubbletea_pagination.go
Original file line number Diff line number Diff line change
@@ -1,14 +1,11 @@
package bubbletea

// A simple program demonstrating the paginator component from the Bubbles
// component library.

import (
"fmt"
"log"
"strings"

"github.com/charmbracelet/bubbles/paginator"
"github.com/charmbracelet/lipgloss"
"github.com/golang/protobuf/proto"

tea "github.com/charmbracelet/bubbletea"
Expand All @@ -21,10 +18,7 @@ type pageModel struct {

func newModel(initMsg []proto.Message) pageModel {
p := paginator.New()
p.Type = paginator.Dots
p.PerPage = defaultMsgPerPage
p.ActiveDot = lipgloss.NewStyle().Foreground(lipgloss.AdaptiveColor{Light: "235", Dark: "252"}).Render("•")
p.InactiveDot = lipgloss.NewStyle().Foreground(lipgloss.AdaptiveColor{Light: "250", Dark: "238"}).Render("•")
p.SetTotalPages(len(initMsg))

return pageModel{
Expand Down Expand Up @@ -59,7 +53,8 @@ func (m pageModel) View() string {
return ""
}
b.WriteString(table)
b.WriteString(" " + m.paginator.View())
currentPage := int(firstBatchIndex-1)*pagePerBatch + m.paginator.Page + 1
b.WriteString(fmt.Sprintf(" PAGE - %d\n", currentPage))
b.WriteString("\n\n h/l ←/→ page • q: quit\n")
return b.String()
}
Expand Down
27 changes: 14 additions & 13 deletions pkg/bubbletea/bubbletea_pagination_util.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,16 +20,17 @@ type DataCallback func(filter filters.Filters) []proto.Message
type PrintableProto struct{ proto.Message }

const (
defaultLimit = 100
defaultMsgPerPage = 10
defaultMsgPerBatch = 100
defaultMsgPerPage = 10
pagePerBatch = defaultMsgPerBatch / defaultMsgPerPage
)

var (
firstBatchIndex int32 = 1
lastBatchIndex int32 = 10
batchLen = make(map[int32]int)

// Callback function from the module that called bubbleteapagination, which is used to fetch data
// Callback function used to fetch data from the module that called bubbletea pagination.
callback DataCallback
// The header of the table
listHeader []printer.Column
Expand Down Expand Up @@ -124,25 +125,25 @@ func printTable(m *pageModel, start int, end int) (string, error) {
return buf.String(), nil
}

func getMessageList(batchPage int32) []proto.Message {
func getMessageList(batchIndex int32) []proto.Message {
msg := callback(filters.Filters{
Limit: defaultLimit,
Page: batchPage,
Limit: defaultMsgPerBatch,
Page: batchIndex,
SortBy: "created_at",
Asc: false,
})
batchLen[batchPage] = len(msg)
batchLen[batchIndex] = len(msg)

return msg
}

func BubbleteaPaginator(_listHeader []printer.Column, _callback DataCallback) {
func Paginator(_listHeader []printer.Column, _callback DataCallback) {
listHeader = _listHeader
callback = _callback

msg := []proto.Message{}
for i := firstBatchIndex; i < lastBatchIndex+1; i++ {
msg = append(msg, getMessageList(int32(i))...)
msg = append(msg, getMessageList(i)...)
}

showPagination(msg)
Expand All @@ -153,21 +154,21 @@ func preFetchPage(m *pageModel) {
if len(m.items)/defaultMsgPerPage == m.paginator.Page+1 {
newMessages := getMessageList(lastBatchIndex + 1)
if len(newMessages) != 0 {
lastBatchIndex += 1
lastBatchIndex++
m.items = append(m.items, newMessages...)
m.items = m.items[batchLen[firstBatchIndex]:] // delete the msgs in the "firstBatchIndex" batch
m.paginator.Page -= batchLen[firstBatchIndex] / defaultMsgPerPage
firstBatchIndex += 1
firstBatchIndex++
}
}
// Triggers when user is at the first page
if m.paginator.Page == 0 && firstBatchIndex > 1 {
newMessages := getMessageList(firstBatchIndex - 1)
firstBatchIndex -= 1
firstBatchIndex--
m.items = append(m.items, newMessages...)
m.items = m.items[:len(m.items)-batchLen[lastBatchIndex]] // delete the msgs in the "lastBatchIndex" batch
m.paginator.Page += batchLen[firstBatchIndex] / defaultMsgPerPage
lastBatchIndex -= 1
lastBatchIndex--
}
m.paginator.SetTotalPages(len(m.items))
}

0 comments on commit 44e2676

Please sign in to comment.