Added rate; closes #5

This commit is contained in:
Caleb Bassi 2018-03-09 00:27:46 -08:00
parent 479491298e
commit 02219b6889
3 changed files with 16 additions and 9 deletions

View File

@ -4,6 +4,7 @@ import (
"fmt" "fmt"
"os" "os"
"os/signal" "os/signal"
"strconv"
"syscall" "syscall"
"time" "time"
@ -28,7 +29,8 @@ var (
colorscheme = colorschemes.Default colorscheme = colorschemes.Default
minimal = false minimal = false
interval = time.Second
cpu *w.CPU cpu *w.CPU
mem *w.Mem mem *w.Mem
@ -48,6 +50,7 @@ Options:
-c, --color <name> Set a colorscheme. -c, --color <name> Set a colorscheme.
-h, --help Show this screen. -h, --help Show this screen.
-m, --minimal Only show CPU, Mem and Process widgets. -m, --minimal Only show CPU, Mem and Process widgets.
-r, --rate=RATE Number of times per second to update CPU and Mem widgets [default: 1].
-v, --version Show version. -v, --version Show version.
Colorschemes: Colorschemes:
@ -63,6 +66,10 @@ Colorschemes:
} }
minimal, _ = args["--minimal"].(bool) minimal, _ = args["--minimal"].(bool)
rateStr, _ := args["--rate"].(string)
rate, _ := strconv.Atoi(rateStr)
interval = time.Second / time.Duration(rate)
} }
func handleColorscheme(cs string) { func handleColorscheme(cs string) {
@ -156,8 +163,8 @@ func main() {
// need to do this before initializing widgets so that they can inherit the colors // need to do this before initializing widgets so that they can inherit the colors
termuiColors() termuiColors()
cpu = w.NewCPU() cpu = w.NewCPU(interval)
mem = w.NewMem() mem = w.NewMem(interval)
proc = w.NewProc(procLoaded, keyPressed) proc = w.NewProc(procLoaded, keyPressed)
if !minimal { if !minimal {
net = w.NewNet() net = w.NewNet()
@ -194,7 +201,7 @@ func main() {
// all rendering done here // all rendering done here
go func() { go func() {
ui.Render(ui.Body) ui.Render(ui.Body)
drawTick := time.NewTicker(time.Second) drawTick := time.NewTicker(interval)
for { for {
select { select {
case <-helpToggled: case <-helpToggled:

View File

@ -14,12 +14,12 @@ type CPU struct {
interval time.Duration interval time.Duration
} }
func NewCPU() *CPU { func NewCPU(interval time.Duration) *CPU {
count, _ := psCPU.Counts(false) count, _ := psCPU.Counts(false)
c := &CPU{ c := &CPU{
LineGraph: ui.NewLineGraph(), LineGraph: ui.NewLineGraph(),
count: count, count: count,
interval: time.Second, interval: interval,
} }
c.Label = "CPU Usage" c.Label = "CPU Usage"
for i := 0; i < c.count; i++ { for i := 0; i < c.count; i++ {
@ -41,7 +41,7 @@ func NewCPU() *CPU {
func (c *CPU) update() { func (c *CPU) update() {
// psutil calculates the CPU usage over a 1 second interval, therefore it blocks for 1 second // psutil calculates the CPU usage over a 1 second interval, therefore it blocks for 1 second
// `true` makes it so psutil doesn't group CPU usage percentages // `true` makes it so psutil doesn't group CPU usage percentages
percent, _ := psCPU.Percent(time.Second, true) percent, _ := psCPU.Percent(c.interval, true)
for i := 0; i < c.count; i++ { for i := 0; i < c.count; i++ {
key := "CPU" + strconv.Itoa(i+1) key := "CPU" + strconv.Itoa(i+1)
c.Data[key] = append(c.Data[key], percent[i]) c.Data[key] = append(c.Data[key], percent[i])

View File

@ -12,10 +12,10 @@ type Mem struct {
interval time.Duration interval time.Duration
} }
func NewMem() *Mem { func NewMem(interval time.Duration) *Mem {
m := &Mem{ m := &Mem{
LineGraph: ui.NewLineGraph(), LineGraph: ui.NewLineGraph(),
interval: time.Second, interval: interval,
} }
m.Label = "Memory Usage" m.Label = "Memory Usage"
m.Data["Main"] = []float64{0} m.Data["Main"] = []float64{0}