58 lines
1.5 KiB
Go
Raw Normal View History

2021-12-19 19:06:37 +11:00
// Demo code for the bar chart primitive.
package main
import (
"math/rand"
"time"
"github.com/gdamore/tcell/v2"
"github.com/navidys/tgraph"
"github.com/rivo/tview"
)
func main() {
app := tview.NewApplication()
2021-12-19 20:48:23 +11:00
2021-12-19 19:06:37 +11:00
barGraph := tgraph.NewBarChart()
barGraph.SetBorder(true)
barGraph.SetTitle("System Resource Usage")
// display system metric usage
2021-12-20 21:33:00 +11:00
barGraph.AddBar("cpu", 20, tcell.ColorBlue)
barGraph.AddBar("mem", 60, tcell.ColorRed)
barGraph.AddBar("swap", 80, tcell.ColorGreen)
barGraph.AddBar("disk", 100, tcell.ColorOrange)
2021-12-19 19:06:37 +11:00
barGraph.SetMaxValue(100)
2021-12-20 21:33:00 +11:00
flex := tview.NewFlex().SetDirection(tview.FlexColumn)
flex.AddItem(barGraph, 40, 0, false)
2021-12-20 21:49:58 +11:00
//flex.AddItem(tview.NewBox().SetBorder(true), 40, 0, false)
2021-12-20 21:33:00 +11:00
flex.SetRect(0, 0, 100, 15)
2021-12-19 20:48:23 +11:00
2021-12-19 19:06:37 +11:00
update := func() {
rand.Seed(time.Now().UnixNano())
tick := time.NewTicker(1000 * time.Millisecond)
for {
select {
case <-tick.C:
rangeLower := 0
2021-12-20 21:33:00 +11:00
rangeUpper := 100
2021-12-19 19:06:37 +11:00
randomNum := rangeLower + rand.Intn(rangeUpper-rangeLower+1)
barGraph.SetBarValue("cpu", randomNum)
randomNum = rangeLower + rand.Intn(rangeUpper-rangeLower+1)
barGraph.SetBarValue("memory", randomNum)
randomNum = rangeLower + rand.Intn(rangeUpper-rangeLower+1)
barGraph.SetBarValue("swap", randomNum)
randomNum = rangeLower + rand.Intn(rangeUpper-rangeLower+1)
barGraph.SetBarValue("disk", randomNum)
app.Draw()
}
}
}
go update()
2021-12-20 21:33:00 +11:00
if err := app.SetRoot(flex, false).EnableMouse(true).Run(); err != nil {
2021-12-19 19:06:37 +11:00
panic(err)
}
}