mirror of
https://github.com/bcicen/ctop.git
synced 2024-08-30 18:23:19 +00:00
add pagination support for compact view
This commit is contained in:
parent
a6ee6edb1d
commit
2b80832a36
83
cursor.go
83
cursor.go
@ -7,7 +7,6 @@ import (
|
|||||||
type GridCursor struct {
|
type GridCursor struct {
|
||||||
selectedID string // id of currently selected container
|
selectedID string // id of currently selected container
|
||||||
filtered Containers
|
filtered Containers
|
||||||
containers Containers
|
|
||||||
cSource ContainerSource
|
cSource ContainerSource
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -18,83 +17,109 @@ func NewGridCursor() *GridCursor {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (gc *GridCursor) Len() int { return len(gc.filtered) }
|
func (gc *GridCursor) Len() int { return len(gc.filtered) }
|
||||||
func (gc *GridCursor) Selected() *Container { return gc.containers[gc.Idx()] }
|
func (gc *GridCursor) Selected() *Container { return gc.filtered[gc.Idx()] }
|
||||||
|
|
||||||
// Refresh containers from source
|
// Refresh containers from source
|
||||||
func (gc *GridCursor) RefreshContainers() (lenChanged bool) {
|
func (gc *GridCursor) RefreshContainers() (lenChanged bool) {
|
||||||
oldLen := gc.Len()
|
oldLen := gc.Len()
|
||||||
gc.setContainers(gc.cSource.All())
|
|
||||||
|
// Containers filtered by display bool
|
||||||
|
gc.filtered = Containers{}
|
||||||
|
var cursorVisible bool
|
||||||
|
for _, c := range gc.cSource.All() {
|
||||||
|
if c.display {
|
||||||
|
if c.Id == gc.selectedID {
|
||||||
|
cursorVisible = true
|
||||||
|
}
|
||||||
|
gc.filtered = append(gc.filtered, c)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
if oldLen != gc.Len() {
|
if oldLen != gc.Len() {
|
||||||
lenChanged = true
|
lenChanged = true
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if !cursorVisible {
|
||||||
|
gc.Reset()
|
||||||
|
}
|
||||||
if gc.selectedID == "" {
|
if gc.selectedID == "" {
|
||||||
gc.Reset()
|
gc.Reset()
|
||||||
}
|
}
|
||||||
return lenChanged
|
return lenChanged
|
||||||
}
|
}
|
||||||
|
|
||||||
func (gc *GridCursor) setContainers(c Containers) {
|
|
||||||
gc.containers = c
|
|
||||||
// Containers filtered by display bool
|
|
||||||
gc.filtered = Containers{}
|
|
||||||
for _, c := range gc.containers {
|
|
||||||
if c.display {
|
|
||||||
gc.filtered = append(gc.filtered, c)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Set an initial cursor position, if possible
|
// Set an initial cursor position, if possible
|
||||||
func (gc *GridCursor) Reset() {
|
func (gc *GridCursor) Reset() {
|
||||||
for _, c := range gc.containers {
|
for _, c := range gc.cSource.All() {
|
||||||
c.Widgets.Name.UnHighlight()
|
c.Widgets.Name.UnHighlight()
|
||||||
}
|
}
|
||||||
if gc.Len() > 0 {
|
if gc.Len() > 0 {
|
||||||
gc.selectedID = gc.containers[0].Id
|
gc.selectedID = gc.filtered[0].Id
|
||||||
gc.containers[0].Widgets.Name.Highlight()
|
gc.filtered[0].Widgets.Name.Highlight()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Return current cursor index
|
// Return current cursor index
|
||||||
func (gc *GridCursor) Idx() int {
|
func (gc *GridCursor) Idx() int {
|
||||||
for n, c := range gc.containers {
|
for n, c := range gc.filtered {
|
||||||
if c.Id == gc.selectedID {
|
if c.Id == gc.selectedID {
|
||||||
return n
|
return n
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
gc.Reset()
|
||||||
return 0
|
return 0
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (gc *GridCursor) ScrollPage() {
|
||||||
|
// skip scroll if no need to page
|
||||||
|
if gc.Len() < cGrid.MaxRows() {
|
||||||
|
cGrid.Offset = 0
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
idx := gc.Idx()
|
||||||
|
|
||||||
|
// page down
|
||||||
|
if idx >= cGrid.Offset+cGrid.MaxRows() {
|
||||||
|
cGrid.Offset++
|
||||||
|
cGrid.Align()
|
||||||
|
}
|
||||||
|
// page up
|
||||||
|
if idx < cGrid.Offset {
|
||||||
|
cGrid.Offset--
|
||||||
|
cGrid.Align()
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
func (gc *GridCursor) Up() {
|
func (gc *GridCursor) Up() {
|
||||||
idx := gc.Idx()
|
idx := gc.Idx()
|
||||||
// decrement if possible
|
if idx <= 0 { // already at top
|
||||||
if idx <= 0 {
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
active := gc.containers[idx]
|
active := gc.filtered[idx]
|
||||||
next := gc.containers[idx-1]
|
next := gc.filtered[idx-1]
|
||||||
|
|
||||||
active.Widgets.Name.UnHighlight()
|
active.Widgets.Name.UnHighlight()
|
||||||
gc.selectedID = next.Id
|
gc.selectedID = next.Id
|
||||||
next.Widgets.Name.Highlight()
|
next.Widgets.Name.Highlight()
|
||||||
|
|
||||||
|
gc.ScrollPage()
|
||||||
ui.Render(cGrid)
|
ui.Render(cGrid)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (gc *GridCursor) Down() {
|
func (gc *GridCursor) Down() {
|
||||||
idx := gc.Idx()
|
idx := gc.Idx()
|
||||||
// increment if possible
|
if idx >= gc.Len()-1 { // already at bottom
|
||||||
if idx >= (gc.Len() - 1) {
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
//if idx >= maxRows()-1 {
|
active := gc.filtered[idx]
|
||||||
//return
|
next := gc.filtered[idx+1]
|
||||||
//}
|
|
||||||
active := gc.containers[idx]
|
|
||||||
next := gc.containers[idx+1]
|
|
||||||
|
|
||||||
active.Widgets.Name.UnHighlight()
|
active.Widgets.Name.UnHighlight()
|
||||||
gc.selectedID = next.Id
|
gc.selectedID = next.Id
|
||||||
next.Widgets.Name.Highlight()
|
next.Widgets.Name.Highlight()
|
||||||
|
|
||||||
|
gc.ScrollPage()
|
||||||
ui.Render(cGrid)
|
ui.Render(cGrid)
|
||||||
}
|
}
|
||||||
|
@ -12,7 +12,7 @@ type CompactGrid struct {
|
|||||||
X, Y int
|
X, Y int
|
||||||
Width int
|
Width int
|
||||||
Height int
|
Height int
|
||||||
cursorID string
|
Offset int // starting row offset
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewCompactGrid() *CompactGrid {
|
func NewCompactGrid() *CompactGrid {
|
||||||
@ -20,29 +20,31 @@ func NewCompactGrid() *CompactGrid {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (cg *CompactGrid) Align() {
|
func (cg *CompactGrid) Align() {
|
||||||
// update row y pos recursively
|
// update row ypos, width recursively
|
||||||
y := cg.Y
|
y := cg.Y
|
||||||
for _, r := range cg.Rows {
|
for _, r := range cg.pageRows() {
|
||||||
r.SetY(y)
|
r.SetY(y)
|
||||||
y += r.GetHeight()
|
y += r.GetHeight()
|
||||||
}
|
|
||||||
|
|
||||||
// update row width recursively
|
|
||||||
for _, r := range cg.Rows {
|
|
||||||
r.SetWidth(cg.Width)
|
r.SetWidth(cg.Width)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (cg *CompactGrid) Clear() { cg.Rows = []ui.GridBufferer{header} }
|
func (cg *CompactGrid) Clear() { cg.Rows = []ui.GridBufferer{} }
|
||||||
func (cg *CompactGrid) GetHeight() int { return len(cg.Rows) }
|
func (cg *CompactGrid) GetHeight() int { return len(cg.Rows) }
|
||||||
func (cg *CompactGrid) SetX(x int) { cg.X = x }
|
func (cg *CompactGrid) SetX(x int) { cg.X = x }
|
||||||
func (cg *CompactGrid) SetY(y int) { cg.Y = y }
|
func (cg *CompactGrid) SetY(y int) { cg.Y = y }
|
||||||
func (cg *CompactGrid) SetWidth(w int) { cg.Width = w }
|
func (cg *CompactGrid) SetWidth(w int) { cg.Width = w }
|
||||||
func (cg *CompactGrid) MaxRows() int { return ui.TermHeight() - header.Height - cg.Y }
|
func (cg *CompactGrid) MaxRows() int { return ui.TermHeight() - header.Height - cg.Y }
|
||||||
|
|
||||||
|
func (cg *CompactGrid) pageRows() (rows []ui.GridBufferer) {
|
||||||
|
rows = append(rows, header)
|
||||||
|
rows = append(rows, cg.Rows[cg.Offset:]...)
|
||||||
|
return rows
|
||||||
|
}
|
||||||
|
|
||||||
func (cg *CompactGrid) Buffer() ui.Buffer {
|
func (cg *CompactGrid) Buffer() ui.Buffer {
|
||||||
buf := ui.NewBuffer()
|
buf := ui.NewBuffer()
|
||||||
for _, r := range cg.Rows {
|
for _, r := range cg.pageRows() {
|
||||||
buf.Merge(r.Buffer())
|
buf.Merge(r.Buffer())
|
||||||
}
|
}
|
||||||
return buf
|
return buf
|
||||||
|
15
grid.go
15
grid.go
@ -19,20 +19,8 @@ func RedrawRows(clr bool) {
|
|||||||
}
|
}
|
||||||
cGrid.SetY(y)
|
cGrid.SetY(y)
|
||||||
|
|
||||||
var cursorVisible bool
|
for _, c := range cursor.filtered {
|
||||||
max := cGrid.MaxRows()
|
|
||||||
for n, c := range cursor.filtered {
|
|
||||||
if n >= max {
|
|
||||||
break
|
|
||||||
}
|
|
||||||
cGrid.AddRows(c.Widgets)
|
cGrid.AddRows(c.Widgets)
|
||||||
if c.Id == cursor.selectedID {
|
|
||||||
cursorVisible = true
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if !cursorVisible {
|
|
||||||
cursor.Reset()
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if clr {
|
if clr {
|
||||||
@ -136,6 +124,7 @@ func Display() bool {
|
|||||||
|
|
||||||
ui.Handle("/sys/wnd/resize", func(e ui.Event) {
|
ui.Handle("/sys/wnd/resize", func(e ui.Event) {
|
||||||
header.Align()
|
header.Align()
|
||||||
|
cursor.ScrollPage()
|
||||||
cGrid.SetWidth(ui.TermWidth())
|
cGrid.SetWidth(ui.TermWidth())
|
||||||
log.Infof("resize: width=%v max-rows=%v", cGrid.Width, cGrid.MaxRows())
|
log.Infof("resize: width=%v max-rows=%v", cGrid.Width, cGrid.MaxRows())
|
||||||
RedrawRows(true)
|
RedrawRows(true)
|
||||||
|
Loading…
Reference in New Issue
Block a user