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

Created the option to add total Mbps as an opiton. #169

Open
wants to merge 1 commit 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
11 changes: 10 additions & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ var (
battery = false
statusbar = false
netInterface = w.NET_INTERFACE_ALL
bandwidth uint64

cpu *w.CpuWidget
batt *w.BatteryWidget
Expand Down Expand Up @@ -77,6 +78,7 @@ Options:
-s, --statusbar Show a statusbar with the time.
-b, --battery Show battery level widget ('minimal' turns off).
-i, --interface=NAME Select network interface [default: all].
-B, --bandwidth=bits Specify the number of bits per seconds.

Colorschemes:
default
Expand Down Expand Up @@ -119,6 +121,13 @@ Colorschemes:
tempScale = w.Fahrenheit
}
netInterface, _ = args["--interface"].(string)
bandString, _ := args["--bandwidth"].(string)
if bandString == "" {
return nil
}
if bandwidth, err = strconv.ParseUint(bandString, 10, 64); err != nil {
log.Fatalf("Could not parse Uint from user input: %v", err)
}

return nil
}
Expand Down Expand Up @@ -264,7 +273,7 @@ func initWidgets() {
if battery {
batt = w.NewBatteryWidget(graphHorizontalScale)
}
net = w.NewNetWidget(netInterface)
net = w.NewNetWidget(netInterface, bandwidth)
disk = w.NewDiskWidget()
temp = w.NewTempWidget(tempScale)
}
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"
"math"
"time"

psNet "github.com/shirou/gopsutil/net"
Expand All @@ -28,7 +29,7 @@ type NetWidget struct {
NetInterface string
}

func NewNetWidget(netInterface string) *NetWidget {
func NewNetWidget(netInterface string, bandwidth uint64) *NetWidget {
recvSparkline := ui.NewSparkline()
recvSparkline.Data = []int{}

Expand All @@ -46,20 +47,20 @@ func NewNetWidget(netInterface string) *NetWidget {
self.Title = fmt.Sprintf(" Network Usage: %s ", netInterface)
}

self.update()
self.update(bandwidth)

go func() {
for range time.NewTicker(self.updateInterval).C {
self.Lock()
self.update()
self.update(bandwidth)
self.Unlock()
}
}()

return self
}

func (self *NetWidget) update() {
func (self *NetWidget) update(bandwidth uint64) {
interfaces, err := psNet.IOCounters(true)
if err != nil {
log.Printf("failed to get network activity from gopsutil: %v", err)
Expand Down Expand Up @@ -94,8 +95,15 @@ func (self *NetWidget) update() {
recentBytesSent = 0
}

self.Lines[0].Data = append(self.Lines[0].Data, int(recentBytesRecv))
self.Lines[1].Data = append(self.Lines[1].Data, int(recentBytesSent))
if bandwidth == 0 {
self.Lines[0].Data = append(self.Lines[0].Data, int(recentBytesRecv))
self.Lines[1].Data = append(self.Lines[1].Data, int(recentBytesSent))
}

if bandwidth > 0 {
self.Lines[0].Data = append(self.Lines[0].Data, getPercentage(recentBytesRecv, bandwidth))
self.Lines[1].Data = append(self.Lines[1].Data, getPercentage(recentBytesSent, bandwidth))
}
}

// used in later calls to update
Expand All @@ -118,3 +126,13 @@ func (self *NetWidget) update() {
self.Lines[i].Title2 = fmt.Sprintf(" %s/s: %9.1f %2s/s", label, recentConverted, unitRecent)
}
}

// getPercentage is used to calculate the percentage of the total bandwidth
// entered by the user on launch or in the configuration file.
func getPercentage(bits, bandwidth uint64) int {
total := float64(bits)
// 1048576 is the total bits in a mebibit
cap := float64(bandwidth * 1048576)
percent := math.Floor((total / cap) * 100)
return int(percent)
}