mirror of
https://github.com/bcicen/ctop.git
synced 2024-08-30 18:23:19 +00:00
add basic cursor
This commit is contained in:
parent
1bbe8463fe
commit
055257528a
42
grid.go
42
grid.go
@ -7,6 +7,7 @@ import (
|
||||
)
|
||||
|
||||
type Grid struct {
|
||||
cursorPos uint
|
||||
containers map[string]*Container
|
||||
}
|
||||
|
||||
@ -14,6 +15,11 @@ func (g *Grid) AddContainer(id string) {
|
||||
g.containers[id] = NewContainer(id)
|
||||
}
|
||||
|
||||
// Return number of containers/rows
|
||||
func (g *Grid) Len() uint {
|
||||
return uint(len(g.containers))
|
||||
}
|
||||
|
||||
// Return sorted list of active container IDs
|
||||
func (g *Grid) CIDs() []string {
|
||||
var ids []string
|
||||
@ -24,6 +30,21 @@ func (g *Grid) CIDs() []string {
|
||||
return ids
|
||||
}
|
||||
|
||||
// Redraw the cursor with the currently selected row
|
||||
func (g *Grid) Cursor() {
|
||||
for n, id := range g.CIDs() {
|
||||
c := g.containers[id]
|
||||
if uint(n) == g.cursorPos {
|
||||
c.widgets.cid.TextFgColor = ui.ColorDefault
|
||||
c.widgets.cid.TextBgColor = ui.ColorWhite
|
||||
} else {
|
||||
c.widgets.cid.TextFgColor = ui.ColorWhite
|
||||
c.widgets.cid.TextBgColor = ui.ColorDefault
|
||||
}
|
||||
}
|
||||
ui.Render(ui.Body)
|
||||
}
|
||||
|
||||
func (g *Grid) Rows() (rows []*ui.Row) {
|
||||
for _, cid := range g.CIDs() {
|
||||
c := g.containers[cid]
|
||||
@ -88,20 +109,25 @@ func Display(g *Grid) {
|
||||
|
||||
// calculate layout
|
||||
ui.Body.Align()
|
||||
|
||||
g.Cursor()
|
||||
ui.Render(ui.Body)
|
||||
|
||||
ui.Handle("/sys/kbd/<up>", func(ui.Event) {
|
||||
if g.cursorPos > 0 {
|
||||
g.cursorPos -= 1
|
||||
g.Cursor()
|
||||
}
|
||||
})
|
||||
ui.Handle("/sys/kbd/<down>", func(ui.Event) {
|
||||
if g.cursorPos < (g.Len() - 1) {
|
||||
g.cursorPos += 1
|
||||
g.Cursor()
|
||||
}
|
||||
})
|
||||
ui.Handle("/sys/kbd/q", func(ui.Event) {
|
||||
ui.StopLoop()
|
||||
})
|
||||
ui.Handle("/timer/1s", func(e ui.Event) {
|
||||
// t := e.Data.(ui.EvtTimer)
|
||||
// i := t.Count
|
||||
// if i > 103 {
|
||||
// ui.StopLoop()
|
||||
// return
|
||||
// }
|
||||
|
||||
ui.Render(ui.Body)
|
||||
})
|
||||
|
||||
|
2
main.go
2
main.go
@ -42,7 +42,7 @@ func main() {
|
||||
containers = os.Args[1:]
|
||||
}
|
||||
|
||||
g := &Grid{make(map[string]*Container)}
|
||||
g := &Grid{0, make(map[string]*Container)}
|
||||
for _, c := range containers {
|
||||
g.AddContainer(c)
|
||||
}
|
||||
|
@ -9,6 +9,7 @@ import (
|
||||
|
||||
type Widgets struct {
|
||||
cid *ui.Par
|
||||
names *ui.Par
|
||||
cpu *ui.Gauge
|
||||
net *ui.Gauge
|
||||
memory *ui.Gauge
|
||||
|
Loading…
x
Reference in New Issue
Block a user