Skip to content

Commit

Permalink
add y pos scrolling to expanded view
Browse files Browse the repository at this point in the history
  • Loading branch information
bcicen committed Mar 12, 2017
1 parent 8327406 commit 12fa716
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 5 deletions.
38 changes: 33 additions & 5 deletions cwidgets/expanded/main.go
Expand Up @@ -18,6 +18,7 @@ type Expanded struct {
Cpu *Cpu
Mem *Mem
IO *IO
X, Y int
Width int
}

Expand All @@ -35,27 +36,54 @@ func NewExpanded(id string) *Expanded {
}
}

func (e *Expanded) SetWidth(w int) {
e.Width = w
func (e *Expanded) Up() {
if e.Y < 0 {
e.Y++
e.Align()
ui.Render(e)
}
}

func (e *Expanded) SetMeta(k, v string) {
e.Info.Set(k, v)
func (e *Expanded) Down() {
if e.Y > (ui.TermHeight() - e.GetHeight()) {
e.Y--
e.Align()
ui.Render(e)
}
}

func (e *Expanded) SetWidth(w int) { e.Width = w }
func (e *Expanded) SetMeta(k, v string) { e.Info.Set(k, v) }

func (e *Expanded) SetMetrics(m metrics.Metrics) {
e.Cpu.Update(m.CPUUtil)
e.Net.Update(m.NetRx, m.NetTx)
e.Mem.Update(int(m.MemUsage), int(m.MemLimit))
e.IO.Update(m.IOBytesRead, m.IOBytesWrite)
}

// Return total column height
func (e *Expanded) GetHeight() (h int) {
h += e.Info.Height
h += e.Net.Height
h += e.Cpu.Height
h += e.Mem.Height
h += e.IO.Height
return h
}

func (e *Expanded) Align() {
y := 0
// reset offset if needed
if e.GetHeight() <= ui.TermHeight() {
e.Y = 0
}

y := e.Y
for _, i := range e.all() {
i.SetY(y)
y += i.GetHeight()
}

if e.Width > colWidth[0] {
colWidth[1] = e.Width - (colWidth[0] + 1)
}
Expand Down
2 changes: 2 additions & 0 deletions grid.go
Expand Up @@ -44,6 +44,8 @@ func ExpandView(c *Container) {

ex.Align()
ui.Render(ex)
ui.Handle("/sys/kbd/<up>", func(ui.Event) { ex.Up() })
ui.Handle("/sys/kbd/<down>", func(ui.Event) { ex.Down() })
ui.Handle("/timer/1s", func(ui.Event) {
ui.Render(ex)
})
Expand Down

0 comments on commit 12fa716

Please sign in to comment.