Adds a battery gauge.

This commit is contained in:
Sean E. Russell 2020-02-18 11:27:29 -06:00
parent 6c9e51470e
commit 8ee5950308
5 changed files with 109 additions and 2 deletions

View File

@ -13,7 +13,11 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
> - **Fixed**: for any bug fixes.
> - **Security**: in case of vulnerabilities.
## [3.3.0] -
## [3.x.x] -
- Adds a battery gauge as `power`
## [3.3.0] - 2020-02-17
- Added: Logs are now rotated. Settings are currently hard-coded at 4 files of 5MB
each, so logs shouldn't take up more than 20MB. I'm going to see how many

View File

@ -94,7 +94,7 @@ and these are separated by spaces.
1. Each line is a row
2. Empty lines are skipped
3. Spaces are compressed (so you can do limited visual formatting)
4. Legal widget names are: cpu, disk, mem, temp, batt, net, procs
4. Legal widget names are: cpu, disk, mem, temp, batt, net, procs, power
5. Widget names are not case sensitive
4. The simplest row is a single widget, by name, e.g.
```

View File

@ -170,6 +170,10 @@ func makeWidget(c gotop.Config, widRule widgetRule) interface{} {
i++
}
w = b
case "power":
b := widgets.NewBatteryGauge()
b.BarColor = ui.Color(c.Colorscheme.ProcCursor)
w = b
default:
log.Printf("Invalid widget name %s. Must be one of %v", widRule.Widget, widgetNames)
return ui.NewBlock()

22
termui/gauge.go Normal file
View File

@ -0,0 +1,22 @@
package termui
import (
. "github.com/gizak/termui/v3"
gizak "github.com/gizak/termui/v3/widgets"
)
// LineGraph implements a line graph of data points.
type Gauge struct {
*gizak.Gauge
}
func NewGauge() *Gauge {
return &Gauge{
Gauge: gizak.NewGauge(),
}
}
func (self *Gauge) Draw(buf *Buffer) {
self.Gauge.Draw(buf)
self.Gauge.SetRect(self.Min.X, self.Min.Y, self.Inner.Dx(), self.Inner.Dy())
}

77
widgets/batterygauge.go Normal file
View File

@ -0,0 +1,77 @@
package widgets
import (
"fmt"
"log"
//"math"
//"strconv"
"time"
"github.com/distatus/battery"
"github.com/prometheus/client_golang/prometheus"
. "github.com/xxxserxxx/gotop/termui"
)
type BatteryGauge struct {
*Gauge
metric prometheus.Gauge
}
func NewBatteryGauge() *BatteryGauge {
self := &BatteryGauge{Gauge: NewGauge()}
self.Title = " Power Level "
self.update()
go func() {
for range time.NewTicker(time.Second).C {
self.Lock()
self.update()
self.Unlock()
}
}()
return self
}
func (b *BatteryGauge) EnableMetric() {
bats, err := battery.GetAll()
if err != nil {
log.Printf("error setting up metrics: %v", err)
return
}
mx := 0.0
cu := 0.0
for _, bat := range bats {
mx += bat.Full
cu += bat.Current
gauge := prometheus.NewGauge(prometheus.GaugeOpts{
Namespace: "gotop",
Subsystem: "battery",
Name: "total",
})
gauge.Set(cu / mx)
b.metric = gauge
prometheus.MustRegister(gauge)
}
}
func (self *BatteryGauge) update() {
bats, err := battery.GetAll()
if err != nil {
log.Printf("error setting up metrics: %v", err)
return
}
mx := 0.0
cu := 0.0
for _, bat := range bats {
mx += bat.Full
cu += bat.Current
}
self.Percent = int((cu / mx) * 100.0)
self.Label = fmt.Sprintf("%d%%", self.Percent)
if self.metric != nil {
self.metric.Set(cu / mx)
}
}