diff --git a/main.go b/main.go index c29b9a08..235ad35e 100644 --- a/main.go +++ b/main.go @@ -48,6 +48,7 @@ var ( battery = false statusbar = false netInterface = w.NET_INTERFACE_ALL + bandwidth uint64 cpu *w.CpuWidget batt *w.BatteryWidget @@ -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 @@ -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 } @@ -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) } diff --git a/src/widgets/net.go b/src/widgets/net.go index 3042a108..5adb9072 100644 --- a/src/widgets/net.go +++ b/src/widgets/net.go @@ -3,6 +3,7 @@ package widgets import ( "fmt" "log" + "math" "time" psNet "github.com/shirou/gopsutil/net" @@ -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{} @@ -46,12 +47,12 @@ 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() } }() @@ -59,7 +60,7 @@ func NewNetWidget(netInterface string) *NetWidget { 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) @@ -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 @@ -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) +}