2018-12-27 12:06:55 +08:00
|
|
|
package widgets
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"log"
|
|
|
|
"math"
|
|
|
|
"strconv"
|
|
|
|
"time"
|
|
|
|
|
2019-01-02 01:04:31 +08:00
|
|
|
ui "github.com/cjbassi/gotop/src/termui"
|
2018-12-27 12:06:55 +08:00
|
|
|
"github.com/distatus/battery"
|
|
|
|
)
|
|
|
|
|
|
|
|
type Batt struct {
|
|
|
|
*ui.LineGraph
|
|
|
|
Count int // number of batteries
|
|
|
|
interval time.Duration
|
|
|
|
}
|
|
|
|
|
2019-01-20 11:37:31 +08:00
|
|
|
func NewBatt(horizontalScale int) *Batt {
|
2018-12-27 12:06:55 +08:00
|
|
|
batts, err := battery.GetAll()
|
|
|
|
self := &Batt{
|
|
|
|
LineGraph: ui.NewLineGraph(),
|
|
|
|
Count: len(batts),
|
2019-01-20 11:37:31 +08:00
|
|
|
interval: time.Minute,
|
2018-12-27 12:06:55 +08:00
|
|
|
}
|
2019-01-02 01:04:31 +08:00
|
|
|
self.Title = "Battery Status"
|
2019-01-13 08:31:37 +08:00
|
|
|
self.HorizontalScale = horizontalScale
|
2018-12-27 12:06:55 +08:00
|
|
|
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}
|
|
|
|
}
|
|
|
|
|
|
|
|
self.update()
|
|
|
|
|
|
|
|
go func() {
|
|
|
|
ticker := time.NewTicker(self.interval)
|
|
|
|
for range ticker.C {
|
|
|
|
self.update()
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
return self
|
|
|
|
}
|
|
|
|
|
|
|
|
func mkId(i int) string {
|
|
|
|
return "Batt" + strconv.Itoa(i)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (self *Batt) update() {
|
|
|
|
batts, err := battery.GetAll()
|
|
|
|
if err != nil {
|
|
|
|
log.Printf("failed to get battery info from system: %v", err)
|
|
|
|
}
|
|
|
|
for i, b := range batts {
|
|
|
|
n := mkId(i)
|
|
|
|
pc := math.Abs(b.Current/b.Full) * 100.0
|
|
|
|
self.Data[n] = append(self.Data[n], pc)
|
|
|
|
self.Labels[n] = fmt.Sprintf("%3.0f%% %.0f/%.0f", pc, math.Abs(b.Current), math.Abs(b.Full))
|
|
|
|
}
|
|
|
|
}
|