summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorCaleb Bassi <calebjbassi@gmail.com>2019-01-30 15:20:09 -0800
committerCaleb Bassi <calebjbassi@gmail.com>2019-01-30 15:20:09 -0800
commitc5e951115772382acc189ba6fe278eb0ea1724e5 (patch)
tree6315347c8e594287088fe062676901607d3e7a04
parentdaf06909db4f8ba38178dcdc7936d0a0c36d820a (diff)
Fix Battery widget panic (#100)
-rw-r--r--src/widgets/battery.go21
-rw-r--r--src/widgets/cpu.go6
-rw-r--r--src/widgets/disk.go6
-rw-r--r--src/widgets/mem.go6
-rw-r--r--src/widgets/net.go11
-rw-r--r--src/widgets/proc.go3
-rw-r--r--src/widgets/temp.go3
7 files changed, 24 insertions, 32 deletions
diff --git a/src/widgets/battery.go b/src/widgets/battery.go
index 77a5c03..2d59849 100644
--- a/src/widgets/battery.go
+++ b/src/widgets/battery.go
@@ -8,38 +8,30 @@ import (
"sync"
"time"
- ui "github.com/cjbassi/gotop/src/termui"
"github.com/distatus/battery"
+
+ ui "github.com/cjbassi/gotop/src/termui"
)
type Batt struct {
*ui.LineGraph
- Count int // number of batteries
interval time.Duration
}
func NewBatt(renderLock *sync.RWMutex, horizontalScale int) *Batt {
- batts, err := battery.GetAll()
self := &Batt{
LineGraph: ui.NewLineGraph(),
- Count: len(batts),
interval: time.Minute,
}
- self.Title = "Battery Status"
+ self.Title = " Battery Status "
self.HorizontalScale = horizontalScale
- if err != nil {
- log.Printf("failed to get battery info from system: %v", err)
- }
- for i, b := range batts {
- pc := math.Abs(b.Current/b.Full) * 100.0
- self.Data[mkId(i)] = []float64{pc}
- }
+ // intentional duplicate
+ self.update()
self.update()
go func() {
- ticker := time.NewTicker(self.interval)
- for range ticker.C {
+ for range time.NewTicker(self.interval).C {
renderLock.RLock()
self.update()
renderLock.RUnlock()
@@ -57,6 +49,7 @@ func (self *Batt) update() {
batts, err := battery.GetAll()
if err != nil {
log.Printf("failed to get battery info from system: %v", err)
+ return
}
for i, b := range batts {
n := mkId(i)
diff --git a/src/widgets/cpu.go b/src/widgets/cpu.go
index aaa7d07..3ee1b23 100644
--- a/src/widgets/cpu.go
+++ b/src/widgets/cpu.go
@@ -6,8 +6,9 @@ import (
"sync"
"time"
- ui "github.com/cjbassi/gotop/src/termui"
psCPU "github.com/shirou/gopsutil/cpu"
+
+ ui "github.com/cjbassi/gotop/src/termui"
)
type CPU struct {
@@ -64,8 +65,7 @@ func NewCPU(renderLock *sync.RWMutex, interval time.Duration, horizontalScale in
self.update()
go func() {
- ticker := time.NewTicker(self.interval)
- for range ticker.C {
+ for range time.NewTicker(self.interval).C {
self.update()
}
}()
diff --git a/src/widgets/disk.go b/src/widgets/disk.go
index f7392df..4246cb7 100644
--- a/src/widgets/disk.go
+++ b/src/widgets/disk.go
@@ -8,9 +8,10 @@ import (
"sync"
"time"
+ psDisk "github.com/shirou/gopsutil/disk"
+
ui "github.com/cjbassi/gotop/src/termui"
"github.com/cjbassi/gotop/src/utils"
- psDisk "github.com/shirou/gopsutil/disk"
)
type Partition struct {
@@ -44,8 +45,7 @@ func NewDisk(renderLock *sync.RWMutex) *Disk {
self.update()
go func() {
- ticker := time.NewTicker(self.interval)
- for range ticker.C {
+ for range time.NewTicker(self.interval).C {
renderLock.RLock()
self.update()
renderLock.RUnlock()
diff --git a/src/widgets/mem.go b/src/widgets/mem.go
index 4c5a3a5..4bd2695 100644
--- a/src/widgets/mem.go
+++ b/src/widgets/mem.go
@@ -6,9 +6,10 @@ import (
"sync"
"time"
+ psMem "github.com/shirou/gopsutil/mem"
+
ui "github.com/cjbassi/gotop/src/termui"
"github.com/cjbassi/gotop/src/utils"
- psMem "github.com/shirou/gopsutil/mem"
)
type Mem struct {
@@ -29,8 +30,7 @@ func NewMem(renderLock *sync.RWMutex, interval time.Duration, horizontalScale in
self.update()
go func() {
- ticker := time.NewTicker(self.interval)
- for range ticker.C {
+ for range time.NewTicker(self.interval).C {
renderLock.RLock()
self.update()
renderLock.RUnlock()
diff --git a/src/widgets/net.go b/src/widgets/net.go
index 740e044..6f0ac4f 100644
--- a/src/widgets/net.go
+++ b/src/widgets/net.go
@@ -6,14 +6,16 @@ import (
"sync"
"time"
+ psNet "github.com/shirou/gopsutil/net"
+
ui "github.com/cjbassi/gotop/src/termui"
"github.com/cjbassi/gotop/src/utils"
- psNet "github.com/shirou/gopsutil/net"
)
type Net struct {
*ui.Sparklines
interval time.Duration
+
// used to calculate recent network activity
prevRecvTotal uint64
prevSentTotal uint64
@@ -21,10 +23,10 @@ type Net struct {
func NewNet(renderLock *sync.RWMutex) *Net {
recv := ui.NewSparkline()
- recv.Data = []int{0}
+ recv.Data = []int{}
sent := ui.NewSparkline()
- sent.Data = []int{0}
+ sent.Data = []int{}
spark := ui.NewSparklines(recv, sent)
self := &Net{
@@ -36,8 +38,7 @@ func NewNet(renderLock *sync.RWMutex) *Net {
self.update()
go func() {
- ticker := time.NewTicker(self.interval)
- for range ticker.C {
+ for range time.NewTicker(self.interval).C {
renderLock.RLock()
self.update()
renderLock.RUnlock()
diff --git a/src/widgets/proc.go b/src/widgets/proc.go
index 69fd199..13aaa38 100644
--- a/src/widgets/proc.go
+++ b/src/widgets/proc.go
@@ -65,8 +65,7 @@ func NewProc(renderLock *sync.RWMutex) *Proc {
self.update()
go func() {
- ticker := time.NewTicker(self.interval)
- for range ticker.C {
+ for range time.NewTicker(self.interval).C {
renderLock.RLock()
self.update()
renderLock.RUnlock()
diff --git a/src/widgets/temp.go b/src/widgets/temp.go
index 4311233..78c1b3d 100644
--- a/src/widgets/temp.go
+++ b/src/widgets/temp.go
@@ -40,8 +40,7 @@ func NewTemp(renderLock *sync.RWMutex, fahrenheit bool) *Temp {
self.update()
go func() {
- ticker := time.NewTicker(self.interval)
- for range ticker.C {
+ for range time.NewTicker(self.interval).C {
renderLock.RLock()
self.update()
renderLock.RUnlock()