Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Termdash paging #310

Merged
merged 1 commit into from
Nov 9, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion cmd/termdash/config/switch.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ var switches = []*Switch{
{
Key: "allTasks",
Val: false,
Label: "Show All Tasks",
Label: "Clear All Filters",
},
{
Key: "enableHeader",
Expand Down
16 changes: 12 additions & 4 deletions cmd/termdash/cursor.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,20 @@ type GridCursor struct {

func NewGridCursor(tesHTTPServerAddress string) *GridCursor {
return &GridCursor{
tSource: NewTaskSource(tesHTTPServerAddress),
tSource: NewTaskSource(tesHTTPServerAddress, 100),
}
}

func (gc *GridCursor) Len() int { return len(gc.filtered) }

func (gc *GridCursor) NextPageExists() bool {
return gc.tSource.GetNextPage() != ""
}

func (gc *GridCursor) PreviousPageExists() bool {
return gc.tSource.GetPreviousPage() != ""
}

func (gc *GridCursor) Selected() *TaskWidget {
idx := gc.Idx()
if idx < gc.Len() {
Expand All @@ -36,13 +44,13 @@ func (gc *GridCursor) RefreshTask(id string) *TaskWidget {
}

// Refresh task list
func (gc *GridCursor) RefreshTaskList() (lenChanged bool) {
func (gc *GridCursor) RefreshTaskList(previous, next bool) (lenChanged bool) {
oldLen := gc.Len()

// Tasks filtered by display bool
gc.filtered = TaskWidgets{}
var cursorVisible bool
for _, t := range gc.tSource.All() {
for _, t := range gc.tSource.List(previous, next) {
if t.display {
if t.Task.Id == gc.selectedID {
t.Widgets.ID.Highlight()
Expand All @@ -69,7 +77,7 @@ func (gc *GridCursor) RefreshTaskList() (lenChanged bool) {

// Set an initial cursor position, if possible
func (gc *GridCursor) Reset() {
for _, t := range gc.tSource.All() {
for _, t := range gc.tSource.List(false, false) {
t.Widgets.ID.UnHighlight()
}
if gc.Len() > 0 {
Expand Down
22 changes: 16 additions & 6 deletions cmd/termdash/grid.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ func RedrawRows(clr bool) {
// build layout
y := 1
if config.GetSwitchVal("enableHeader") {
header.SetCount(cursor.Len())
header.SetPrevious(cursor.PreviousPageExists())
header.SetNext(cursor.NextPageExists())
header.SetFilter(config.GetVal("filterStr"))
y += header.Height()
}
Expand Down Expand Up @@ -65,10 +66,20 @@ func ExpandView(t *TaskWidget) {
}

func RefreshDisplay() {
needsClear := cursor.RefreshTaskList()
needsClear := cursor.RefreshTaskList(false, false)
RedrawRows(needsClear)
}

func NextPage() {
cursor.RefreshTaskList(false, true)
RedrawRows(true)
}

func PreviousPage() {
cursor.RefreshTaskList(true, false)
RedrawRows(true)
}

func Display() bool {
var menu func()
var expand bool
Expand All @@ -77,14 +88,13 @@ func Display() bool {

// initial draw
header.Align()
cursor.RefreshTaskList()
cursor.RefreshTaskList(false, false)
RedrawRows(true)

HandleKeys("up", cursor.Up)
HandleKeys("down", cursor.Down)

HandleKeys("pgup", cursor.PgUp)
HandleKeys("pgdown", cursor.PgDown)
HandleKeys("left", PreviousPage)
HandleKeys("right", NextPage)

HandleKeys("exit", ui.StopLoop)
HandleKeys("help", func() {
Expand Down
12 changes: 6 additions & 6 deletions cmd/termdash/keys.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,13 @@ var keyMap = map[string][]string{
"/sys/kbd/<down>",
"/sys/kbd/j",
},
"pgup": {
"/sys/kbd/<previous>",
"/sys/kbd/C-<up>",
"left": {
"/sys/kbd/<left>",
"sys/kbd/h",
},
"pgdown": {
"/sys/kbd/<next>",
"/sys/kbd/C-<down>",
"right": {
"/sys/kbd/<right>",
"sys/kbd/l",
},
"exit": {
"/sys/kbd/q",
Expand Down
2 changes: 1 addition & 1 deletion cmd/termdash/menus.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (
)

var helpDialog = []menu.Item{
{Label: "[a] - toggle display of all tasks", Val: ""},
{Label: "[a] - toggle active filter", Val: ""},
{Label: "[f] - filter displayed tasks", Val: ""},
{Label: "[h] - open this help dialog", Val: ""},
{Label: "[H] - toggle dashboard header", Val: ""},
Expand Down
109 changes: 68 additions & 41 deletions cmd/termdash/tasksource.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,78 +10,92 @@ import (
)

type TesTaskSource interface {
All() TaskWidgets
List(bool, bool) TaskWidgets
Get(string) *TaskWidget
GetNextPage() string
GetPreviousPage() string
}

type TaskSource struct {
client *client.Client
tasks TaskWidgets
lock sync.RWMutex
client *client.Client
pageSize uint32
pPage []string
cPage string
nPage string
tasks TaskWidgets
lock sync.RWMutex
}

func NewTaskSource(tesHTTPServerAddress string) *TaskSource {
func NewTaskSource(tesHTTPServerAddress string, pageSize uint32) *TaskSource {
// init funnel http client
cli := client.NewClient(tesHTTPServerAddress)
cm := &TaskSource{
client: cli,
lock: sync.RWMutex{},
ts := &TaskSource{
client: cli,
pageSize: pageSize,
lock: sync.RWMutex{},
}
cm.tasks = cm.listTasks()
return cm
ts.tasks = ts.listTasks(false, false)
return ts
}

func (cm *TaskSource) listTasks() TaskWidgets {
func (ts *TaskSource) listTasks(previous, next bool) TaskWidgets {
var tasks TaskWidgets
var page string

defer func() {
if r := recover(); r != nil {
if header != nil {
header.SetError(fmt.Sprintf("%v", r))
if next && !previous {
if ts.nPage != "" {
if ts.cPage != "" {
ts.pPage = append(ts.pPage, ts.cPage)
}
ts.cPage = ts.nPage
}
}()

for {
resp, err := cm.client.ListTasks(context.Background(), &tes.ListTasksRequest{
View: tes.TaskView_BASIC,
PageToken: page,
})
if err != nil {
panic(err)
} else if previous && !next {
if len(ts.pPage) > 0 {
ts.cPage = ts.pPage[len(ts.pPage)-1]
ts.pPage = ts.pPage[:len(ts.pPage)-1]
} else {
header.SetError("")
}
for _, t := range resp.Tasks {
tasks = append(tasks, NewTaskWidget(t))
}
page = resp.NextPageToken
if page == "" {
break
ts.cPage = ""
}
}

resp, err := ts.client.ListTasks(context.Background(), &tes.ListTasksRequest{
View: tes.TaskView_BASIC,
PageSize: ts.pageSize,
PageToken: ts.cPage,
})
if err != nil {
header.SetError(fmt.Sprintf("%v", err))
return nil
} else {
// header.SetError(fmt.Sprintf("Previous: %s; Next: %s; Current: %s", ts.pPage, ts.nPage, ts.cPage))
header.SetError("")
}
ts.nPage = resp.NextPageToken

for _, t := range resp.Tasks {
tasks = append(tasks, NewTaskWidget(t))
}

return tasks
}

// Return array of all tasks, sorted by field
func (cm *TaskSource) All() TaskWidgets {
cm.tasks = cm.listTasks()
// Return array of tasks, sorted by field
func (ts *TaskSource) List(previous, next bool) TaskWidgets {
ts.tasks = ts.listTasks(previous, next)

cm.lock.Lock()
ts.lock.Lock()
var tasks TaskWidgets
for _, t := range cm.tasks {
for _, t := range ts.tasks {
tasks = append(tasks, t)
}
cm.lock.Unlock()
ts.lock.Unlock()

sort.Sort(tasks)
tasks.Filter()
return tasks
}

// Get a single task, by ID
func (cm *TaskSource) Get(id string) *TaskWidget {
func (ts *TaskSource) Get(id string) *TaskWidget {
defer func() {
if r := recover(); r != nil {
if header != nil {
Expand All @@ -90,7 +104,7 @@ func (cm *TaskSource) Get(id string) *TaskWidget {
}
}()

task, err := cm.client.GetTask(context.Background(), &tes.GetTaskRequest{
task, err := ts.client.GetTask(context.Background(), &tes.GetTaskRequest{
Id: id,
View: tes.TaskView_FULL,
})
Expand All @@ -99,3 +113,16 @@ func (cm *TaskSource) Get(id string) *TaskWidget {
}
return NewTaskWidget(task)
}

func (ts *TaskSource) GetNextPage() string {
return ts.nPage
}

func (ts *TaskSource) GetPreviousPage() string {
if len(ts.pPage) > 0 {
return ts.pPage[len(ts.pPage)-1]
} else if ts.cPage != "" {
return ts.cPage
}
return ""
}
2 changes: 1 addition & 1 deletion cmd/termdash/termdash.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,9 @@ func termdash(tesHTTPServerAddress string) {
defer Shutdown()

// init grid, cursor, header
header = widgets.NewTermDashHeader()
cursor = NewGridCursor(tesHTTPServerAddress)
cGrid = compact.NewGrid()
header = widgets.NewTermDashHeader()

for {
exit := Display()
Expand Down
Loading