summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorCaleb Bassi <calebjbassi@gmail.com>2018-03-09 00:27:46 -0800
committerCaleb Bassi <calebjbassi@gmail.com>2018-03-09 00:27:46 -0800
commit02219b68894fbd268b83cd1fb2864f444e4d75f7 (patch)
treec1e12d73c88b2da7e84f381d27dddc1d1870edd2
parent479491298ea80dd88e00cfaa6bfb0132a879828d (diff)
Added rate; closes #5
-rw-r--r--gotop.go15
-rw-r--r--widgets/cpu.go6
-rw-r--r--widgets/mem.go4
3 files changed, 16 insertions, 9 deletions
diff --git a/gotop.go b/gotop.go
index 74a516f..5b00ab6 100644
--- a/gotop.go
+++ b/gotop.go
@@ -4,6 +4,7 @@ import (
"fmt"
"os"
"os/signal"
+ "strconv"
"syscall"
"time"
@@ -28,7 +29,8 @@ var (
colorscheme = colorschemes.Default
- minimal = false
+ minimal = false
+ interval = time.Second
cpu *w.CPU
mem *w.Mem
@@ -48,6 +50,7 @@ Options:
-c, --color <name> Set a colorscheme.
-h, --help Show this screen.
-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.
Colorschemes:
@@ -63,6 +66,10 @@ Colorschemes:
}
minimal, _ = args["--minimal"].(bool)
+
+ rateStr, _ := args["--rate"].(string)
+ rate, _ := strconv.Atoi(rateStr)
+ interval = time.Second / time.Duration(rate)
}
func handleColorscheme(cs string) {
@@ -156,8 +163,8 @@ func main() {
// need to do this before initializing widgets so that they can inherit the colors
termuiColors()
- cpu = w.NewCPU()
- mem = w.NewMem()
+ cpu = w.NewCPU(interval)
+ mem = w.NewMem(interval)
proc = w.NewProc(procLoaded, keyPressed)
if !minimal {
net = w.NewNet()
@@ -194,7 +201,7 @@ func main() {
// all rendering done here
go func() {
ui.Render(ui.Body)
- drawTick := time.NewTicker(time.Second)
+ drawTick := time.NewTicker(interval)
for {
select {
case <-helpToggled:
diff --git a/widgets/cpu.go b/widgets/cpu.go
index cc98bf8..82c097c 100644
--- a/widgets/cpu.go
+++ b/widgets/cpu.go
@@ -14,12 +14,12 @@ type CPU struct {
interval time.Duration
}
-func NewCPU() *CPU {
+func NewCPU(interval time.Duration) *CPU {
count, _ := psCPU.Counts(false)
c := &CPU{
LineGraph: ui.NewLineGraph(),
count: count,
- interval: time.Second,
+ interval: interval,
}
c.Label = "CPU Usage"
for i := 0; i < c.count; i++ {
@@ -41,7 +41,7 @@ func NewCPU() *CPU {
func (c *CPU) update() {
// 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
- percent, _ := psCPU.Percent(time.Second, true)
+ percent, _ := psCPU.Percent(c.interval, true)
for i := 0; i < c.count; i++ {
key := "CPU" + strconv.Itoa(i+1)
c.Data[key] = append(c.Data[key], percent[i])
diff --git a/widgets/mem.go b/widgets/mem.go
index 06a885c..70bf9ae 100644
--- a/widgets/mem.go
+++ b/widgets/mem.go
@@ -12,10 +12,10 @@ type Mem struct {
interval time.Duration
}
-func NewMem() *Mem {
+func NewMem(interval time.Duration) *Mem {
m := &Mem{
LineGraph: ui.NewLineGraph(),
- interval: time.Second,
+ interval: interval,
}
m.Label = "Memory Usage"
m.Data["Main"] = []float64{0}