From b80258b07e3efcce8e2d58f32e84e72785090cb5 Mon Sep 17 00:00:00 2001 From: Matt Melquiond Date: Wed, 12 Jun 2019 23:28:52 +0200 Subject: [PATCH 1/2] Implement #148 and #149 --- README.md | 2 +- src/widgets/net.go | 28 ++++++++++++++++++++++++---- 2 files changed, 25 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index 458d534c..1239a186 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 3042a108..f13965c7 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 } From fcdb71bfad2c782a480e980821f085e090cb437c Mon Sep 17 00:00:00 2001 From: Matt Melquiond Date: Mon, 9 Sep 2019 20:46:34 +0200 Subject: [PATCH 2/2] added cli usage info, removed useless type alias --- README.md | 4 ++++ main.go | 4 ++++ src/widgets/net.go | 2 -- 3 files changed, 8 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 1239a186..4cda841e 100644 --- a/README.md +++ b/README.md @@ -115,6 +115,10 @@ To make a custom colorscheme, check out the [template](./colorschemes/template.g `-b`, `--battery` Show battery level widget (`minimal` turns off). [preview](./assets/screenshots/battery.png) `-i`, `--interface=NAME` Select network interface [default: all]. +Several interfaces can be defined using comma separated values. + +Interfaces can also be ignored using `!` + ## Built With - [gizak/termui](https://github.com/gizak/termui) diff --git a/main.go b/main.go index c29b9a08..6c55db0c 100644 --- a/main.go +++ b/main.go @@ -78,6 +78,10 @@ Options: -b, --battery Show battery level widget ('minimal' turns off). -i, --interface=NAME Select network interface [default: all]. +Several interfaces can be defined using comma separated values. + +Interfaces can also be ignored using ! + Colorschemes: default default-dark (for white background) diff --git a/src/widgets/net.go b/src/widgets/net.go index f13965c7..3ff296b6 100644 --- a/src/widgets/net.go +++ b/src/widgets/net.go @@ -17,8 +17,6 @@ const ( NET_INTERFACE_VPN = "tun0" ) -type NetInterface string - type NetWidget struct { *ui.SparklineGroup updateInterval time.Duration