diff --git a/README.md b/README.md index 0f667e2..c1393de 100644 --- a/README.md +++ b/README.md @@ -7,6 +7,7 @@ Another terminal based graphical activity monitor, inspired by [gtop](https://gi + @@ -95,6 +96,7 @@ To make a custom colorscheme, check out the [template](./colorschemes/template.g `-v`, `--version` Show version. `-p`, `--percpu` Show each 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 @@ -118,6 +120,7 @@ This will place the built packages into the `dist` folder. - [termbox](https://github.com/nsf/termbox-go) - [gopsutil](https://github.com/shirou/gopsutil) - [goreleaser](https://github.com/goreleaser/goreleaser) +- [battery](https://github.com/distatus/battery) ## Stargazers over time diff --git a/assets/battery.png b/assets/battery.png new file mode 100644 index 0000000..b70f2ab Binary files /dev/null and b/assets/battery.png differ diff --git a/colorschemes/default.go b/colorschemes/default.go index 48db2d3..de65c22 100644 --- a/colorschemes/default.go +++ b/colorschemes/default.go @@ -9,6 +9,8 @@ var Default = Colorscheme{ CPULines: []int{4, 3, 2, 1, 5, 6, 7, 8}, + BattLines: []int{4, 3, 2, 1, 5, 6, 7, 8}, + MainMem: 5, SwapMem: 11, diff --git a/colorschemes/default.json b/colorschemes/default.json index 5d26a8b..99abfaa 100644 --- a/colorschemes/default.json +++ b/colorschemes/default.json @@ -9,6 +9,8 @@ "CPULines": [4, 3, 2, 1, 5, 6, 7, 8], + "BattLines": [4, 3, 2, 1, 5, 6, 7, 8], + "MainMem": 5, "SwapMem": 11, diff --git a/colorschemes/default_dark.go b/colorschemes/default_dark.go index 15f7137..6070985 100644 --- a/colorschemes/default_dark.go +++ b/colorschemes/default_dark.go @@ -9,6 +9,8 @@ var DefaultDark = Colorscheme{ CPULines: []int{4, 3, 2, 1, 5, 6, 7, 8}, + BattLines: []int{4, 3, 2, 1, 5, 6, 7, 8}, + MainMem: 5, SwapMem: 3, diff --git a/colorschemes/monokai.go b/colorschemes/monokai.go index b3dbc23..445f054 100644 --- a/colorschemes/monokai.go +++ b/colorschemes/monokai.go @@ -9,6 +9,8 @@ var Monokai = Colorscheme{ CPULines: []int{81, 70, 208, 197, 249, 141, 221, 186}, + BattLines: []int{81, 70, 208, 197, 249, 141, 221, 186}, + MainMem: 208, SwapMem: 186, diff --git a/colorschemes/solarized.go b/colorschemes/solarized.go index 2ead4df..3ce9997 100644 --- a/colorschemes/solarized.go +++ b/colorschemes/solarized.go @@ -9,6 +9,8 @@ var Solarized = Colorscheme{ CPULines: []int{61, 33, 37, 64, 125, 160, 166, 136}, + BattLines: []int{61, 33, 37, 64, 125, 160, 166, 136}, + MainMem: 125, SwapMem: 166, diff --git a/colorschemes/template.go b/colorschemes/template.go index d303e4b..f410e61 100644 --- a/colorschemes/template.go +++ b/colorschemes/template.go @@ -30,6 +30,8 @@ type Colorscheme struct { // should add at least 8 here CPULines []int + BattLines []int + MainMem int SwapMem int diff --git a/go.mod b/go.mod index e2f7ec6..e0dace1 100644 --- a/go.mod +++ b/go.mod @@ -1,19 +1,22 @@ module github.com/cjbassi/gotop require ( + github.com/0xAX/notificator v0.0.0-20181105090803-d81462e38c21 // indirect github.com/ProtonMail/go-appdir v0.0.0-20180220133335-7c788d1b45c6 github.com/StackExchange/wmi v0.0.0-20180116203802-5d049714c4a6 // indirect github.com/cjbassi/drawille-go v0.0.0-20180329221028-ad535d0f92cd // indirect github.com/cjbassi/termui v0.0.0-20181208033036-e8dd23f6146c 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/go-ole/go-ole v1.2.1 // 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/pmezard/go-difflib v1.0.0 // indirect github.com/shirou/gopsutil v2.18.11+incompatible github.com/shirou/w32 v0.0.0-20160930032740-bb4de0191aa4 // 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 ) diff --git a/go.sum b/go.sum index b869d58..f1007cc 100644 --- a/go.sum +++ b/go.sum @@ -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/go.mod h1:3d8Y9F5mbEUjrYbcJ3rcDxcWbqbttF+011nVZmdRdzc= 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/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/docopt/docopt-go v0.0.0-20180111231733-ee0de3bc6815 h1:bWDMxwH3px2JBh6AyO7hdCn/PkvCZXii8TGj7sbtEbQ= -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 h1:rxw7AZpSwNgPPFdxDNSwQ+TfvOjmSGDK6tlpY7vw9bw= +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/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/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/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/go.mod h1:IuKpRQcYE1Tfu+oAQqaLisqDeXgjyyltCfsaoYN18NQ= 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/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/go.mod h1:5b4v6he4MtMOwMlS0TUMTu2PcXUg8+E1lC7eC3UO/RA= 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= 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-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= diff --git a/main.go b/main.go index 60df7d5..fc6df73 100644 --- a/main.go +++ b/main.go @@ -32,14 +32,16 @@ var ( zoomInterval = 3 helpVisible = false averageLoad = false + battery = false percpuLoad = false - widgetCount = 6 + widgetCount = 7 fahrenheit = false configDir = appdir.New("gotop").UserConfig() logPath = filepath.Join(configDir, "errors.log") stderrLogger = log.New(os.Stderr, "", 0) cpu *w.CPU + batt *w.Batt mem *w.Mem proc *w.Proc net *w.Net @@ -61,6 +63,7 @@ Options: -p, --percpu Show each CPU in the CPU widget. -a, --averagecpu Show average CPU in the CPU widget. -f, --fahrenheit Show temperatures in fahrenheit. + -b, --battery Show battery charge over time (minimal overrides & sets false) Colorschemes: default @@ -81,6 +84,7 @@ Colorschemes: } averageLoad, _ = args["--averagecpu"].(bool) percpuLoad, _ = args["--percpu"].(bool) + battery, _ = args["--battery"].(bool) minimal, _ = args["--minimal"].(bool) if minimal { @@ -145,7 +149,12 @@ func setupGrid() { ui.Body.Set(0, 6, 6, 12, mem) ui.Body.Set(6, 6, 12, 12, proc) } else { - ui.Body.Set(0, 0, 12, 4, cpu) + if battery { + ui.Body.Set(0, 0, 8, 4, cpu) + ui.Body.Set(8, 0, 12, 4, batt) + } else { + ui.Body.Set(0, 0, 12, 4, cpu) + } ui.Body.Set(0, 4, 4, 6, disk) ui.Body.Set(0, 6, 4, 8, temp) @@ -192,6 +201,21 @@ func widgetColors() { if !minimal { temp.TempLow = ui.Color(colorscheme.TempLow) 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() }() if !minimal { + go func() { + batt = w.NewBatt(time.Minute, zoom) + wg.Done() + }() go func() { net = w.NewNet() wg.Done() diff --git a/src/widgets/battery.go b/src/widgets/battery.go new file mode 100644 index 0000000..c087dd2 --- /dev/null +++ b/src/widgets/battery.go @@ -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)) + } +}