2018-12-27 12:06:55 +08:00
|
|
|
package widgets
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"log"
|
|
|
|
"math"
|
|
|
|
"strconv"
|
2019-01-20 12:34:19 +08:00
|
|
|
"sync"
|
2018-12-27 12:06:55 +08:00
|
|
|
"time"
|
|
|
|
|
2019-02-06 17:20:22 +08:00
|
|
|
"github.com/cjbassi/battery"
|
2019-01-31 07:20:09 +08:00
|
|
|
|
|
|
|
ui "github.com/cjbassi/gotop/src/termui"
|
2018-12-27 12:06:55 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
type Batt struct {
|
|
|
|
*ui.LineGraph
|
|
|
|
interval time.Duration
|
|
|
|
}
|
|
|
|
|
2019-01-20 12:34:19 +08:00
|
|
|
func NewBatt(renderLock *sync.RWMutex, horizontalScale int) *Batt {
|
2018-12-27 12:06:55 +08:00
|
|
|
self := &Batt{
|
|
|
|
LineGraph: ui.NewLineGraph(),
|
2019-01-20 11:37:31 +08:00
|
|
|
interval: time.Minute,
|
2018-12-27 12:06:55 +08:00
|
|
|
}
|
2019-01-31 07:20:09 +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
|
|
|
|
2019-01-31 07:20:09 +08:00
|
|
|
// intentional duplicate
|
|
|
|
self.update()
|
2018-12-27 12:06:55 +08:00
|
|
|
self.update()
|
|
|
|
|
|
|
|
go func() {
|
2019-01-31 07:20:09 +08:00
|
|
|
for range time.NewTicker(self.interval).C {
|
2019-01-20 12:34:19 +08:00
|
|
|
renderLock.RLock()
|
2018-12-27 12:06:55 +08:00
|
|
|
self.update()
|
2019-01-20 12:34:19 +08:00
|
|
|
renderLock.RUnlock()
|
2018-12-27 12:06:55 +08:00
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
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)
|
2019-01-31 07:20:09 +08:00
|
|
|
return
|
2018-12-27 12:06:55 +08:00
|
|
|
}
|
|
|
|
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))
|
|
|
|
}
|
|
|
|
}
|