summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMatt Melquiond <matt.llvw@gmail.com>2019-06-12 23:28:52 +0200
committerMatt Melquiond <matt.llvw@gmail.com>2019-06-12 23:28:52 +0200
commitb80258b07e3efcce8e2d58f32e84e72785090cb5 (patch)
treea561e5647ced5acff5ec11ac06eaf910aef1caee
parente1f3488c4cb3a15dc95d5d2c32b8727fea9ddff2 (diff)
Implement #148 and #149
-rw-r--r--README.md2
-rw-r--r--src/widgets/net.go28
2 files changed, 25 insertions, 5 deletions
diff --git a/README.md b/README.md
index 458d534..1239a18 100644
--- a/README.md
+++ b/README.md
@@ -112,7 +112,7 @@ To make a custom colorscheme, check out the [template](./colorschemes/template.g
`-p`, `--percpu` Show each CPU in the CPU widget.
`-a`, `--averagecpu` Show average CPU in the CPU widget.
`-s`, `--statusbar` Show a statusbar with the time.
-`-b`, `--battery` Show battery level widget (`minimal` turns off). [preview](./assets/screenshots/battery.png)
+`-b`, `--battery` Show battery level widget (`minimal` turns off). [preview](./assets/screenshots/battery.png)
`-i`, `--interface=NAME` Select network interface [default: all].
## Built With
diff --git a/src/widgets/net.go b/src/widgets/net.go
index 3042a10..f13965c 100644
--- a/src/widgets/net.go
+++ b/src/widgets/net.go
@@ -3,6 +3,7 @@ package widgets
import (
"fmt"
"log"
+ "strings"
"time"
psNet "github.com/shirou/gopsutil/net"
@@ -25,7 +26,7 @@ type NetWidget struct {
// used to calculate recent network activity
totalBytesRecv uint64
totalBytesSent uint64
- NetInterface string
+ NetInterface []string
}
func NewNetWidget(netInterface string) *NetWidget {
@@ -39,7 +40,7 @@ func NewNetWidget(netInterface string) *NetWidget {
self := &NetWidget{
SparklineGroup: spark,
updateInterval: time.Second,
- NetInterface: netInterface,
+ NetInterface: strings.Split(netInterface, ","),
}
self.Title = " Network Usage "
if netInterface != "all" {
@@ -68,9 +69,28 @@ func (self *NetWidget) update() {
var totalBytesRecv uint64
var totalBytesSent uint64
+ interfaceMap := make(map[string]bool)
+ // Default behaviour
+ interfaceMap[NET_INTERFACE_ALL] = true
+ interfaceMap[NET_INTERFACE_VPN] = false
+ // Build a map with wanted status for each interfaces.
+ for _, iface := range self.NetInterface {
+ if strings.HasPrefix(iface, "!") {
+ interfaceMap[strings.TrimPrefix(iface, "!")] = false
+ } else {
+ // if we specify a wanted interface, remove capture on all.
+ delete(interfaceMap, NET_INTERFACE_ALL)
+ interfaceMap[iface] = true
+ }
+ }
for _, _interface := range interfaces {
- // ignore VPN interface or filter interface by name
- if ((self.NetInterface == NET_INTERFACE_ALL) && (_interface.Name != NET_INTERFACE_VPN)) || (_interface.Name == self.NetInterface) {
+ wanted, ok := interfaceMap[_interface.Name]
+ if wanted && ok { // Simple case
+ totalBytesRecv += _interface.BytesRecv
+ totalBytesSent += _interface.BytesSent
+ } else if ok { // Present but unwanted
+ continue
+ } else if interfaceMap[NET_INTERFACE_ALL] { // Capture other
totalBytesRecv += _interface.BytesRecv
totalBytesSent += _interface.BytesSent
}