Adds battery support

Cleans up mod file; adds flag for battery.

Adds documentation; fixes missing color sets for battery.
This commit is contained in:
Sean E. Russell 2018-12-26 22:06:55 -06:00
parent 7b77956fee
commit 5ee4b4a978
12 changed files with 134 additions and 6 deletions

View File

@ -7,6 +7,7 @@ Another terminal based graphical activity monitor, inspired by [gtop](https://gi
<img src="./assets/demo.gif" /> <img src="./assets/demo.gif" />
<img src="./assets/minimal.png" width="96%" /> <img src="./assets/minimal.png" width="96%" />
<img src="./assets/battery.png"/>
</div> </div>
@ -95,6 +96,7 @@ To make a custom colorscheme, check out the [template](./colorschemes/template.g
`-v`, `--version` Show version. `-v`, `--version` Show version.
`-p`, `--percpu` Show each CPU in the CPU widget. `-p`, `--percpu` Show each CPU in the CPU widget.
`-a`, `--averagecpu` Show average CPU in the CPU widget. `-a`, `--averagecpu` Show average CPU in the CPU widget.
`-b`, `--battery` Show battery level (defaults to false, `minimal` turns off)
## Building deb/rpms ## Building deb/rpms
@ -118,6 +120,7 @@ This will place the built packages into the `dist` folder.
- [termbox](https://github.com/nsf/termbox-go) - [termbox](https://github.com/nsf/termbox-go)
- [gopsutil](https://github.com/shirou/gopsutil) - [gopsutil](https://github.com/shirou/gopsutil)
- [goreleaser](https://github.com/goreleaser/goreleaser) - [goreleaser](https://github.com/goreleaser/goreleaser)
- [battery](https://github.com/distatus/battery)
## Stargazers over time ## Stargazers over time

BIN
assets/battery.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 93 KiB

View File

@ -9,6 +9,8 @@ var Default = Colorscheme{
CPULines: []int{4, 3, 2, 1, 5, 6, 7, 8}, CPULines: []int{4, 3, 2, 1, 5, 6, 7, 8},
BattLines: []int{4, 3, 2, 1, 5, 6, 7, 8},
MainMem: 5, MainMem: 5,
SwapMem: 11, SwapMem: 11,

View File

@ -9,6 +9,8 @@
"CPULines": [4, 3, 2, 1, 5, 6, 7, 8], "CPULines": [4, 3, 2, 1, 5, 6, 7, 8],
"BattLines": [4, 3, 2, 1, 5, 6, 7, 8],
"MainMem": 5, "MainMem": 5,
"SwapMem": 11, "SwapMem": 11,

View File

@ -9,6 +9,8 @@ var DefaultDark = Colorscheme{
CPULines: []int{4, 3, 2, 1, 5, 6, 7, 8}, CPULines: []int{4, 3, 2, 1, 5, 6, 7, 8},
BattLines: []int{4, 3, 2, 1, 5, 6, 7, 8},
MainMem: 5, MainMem: 5,
SwapMem: 3, SwapMem: 3,

View File

@ -9,6 +9,8 @@ var Monokai = Colorscheme{
CPULines: []int{81, 70, 208, 197, 249, 141, 221, 186}, CPULines: []int{81, 70, 208, 197, 249, 141, 221, 186},
BattLines: []int{81, 70, 208, 197, 249, 141, 221, 186},
MainMem: 208, MainMem: 208,
SwapMem: 186, SwapMem: 186,

View File

@ -9,6 +9,8 @@ var Solarized = Colorscheme{
CPULines: []int{61, 33, 37, 64, 125, 160, 166, 136}, CPULines: []int{61, 33, 37, 64, 125, 160, 166, 136},
BattLines: []int{61, 33, 37, 64, 125, 160, 166, 136},
MainMem: 125, MainMem: 125,
SwapMem: 166, SwapMem: 166,

View File

@ -30,6 +30,8 @@ type Colorscheme struct {
// should add at least 8 here // should add at least 8 here
CPULines []int CPULines []int
BattLines []int
MainMem int MainMem int
SwapMem int SwapMem int

7
go.mod
View File

@ -1,19 +1,22 @@
module github.com/cjbassi/gotop module github.com/cjbassi/gotop
require ( require (
github.com/0xAX/notificator v0.0.0-20181105090803-d81462e38c21 // indirect
github.com/ProtonMail/go-appdir v0.0.0-20180220133335-7c788d1b45c6 github.com/ProtonMail/go-appdir v0.0.0-20180220133335-7c788d1b45c6
github.com/StackExchange/wmi v0.0.0-20180116203802-5d049714c4a6 // indirect github.com/StackExchange/wmi v0.0.0-20180116203802-5d049714c4a6 // indirect
github.com/cjbassi/drawille-go v0.0.0-20180329221028-ad535d0f92cd // indirect github.com/cjbassi/drawille-go v0.0.0-20180329221028-ad535d0f92cd // indirect
github.com/cjbassi/termui v0.0.0-20181208033036-e8dd23f6146c github.com/cjbassi/termui v0.0.0-20181208033036-e8dd23f6146c
github.com/davecgh/go-spew v1.1.1 // indirect github.com/davecgh/go-spew v1.1.1 // indirect
github.com/docopt/docopt-go v0.0.0-20180111231733-ee0de3bc6815 github.com/distatus/battery v0.0.0-20170521010419-916919eb43bd
github.com/docopt/docopt.go v0.0.0-20180111231733-ee0de3bc6815 github.com/docopt/docopt.go v0.0.0-20180111231733-ee0de3bc6815
github.com/go-ole/go-ole v1.2.1 // indirect github.com/go-ole/go-ole v1.2.1 // indirect
github.com/mattn/go-runewidth v0.0.2 // indirect github.com/mattn/go-runewidth v0.0.2 // indirect
github.com/nexulh/batmond v0.0.0-20180213215845-395ac7fdba8c // indirect
github.com/nightlyone/lockfile v0.0.0-20180618180623-0ad87eef1443 // indirect
github.com/nsf/termbox-go v0.0.0-20180407224525-3e24a7b6661e // indirect github.com/nsf/termbox-go v0.0.0-20180407224525-3e24a7b6661e // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/shirou/gopsutil v2.18.11+incompatible github.com/shirou/gopsutil v2.18.11+incompatible
github.com/shirou/w32 v0.0.0-20160930032740-bb4de0191aa4 // indirect github.com/shirou/w32 v0.0.0-20160930032740-bb4de0191aa4 // indirect
github.com/stretchr/testify v1.2.2 // indirect github.com/stretchr/testify v1.2.2 // indirect
golang.org/x/sys v0.0.0-20180406135729-3b87a42e500a // indirect golang.org/x/sys v0.0.0-20181221143128-b4a75ba826a6 // indirect
) )

22
go.sum
View File

@ -1,3 +1,5 @@
github.com/0xAX/notificator v0.0.0-20181105090803-d81462e38c21 h1:moSC7ACaTejHmVRRwfDTMgByRSwjg2vZooncdWLj7o8=
github.com/0xAX/notificator v0.0.0-20181105090803-d81462e38c21/go.mod h1:NtXa9WwQsukMHZpjNakTTz0LArxvGYdPA9CjIcUSZ6s=
github.com/ProtonMail/go-appdir v0.0.0-20180220133335-7c788d1b45c6 h1:de/SvQsi6Oou9TJYp6Kp17S+JjXGn1w8XVYWFlE0z/U= github.com/ProtonMail/go-appdir v0.0.0-20180220133335-7c788d1b45c6 h1:de/SvQsi6Oou9TJYp6Kp17S+JjXGn1w8XVYWFlE0z/U=
github.com/ProtonMail/go-appdir v0.0.0-20180220133335-7c788d1b45c6/go.mod h1:3d8Y9F5mbEUjrYbcJ3rcDxcWbqbttF+011nVZmdRdzc= github.com/ProtonMail/go-appdir v0.0.0-20180220133335-7c788d1b45c6/go.mod h1:3d8Y9F5mbEUjrYbcJ3rcDxcWbqbttF+011nVZmdRdzc=
github.com/StackExchange/wmi v0.0.0-20180116203802-5d049714c4a6 h1:fLjPD/aNc3UIOA6tDi6QXUemppXK3P9BI7mr2hd6gx8= github.com/StackExchange/wmi v0.0.0-20180116203802-5d049714c4a6 h1:fLjPD/aNc3UIOA6tDi6QXUemppXK3P9BI7mr2hd6gx8=
@ -8,18 +10,27 @@ github.com/cjbassi/termui v0.0.0-20181208033036-e8dd23f6146c h1:vcaCtK8ObawtpQRW
github.com/cjbassi/termui v0.0.0-20181208033036-e8dd23f6146c/go.mod h1:rqXckrwz+i0fH/zNwU6AdBNULHwmZsgehnSlhKP5i2Q= github.com/cjbassi/termui v0.0.0-20181208033036-e8dd23f6146c/go.mod h1:rqXckrwz+i0fH/zNwU6AdBNULHwmZsgehnSlhKP5i2Q=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/docopt/docopt-go v0.0.0-20180111231733-ee0de3bc6815 h1:bWDMxwH3px2JBh6AyO7hdCn/PkvCZXii8TGj7sbtEbQ= github.com/distatus/battery v0.0.0-20170521010419-916919eb43bd h1:rxw7AZpSwNgPPFdxDNSwQ+TfvOjmSGDK6tlpY7vw9bw=
github.com/docopt/docopt-go v0.0.0-20180111231733-ee0de3bc6815/go.mod h1:WwZ+bS3ebgob9U8Nd0kOddGdZWjyMGR8Wziv+TBNwSE= github.com/distatus/battery v0.0.0-20170521010419-916919eb43bd/go.mod h1:gGO7GxHTi1zlRT+cAj8uGG0/8HFiqAeH0TJvoipnuPs=
github.com/docopt/docopt.go v0.0.0-20180111231733-ee0de3bc6815 h1:HMAfwOa33y82IaQEKQDfUCiwNlxtM1iw7HLM9ru0RNc= github.com/docopt/docopt.go v0.0.0-20180111231733-ee0de3bc6815 h1:HMAfwOa33y82IaQEKQDfUCiwNlxtM1iw7HLM9ru0RNc=
github.com/docopt/docopt.go v0.0.0-20180111231733-ee0de3bc6815/go.mod h1:l7JNRynTRuqe45tpIyItHNqZWTxywYjp87MWTOnU5cg= github.com/docopt/docopt.go v0.0.0-20180111231733-ee0de3bc6815/go.mod h1:l7JNRynTRuqe45tpIyItHNqZWTxywYjp87MWTOnU5cg=
github.com/go-ole/go-ole v1.2.1 h1:2lOsA72HgjxAuMlKpFiCbHTvu44PIVkZ5hqm3RSdI/E= github.com/go-ole/go-ole v1.2.1 h1:2lOsA72HgjxAuMlKpFiCbHTvu44PIVkZ5hqm3RSdI/E=
github.com/go-ole/go-ole v1.2.1/go.mod h1:7FAglXiTm7HKlQRDeOQ6ZNUHidzCWXuZWq/1dTyBNF8= github.com/go-ole/go-ole v1.2.1/go.mod h1:7FAglXiTm7HKlQRDeOQ6ZNUHidzCWXuZWq/1dTyBNF8=
github.com/jessevdk/go-flags v1.4.0/go.mod h1:4FA24M0QyGHXBuZZK/XkWh8h0e1EYbRYJSGM75WSRxI=
github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo=
github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
github.com/mattn/go-runewidth v0.0.2 h1:UnlwIPBGaTZfPQ6T1IGzPI0EkYAQmT9fAEJ/poFC63o= github.com/mattn/go-runewidth v0.0.2 h1:UnlwIPBGaTZfPQ6T1IGzPI0EkYAQmT9fAEJ/poFC63o=
github.com/mattn/go-runewidth v0.0.2/go.mod h1:LwmH8dsx7+W8Uxz3IHJYH5QSwggIsqBzpuz5H//U1FU= github.com/mattn/go-runewidth v0.0.2/go.mod h1:LwmH8dsx7+W8Uxz3IHJYH5QSwggIsqBzpuz5H//U1FU=
github.com/nexulh/batmond v0.0.0-20180213215845-395ac7fdba8c h1:Hiq+wfawWPyyX2AVpIhEiVsoKVQdc8zQwAwiKiVZbTI=
github.com/nexulh/batmond v0.0.0-20180213215845-395ac7fdba8c/go.mod h1:Ho2yA2Aa5zD+GEgmhFdLLpHVFoGJXF/Et15iRydTFtY=
github.com/nightlyone/lockfile v0.0.0-20180618180623-0ad87eef1443 h1:+2OJrU8cmOstEoh0uQvYemRGVH1O6xtO2oANUWHFnP0=
github.com/nightlyone/lockfile v0.0.0-20180618180623-0ad87eef1443/go.mod h1:JbxfV1Iifij2yhRjXai0oFrbpxszXHRx1E5RuM26o4Y=
github.com/nsf/termbox-go v0.0.0-20180407224525-3e24a7b6661e h1:w2JDz0jtOlFFdvtUXISyYPFwmbZnwKL1mRDT0tKDvuk= github.com/nsf/termbox-go v0.0.0-20180407224525-3e24a7b6661e h1:w2JDz0jtOlFFdvtUXISyYPFwmbZnwKL1mRDT0tKDvuk=
github.com/nsf/termbox-go v0.0.0-20180407224525-3e24a7b6661e/go.mod h1:IuKpRQcYE1Tfu+oAQqaLisqDeXgjyyltCfsaoYN18NQ= github.com/nsf/termbox-go v0.0.0-20180407224525-3e24a7b6661e/go.mod h1:IuKpRQcYE1Tfu+oAQqaLisqDeXgjyyltCfsaoYN18NQ=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/serussell/battery v0.0.0-20170521010419-b399f895029c/go.mod h1:0eSBUZHzM+0VCcUzFSkpxGxOg+jkMTT2cr4U/zupwsk=
github.com/shirou/gopsutil v2.18.11+incompatible h1:PMFTKnFTr/YTRW5rbLK4vWALV3a+IGXse5nvhSjztmg= github.com/shirou/gopsutil v2.18.11+incompatible h1:PMFTKnFTr/YTRW5rbLK4vWALV3a+IGXse5nvhSjztmg=
github.com/shirou/gopsutil v2.18.11+incompatible/go.mod h1:5b4v6he4MtMOwMlS0TUMTu2PcXUg8+E1lC7eC3UO/RA= github.com/shirou/gopsutil v2.18.11+incompatible/go.mod h1:5b4v6he4MtMOwMlS0TUMTu2PcXUg8+E1lC7eC3UO/RA=
github.com/shirou/w32 v0.0.0-20160930032740-bb4de0191aa4 h1:udFKJ0aHUL60LboW/A+DfgoHVedieIzIXE8uylPue0U= github.com/shirou/w32 v0.0.0-20160930032740-bb4de0191aa4 h1:udFKJ0aHUL60LboW/A+DfgoHVedieIzIXE8uylPue0U=
@ -28,3 +39,10 @@ github.com/stretchr/testify v1.2.2 h1:bSDNvY7ZPG5RlJ8otE/7V6gMiyenm9RtJ7IUVIAoJ1
github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs= github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
golang.org/x/sys v0.0.0-20180406135729-3b87a42e500a h1:DwI0ihryIiWlRUKL/ii7Snvn4LiL9TvMoVZq3qMbffg= golang.org/x/sys v0.0.0-20180406135729-3b87a42e500a h1:DwI0ihryIiWlRUKL/ii7Snvn4LiL9TvMoVZq3qMbffg=
golang.org/x/sys v0.0.0-20180406135729-3b87a42e500a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= golang.org/x/sys v0.0.0-20180406135729-3b87a42e500a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20181221143128-b4a75ba826a6 h1:IcgEB62HYgAhX0Nd/QrVgZlxlcyxbGQHElLUhW2X4Fo=
golang.org/x/sys v0.0.0-20181221143128-b4a75ba826a6/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v2 v2.2.1/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
howett.net/plist v0.0.0-20181124034731-591f970eefbb h1:jhnBjNi9UFpfpl8YZhA9CrOqpnJdvzuiHsl/dnxl11M=
howett.net/plist v0.0.0-20181124034731-591f970eefbb/go.mod h1:vMygbs4qMhSZSc4lCUl2OEE+rDiIIJAIdR4m7MiMcm0=

30
main.go
View File

@ -32,14 +32,16 @@ var (
zoomInterval = 3 zoomInterval = 3
helpVisible = false helpVisible = false
averageLoad = false averageLoad = false
battery = false
percpuLoad = false percpuLoad = false
widgetCount = 6 widgetCount = 7
fahrenheit = false fahrenheit = false
configDir = appdir.New("gotop").UserConfig() configDir = appdir.New("gotop").UserConfig()
logPath = filepath.Join(configDir, "errors.log") logPath = filepath.Join(configDir, "errors.log")
stderrLogger = log.New(os.Stderr, "", 0) stderrLogger = log.New(os.Stderr, "", 0)
cpu *w.CPU cpu *w.CPU
batt *w.Batt
mem *w.Mem mem *w.Mem
proc *w.Proc proc *w.Proc
net *w.Net net *w.Net
@ -61,6 +63,7 @@ Options:
-p, --percpu Show each CPU in the CPU widget. -p, --percpu Show each CPU in the CPU widget.
-a, --averagecpu Show average CPU in the CPU widget. -a, --averagecpu Show average CPU in the CPU widget.
-f, --fahrenheit Show temperatures in fahrenheit. -f, --fahrenheit Show temperatures in fahrenheit.
-b, --battery Show battery charge over time (minimal overrides & sets false)
Colorschemes: Colorschemes:
default default
@ -81,6 +84,7 @@ Colorschemes:
} }
averageLoad, _ = args["--averagecpu"].(bool) averageLoad, _ = args["--averagecpu"].(bool)
percpuLoad, _ = args["--percpu"].(bool) percpuLoad, _ = args["--percpu"].(bool)
battery, _ = args["--battery"].(bool)
minimal, _ = args["--minimal"].(bool) minimal, _ = args["--minimal"].(bool)
if minimal { if minimal {
@ -144,8 +148,13 @@ func setupGrid() {
ui.Body.Set(0, 0, 12, 6, cpu) ui.Body.Set(0, 0, 12, 6, cpu)
ui.Body.Set(0, 6, 6, 12, mem) ui.Body.Set(0, 6, 6, 12, mem)
ui.Body.Set(6, 6, 12, 12, proc) ui.Body.Set(6, 6, 12, 12, proc)
} else {
if battery {
ui.Body.Set(0, 0, 8, 4, cpu)
ui.Body.Set(8, 0, 12, 4, batt)
} else { } else {
ui.Body.Set(0, 0, 12, 4, cpu) ui.Body.Set(0, 0, 12, 4, cpu)
}
ui.Body.Set(0, 4, 4, 6, disk) ui.Body.Set(0, 4, 4, 6, disk)
ui.Body.Set(0, 6, 4, 8, temp) ui.Body.Set(0, 6, 4, 8, temp)
@ -192,6 +201,21 @@ func widgetColors() {
if !minimal { if !minimal {
temp.TempLow = ui.Color(colorscheme.TempLow) temp.TempLow = ui.Color(colorscheme.TempLow)
temp.TempHigh = ui.Color(colorscheme.TempHigh) temp.TempHigh = ui.Color(colorscheme.TempHigh)
var battKeys []string
for key := range batt.Data {
battKeys = append(battKeys, key)
}
sort.Strings(battKeys)
bi := 0
for _, v := range battKeys {
if bi >= len(colorscheme.BattLines) {
// assuming colorscheme for CPU lines is not empty
bi = 0
}
c := colorscheme.BattLines[bi]
batt.LineColor[v] = ui.Color(c)
bi++
}
} }
} }
@ -212,6 +236,10 @@ func initWidgets() {
wg.Done() wg.Done()
}() }()
if !minimal { if !minimal {
go func() {
batt = w.NewBatt(time.Minute, zoom)
wg.Done()
}()
go func() { go func() {
net = w.NewNet() net = w.NewNet()
wg.Done() wg.Done()

64
src/widgets/battery.go Normal file
View File

@ -0,0 +1,64 @@
package widgets
import (
"fmt"
"log"
"math"
"strconv"
"time"
ui "github.com/cjbassi/termui"
"github.com/distatus/battery"
)
type Batt struct {
*ui.LineGraph
Count int // number of batteries
interval time.Duration
}
func NewBatt(interval time.Duration, zoom int) *Batt {
batts, err := battery.GetAll()
self := &Batt{
LineGraph: ui.NewLineGraph(),
Count: len(batts),
interval: interval,
}
self.Label = "Battery Status"
self.Zoom = zoom
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))
}
}