Skip to content
This repository has been archived by the owner on Aug 29, 2020. It is now read-only.

Implement #148 and #149 #152

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -112,9 +112,13 @@ 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].
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it would be a good idea to let users know in the readme that many interfaces can be defined with CSVs and that interfaces can be ignored with !

The same documentation should also appear in the cli usage text

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

NICE
image


Several interfaces can be defined using comma separated values.

Interfaces can also be ignored using `!`

## Built With

- [gizak/termui](https://github.com/gizak/termui)
Expand Down
4 changes: 4 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
30 changes: 24 additions & 6 deletions src/widgets/net.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package widgets
import (
"fmt"
"log"
"strings"
"time"

psNet "github.com/shirou/gopsutil/net"
Expand All @@ -16,16 +17,14 @@ const (
NET_INTERFACE_VPN = "tun0"
)

type NetInterface string

type NetWidget struct {
*ui.SparklineGroup
updateInterval time.Duration

// used to calculate recent network activity
totalBytesRecv uint64
totalBytesSent uint64
NetInterface string
NetInterface []string
}

func NewNetWidget(netInterface string) *NetWidget {
Expand All @@ -39,7 +38,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" {
Expand Down Expand Up @@ -68,9 +67,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
}
Expand Down