summaryrefslogtreecommitdiffstats
path: root/widgets
diff options
context:
space:
mode:
authorSean E. Russell <ser@ser1.net>2020-04-27 20:33:41 -0500
committerSean E. Russell <ser@ser1.net>2020-04-27 20:33:41 -0500
commit285d4d02972e5d3e52efa6554fcbb08b42577f7c (patch)
treea06df20b360abdbc011b697e01e7ac9df5022117 /widgets
parent1e78e6faa096237440e9cbbe254951f6d8588624 (diff)
Go vet/lint cleanups
Diffstat (limited to 'widgets')
-rw-r--r--widgets/battery.go14
-rw-r--r--widgets/batterygauge.go16
-rw-r--r--widgets/cpu.go86
-rw-r--r--widgets/disk.go46
-rw-r--r--widgets/help.go11
-rw-r--r--widgets/mem.go22
-rw-r--r--widgets/net.go56
-rw-r--r--widgets/proc.go175
-rw-r--r--widgets/proc_linux.go2
-rw-r--r--widgets/statusbar.go14
-rw-r--r--widgets/temp.go46
11 files changed, 245 insertions, 243 deletions
diff --git a/widgets/battery.go b/widgets/battery.go
index 4e813e7..7167668 100644
--- a/widgets/battery.go
+++ b/widgets/battery.go
@@ -62,7 +62,7 @@ func (b *BatteryWidget) EnableMetric() {
}
}
-func makeId(i int) string {
+func makeID(i int) string {
return "Batt" + strconv.Itoa(i)
}
@@ -70,7 +70,7 @@ func (b *BatteryWidget) Scale(i int) {
b.LineGraph.HorizontalScale = i
}
-func (self *BatteryWidget) update() {
+func (b *BatteryWidget) update() {
batteries, err := battery.GetAll()
if err != nil {
switch errt := err.(type) {
@@ -94,13 +94,13 @@ func (self *BatteryWidget) update() {
}
}
for i, battery := range batteries {
- id := makeId(i)
+ id := makeID(i)
perc := battery.Current / battery.Full
percentFull := math.Abs(perc) * 100.0
- self.Data[id] = append(self.Data[id], percentFull)
- self.Labels[id] = fmt.Sprintf("%3.0f%% %.0f/%.0f", percentFull, math.Abs(battery.Current), math.Abs(battery.Full))
- if self.metric != nil {
- self.metric[i].Set(perc)
+ b.Data[id] = append(b.Data[id], percentFull)
+ b.Labels[id] = fmt.Sprintf("%3.0f%% %.0f/%.0f", percentFull, math.Abs(battery.Current), math.Abs(battery.Full))
+ if b.metric != nil {
+ b.metric[i].Set(perc)
}
}
}
diff --git a/widgets/batterygauge.go b/widgets/batterygauge.go
index a379a84..45fec1c 100644
--- a/widgets/batterygauge.go
+++ b/widgets/batterygauge.go
@@ -9,16 +9,16 @@ import (
"github.com/distatus/battery"
"github.com/prometheus/client_golang/prometheus"
- . "github.com/xxxserxxx/gotop/v4/termui"
+ "github.com/xxxserxxx/gotop/v4/termui"
)
type BatteryGauge struct {
- *Gauge
+ *termui.Gauge
metric prometheus.Gauge
}
func NewBatteryGauge() *BatteryGauge {
- self := &BatteryGauge{Gauge: NewGauge()}
+ self := &BatteryGauge{Gauge: termui.NewGauge()}
self.Title = " Power Level "
self.update()
@@ -56,7 +56,7 @@ func (b *BatteryGauge) EnableMetric() {
}
}
-func (self *BatteryGauge) update() {
+func (b *BatteryGauge) update() {
bats, err := battery.GetAll()
if err != nil {
log.Printf("error setting up batteries: %v", err)
@@ -78,9 +78,9 @@ func (self *BatteryGauge) update() {
}
tn := (mx - cu) / rate
d, _ := time.ParseDuration(fmt.Sprintf("%fh", tn))
- self.Percent = int((cu / mx) * 100.0)
- self.Label = fmt.Sprintf(charging, self.Percent, d.Truncate(time.Minute))
- if self.metric != nil {
- self.metric.Set(cu / mx)
+ b.Percent = int((cu / mx) * 100.0)
+ b.Label = fmt.Sprintf(charging, b.Percent, d.Truncate(time.Minute))
+ if b.metric != nil {
+ b.metric.Set(cu / mx)
}
}
diff --git a/widgets/cpu.go b/widgets/cpu.go
index c48273e..03346bf 100644
--- a/widgets/cpu.go
+++ b/widgets/cpu.go
@@ -12,11 +12,11 @@ import (
ui "github.com/xxxserxxx/gotop/v4/termui"
)
-type CpuWidget struct {
+type CPUWidget struct {
*ui.LineGraph
- CpuCount int
+ CPUCount int
ShowAverageLoad bool
- ShowPerCpuLoad bool
+ ShowPerCPULoad bool
updateInterval time.Duration
updateLock sync.Mutex
metric map[string]prometheus.Gauge
@@ -24,20 +24,20 @@ type CpuWidget struct {
var cpuLabels []string
-func NewCpuWidget(updateInterval time.Duration, horizontalScale int, showAverageLoad bool, showPerCpuLoad bool) *CpuWidget {
- self := &CpuWidget{
+func NewCPUWidget(updateInterval time.Duration, horizontalScale int, showAverageLoad bool, showPerCPULoad bool) *CPUWidget {
+ self := &CPUWidget{
LineGraph: ui.NewLineGraph(),
- CpuCount: len(cpuLabels),
+ CPUCount: len(cpuLabels),
updateInterval: updateInterval,
ShowAverageLoad: showAverageLoad,
- ShowPerCpuLoad: showPerCpuLoad,
+ ShowPerCPULoad: showPerCPULoad,
}
self.Title = " CPU Usage "
self.HorizontalScale = horizontalScale
- if !(self.ShowAverageLoad || self.ShowPerCpuLoad) {
- if self.CpuCount <= 8 {
- self.ShowPerCpuLoad = true
+ if !(self.ShowAverageLoad || self.ShowPerCPULoad) {
+ if self.CPUCount <= 8 {
+ self.ShowPerCPULoad = true
} else {
self.ShowAverageLoad = true
}
@@ -47,9 +47,9 @@ func NewCpuWidget(updateInterval time.Duration, horizontalScale int, showAverage
self.Data["AVRG"] = []float64{0}
}
- if self.ShowPerCpuLoad {
+ if self.ShowPerCPULoad {
cpus := make(map[string]int)
- devices.UpdateCPU(cpus, self.updateInterval, self.ShowPerCpuLoad)
+ devices.UpdateCPU(cpus, self.updateInterval, self.ShowPerCPULoad)
for k, v := range cpus {
self.Data[k] = []float64{float64(v)}
}
@@ -66,17 +66,17 @@ func NewCpuWidget(updateInterval time.Duration, horizontalScale int, showAverage
return self
}
-func (self *CpuWidget) EnableMetric() {
- if self.ShowAverageLoad {
- self.metric = make(map[string]prometheus.Gauge)
- self.metric["AVRG"] = prometheus.NewGauge(prometheus.GaugeOpts{
+func (cpu *CPUWidget) EnableMetric() {
+ if cpu.ShowAverageLoad {
+ cpu.metric = make(map[string]prometheus.Gauge)
+ cpu.metric["AVRG"] = prometheus.NewGauge(prometheus.GaugeOpts{
Subsystem: "cpu",
Name: "avg",
})
} else {
cpus := make(map[string]int)
- devices.UpdateCPU(cpus, self.updateInterval, self.ShowPerCpuLoad)
- self.metric = make(map[string]prometheus.Gauge)
+ devices.UpdateCPU(cpus, cpu.updateInterval, cpu.ShowPerCPULoad)
+ cpu.metric = make(map[string]prometheus.Gauge)
for key, perc := range cpus {
gauge := prometheus.NewGauge(prometheus.GaugeOpts{
Namespace: "gotop",
@@ -85,53 +85,53 @@ func (self *CpuWidget) EnableMetric() {
})
gauge.Set(float64(perc))
prometheus.MustRegister(gauge)
- self.metric[key] = gauge
+ cpu.metric[key] = gauge
}
}
}
-func (b *CpuWidget) Scale(i int) {
- b.LineGraph.HorizontalScale = i
+func (cpu *CPUWidget) Scale(i int) {
+ cpu.LineGraph.HorizontalScale = i
}
-func (self *CpuWidget) update() {
- if self.ShowAverageLoad {
+func (cpu *CPUWidget) update() {
+ if cpu.ShowAverageLoad {
go func() {
cpus := make(map[string]int)
- devices.UpdateCPU(cpus, self.updateInterval, false)
- self.Lock()
- defer self.Unlock()
- self.updateLock.Lock()
- defer self.updateLock.Unlock()
+ devices.UpdateCPU(cpus, cpu.updateInterval, false)
+ cpu.Lock()
+ defer cpu.Unlock()
+ cpu.updateLock.Lock()
+ defer cpu.updateLock.Unlock()
var val float64
for _, v := range cpus {
val = float64(v)
break
}
- self.Data["AVRG"] = append(self.Data["AVRG"], val)
- self.Labels["AVRG"] = fmt.Sprintf("%3.0f%%", val)
- if self.metric != nil {
- self.metric["AVRG"].Set(val)
+ cpu.Data["AVRG"] = append(cpu.Data["AVRG"], val)
+ cpu.Labels["AVRG"] = fmt.Sprintf("%3.0f%%", val)
+ if cpu.metric != nil {
+ cpu.metric["AVRG"].Set(val)
}
}()
}
- if self.ShowPerCpuLoad {
+ if cpu.ShowPerCPULoad {
go func() {
cpus := make(map[string]int)
- devices.UpdateCPU(cpus, self.updateInterval, true)
- self.Lock()
- defer self.Unlock()
- self.updateLock.Lock()
- defer self.updateLock.Unlock()
+ devices.UpdateCPU(cpus, cpu.updateInterval, true)
+ cpu.Lock()
+ defer cpu.Unlock()
+ cpu.updateLock.Lock()
+ defer cpu.updateLock.Unlock()
for key, percent := range cpus {
- self.Data[key] = append(self.Data[key], float64(percent))
- self.Labels[key] = fmt.Sprintf("%d%%", percent)
- if self.metric != nil {
- if self.metric[key] == nil {
+ cpu.Data[key] = append(cpu.Data[key], float64(percent))
+ cpu.Labels[key] = fmt.Sprintf("%d%%", percent)
+ if cpu.metric != nil {
+ if cpu.metric[key] == nil {
log.Printf("no metrics for %s", key)
} else {
- self.metric[key].Set(float64(percent))
+ cpu.metric[key].Set(float64(percent))
}
}
}
diff --git a/widgets/disk.go b/widgets/disk.go
index 54f4a33..fb77ace 100644
--- a/widgets/disk.go
+++ b/widgets/disk.go
@@ -64,9 +64,9 @@ func NewDiskWidget() *DiskWidget {
return self
}
-func (self *DiskWidget) EnableMetric() {
- self.metric = make(map[string]prometheus.Gauge)
- for key, part := range self.Partitions {
+func (disk *DiskWidget) EnableMetric() {
+ disk.metric = make(map[string]prometheus.Gauge)
+ for key, part := range disk.Partitions {
gauge := prometheus.NewGauge(prometheus.GaugeOpts{
Namespace: "gotop",
Subsystem: "disk",
@@ -75,11 +75,11 @@ func (self *DiskWidget) EnableMetric() {
})
gauge.Set(float64(part.UsedPercent) / 100.0)
prometheus.MustRegister(gauge)
- self.metric[key] = gauge
+ disk.metric[key] = gauge
}
}
-func (self *DiskWidget) update() {
+func (disk *DiskWidget) update() {
partitions, err := psDisk.Partitions(false)
if err != nil {
log.Printf("failed to get disk partitions from gopsutil: %v", err)
@@ -97,8 +97,8 @@ func (self *DiskWidget) update() {
continue
}
// check if partition doesn't already exist in our list
- if _, ok := self.Partitions[partition.Device]; !ok {
- self.Partitions[partition.Device] = &Partition{
+ if _, ok := disk.Partitions[partition.Device]; !ok {
+ disk.Partitions[partition.Device] = &Partition{
Device: partition.Device,
MountPoint: partition.Mountpoint,
}
@@ -107,7 +107,7 @@ func (self *DiskWidget) update() {
// delete a partition if it no longer exists
toDelete := []string{}
- for device := range self.Partitions {
+ for device := range disk.Partitions {
exists := false
for _, partition := range partitions {
if device == partition.Device {
@@ -120,11 +120,11 @@ func (self *DiskWidget) update() {
}
}
for _, device := range toDelete {
- delete(self.Partitions, device)
+ delete(disk.Partitions, device)
}
// updates partition info. We add 0.5 to all values to make sure the truncation rounds
- for _, partition := range self.Partitions {
+ for _, partition := range disk.Partitions {
usage, err := psDisk.Usage(partition.MountPoint)
if err != nil {
log.Printf("failed to get partition usage statistics from gopsutil: %v. partition: %v", err, partition)
@@ -160,27 +160,27 @@ func (self *DiskWidget) update() {
// converts self.Partitions into self.Rows which is a [][]String
sortedPartitions := []string{}
- for seriesName := range self.Partitions {
+ for seriesName := range disk.Partitions {
sortedPartitions = append(sortedPartitions, seriesName)
}
sort.Strings(sortedPartitions)
- self.Rows = make([][]string, len(self.Partitions))
+ disk.Rows = make([][]string, len(disk.Partitions))
for i, key := range sortedPartitions {
- partition := self.Partitions[key]
- self.Rows[i] = make([]string, 6)
- self.Rows[i][0] = strings.Replace(strings.Replace(partition.Device, "/dev/", "", -1), "mapper/", "", -1)
- self.Rows[i][1] = partition.MountPoint
- self.Rows[i][2] = fmt.Sprintf("%d%%", partition.UsedPercent)
- self.Rows[i][3] = partition.Free
- self.Rows[i][4] = partition.BytesReadRecently
- self.Rows[i][5] = partition.BytesWrittenRecently
- if self.metric != nil {
- if self.metric[key] == nil {
+ partition := disk.Partitions[key]
+ disk.Rows[i] = make([]string, 6)
+ disk.Rows[i][0] = strings.Replace(strings.Replace(partition.Device, "/dev/", "", -1), "mapper/", "", -1)
+ disk.Rows[i][1] = partition.MountPoint
+ disk.Rows[i][2] = fmt.Sprintf("%d%%", partition.UsedPercent)
+ disk.Rows[i][3] = partition.Free
+ disk.Rows[i][4] = partition.BytesReadRecently
+ disk.Rows[i][5] = partition.BytesWrittenRecently
+ if disk.metric != nil {
+ if disk.metric[key] == nil {
log.Printf("ERROR: missing metric %s", key)
} else {
- self.metric[key].Set(float64(partition.UsedPercent) / 100.0)
+ disk.metric[key].Set(float64(partition.UsedPercent) / 100.0)
}
}
}
diff --git a/widgets/help.go b/widgets/help.go
index 012ca9d..a2c38e4 100644
--- a/widgets/help.go
+++ b/widgets/help.go
@@ -7,6 +7,7 @@ import (
ui "github.com/gizak/termui/v3"
)
+// KEYBINDS is the help text for the in-program shortcuts
const KEYBINDS = `
Quit: q or <C-c>
@@ -55,7 +56,7 @@ func NewHelpMenu() *HelpMenu {
}
}
-func (self *HelpMenu) Resize(termWidth, termHeight int) {
+func (help *HelpMenu) Resize(termWidth, termHeight int) {
textWidth := 53
for _, line := range strings.Split(KEYBINDS, "\n") {
if textWidth < len(line) {
@@ -66,17 +67,17 @@ func (self *HelpMenu) Resize(termWidth, termHeight int) {
x := (termWidth - textWidth) / 2
y := (termHeight - textHeight) / 2
- self.Block.SetRect(x, y, textWidth+x, textHeight+y)
+ help.Block.SetRect(x, y, textWidth+x, textHeight+y)
}
-func (self *HelpMenu) Draw(buf *ui.Buffer) {
- self.Block.Draw(buf)
+func (help *HelpMenu) Draw(buf *ui.Buffer) {
+ help.Block.Draw(buf)
for y, line := range strings.Split(KEYBINDS, "\n") {
for x, rune := range line {
buf.SetCell(
ui.NewCell(rune, ui.Theme.Default),
- image.Pt(self.Inner.Min.X+x, self.Inner.Min.Y+y-1),
+ image.Pt(help.Inner.Min.X+x, help.Inner.Min.Y+y-1),
)
}
}
diff --git a/widgets/mem.go b/widgets/mem.go
index 4bf8550..6b5d326 100644
--- a/widgets/mem.go
+++ b/widgets/mem.go
@@ -48,30 +48,30 @@ func NewMemWidget(updateInterval time.Duration, horizontalScale int) *MemWidget
return self
}
-func (b *MemWidget) EnableMetric() {
- b.metrics = make(map[string]prometheus.Gauge)
+func (mem *MemWidget) EnableMetric() {
+ mem.metrics = make(map[string]prometheus.Gauge)
mems := make(map[string]devices.MemoryInfo)
devices.UpdateMem(mems)
- for l, mem := range mems {
- b.metrics[l] = prometheus.NewGauge(prometheus.GaugeOpts{
+ for l, m := range mems {
+ mem.metrics[l] = prometheus.NewGauge(prometheus.GaugeOpts{
Namespace: "gotop",
Subsystem: "memory",
Name: l,
})
- b.metrics[l].Set(mem.UsedPercent)
- prometheus.MustRegister(b.metrics[l])
+ mem.metrics[l].Set(m.UsedPercent)
+ prometheus.MustRegister(mem.metrics[l])
}
}
-func (b *MemWidget) Scale(i int) {
- b.LineGraph.HorizontalScale = i
+func (mem *MemWidget) Scale(i int) {
+ mem.LineGraph.HorizontalScale = i
}
-func (self *MemWidget) renderMemInfo(line string, memoryInfo devices.MemoryInfo) {
- self.Data[line] = append(self.Data[line], memoryInfo.UsedPercent)
+func (mem *MemWidget) renderMemInfo(line string, memoryInfo devices.MemoryInfo) {
+ mem.Data[line] = append(mem.Data[line], memoryInfo.UsedPercent)
memoryTotalBytes, memoryTotalMagnitude := utils.ConvertBytes(memoryInfo.Total)
memoryUsedBytes, memoryUsedMagnitude := utils.ConvertBytes(memoryInfo.Used)
- self.Labels[line] = fmt.Sprintf("%3.0f%% %5.1f%s/%.0f%s",
+ mem.Labels[line] = fmt.Sprintf("%3.0f%% %5.1f%s/%.0f%s",
memoryInfo.UsedPercent,
memoryUsedBytes,
memoryUsedMagnitude,
diff --git a/widgets/net.go b/widgets/net.go
index f851454..9cd4211 100644
--- a/widgets/net.go
+++ b/widgets/net.go
@@ -14,8 +14,10 @@ import (
)
const (
- NET_INTERFACE_ALL = "all"
- NET_INTERFACE_VPN = "tun0"
+ // NetInterfaceAll enables all network interfaces
+ NetInterfaceAll = "all"
+ // NetInterfaceVpn is the VPN interface
+ NetInterfaceVpn = "tun0"
)
type NetWidget struct {
@@ -63,23 +65,23 @@ func NewNetWidget(netInterface string) *NetWidget {
return self
}
-func (b *NetWidget) EnableMetric() {
- b.recvMetric = prometheus.NewCounter(prometheus.CounterOpts{
+func (net *NetWidget) EnableMetric() {
+ net.recvMetric = prometheus.NewCounter(prometheus.CounterOpts{
Namespace: "gotop",
Subsystem: "net",
Name: "recv",
})
- prometheus.MustRegister(b.recvMetric)
+ prometheus.MustRegister(net.recvMetric)
- b.sentMetric = prometheus.NewCounter(prometheus.CounterOpts{
+ net.sentMetric = prometheus.NewCounter(prometheus.CounterOpts{
Namespace: "gotop",
Subsystem: "net",
Name: "sent",
})
- prometheus.MustRegister(b.sentMetric)
+ prometheus.MustRegister(net.sentMetric)
}
-func (self *NetWidget) update() {
+func (net *NetWidget) update() {
interfaces, err := psNet.IOCounters(true)
if err != nil {
log.Printf("failed to get network activity from gopsutil: %v", err)
@@ -90,15 +92,15 @@ func (self *NetWidget) update() {
var totalBytesSent uint64
interfaceMap := make(map[string]bool)
// Default behaviour
- interfaceMap[NET_INTERFACE_ALL] = true
- interfaceMap[NET_INTERFACE_VPN] = false
+ interfaceMap[NetInterfaceAll] = true
+ interfaceMap[NetInterfaceVpn] = false
// Build a map with wanted status for each interfaces.
- for _, iface := range self.NetInterface {
+ for _, iface := range net.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)
+ delete(interfaceMap, NetInterfaceAll)
interfaceMap[iface] = true
}
}
@@ -109,7 +111,7 @@ func (self *NetWidget) update() {
totalBytesSent += _interface.BytesSent
} else if ok { // Present but unwanted
continue
- } else if interfaceMap[NET_INTERFACE_ALL] { // Capture other
+ } else if interfaceMap[NetInterfaceAll] { // Capture other
totalBytesRecv += _interface.BytesRecv
totalBytesSent += _interface.BytesSent
}
@@ -118,9 +120,9 @@ func (self *NetWidget) update() {
var recentBytesRecv uint64
var recentBytesSent uint64
- if self.totalBytesRecv != 0 { // if this isn't the first update
- recentBytesRecv = totalBytesRecv - self.totalBytesRecv
- recentBytesSent = totalBytesSent - self.totalBytesSent
+ if net.totalBytesRecv != 0 { // if this isn't the first update
+ recentBytesRecv = totalBytesRecv - net.totalBytesRecv
+ recentBytesSent = totalBytesSent - net.totalBytesSent
if int(recentBytesRecv) < 0 {
log.Printf("error: negative value for recently received network data from gopsutil. recentBytesRecv: %v", recentBytesRecv)
@@ -133,20 +135,20 @@ 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 self.sentMetric != nil {
- self.sentMetric.Add(float64(recentBytesSent))
- self.recvMetric.Add(float64(recentBytesRecv))
+ net.Lines[0].Data = append(net.Lines[0].Data, int(recentBytesRecv))
+ net.Lines[1].Data = append(net.Lines[1].Data, int(recentBytesSent))
+ if net.sentMetric != nil {
+ net.sentMetric.Add(float64(recentBytesSent))
+ net.recvMetric.Add(float64(recentBytesRecv))
}
}
// used in later calls to update
- self.totalBytesRecv = totalBytesRecv
- self.totalBytesSent = totalBytesSent
+ net.totalBytesRecv = totalBytesRecv
+ net.totalBytesSent = totalBytesSent
rx, tx := "RX/s", "TX/s"
- if self.Mbps {
+ if net.Mbps {
rx, tx = "mbps", "mbps"
}
format := " %s: %9.1f %2s/s"
@@ -163,13 +165,13 @@ func (self *NetWidget) update() {
}
totalConverted, unitTotal := utils.ConvertBytes(total)
- if self.Mbps {
+ if net.Mbps {
recentConverted, unitRecent, format = float64(recent)*0.000008, "", " %s: %11.3f %2s"
} else {
recentConverted, unitRecent = utils.ConvertBytes(recent)
}
- self.Lines[i].Title1 = fmt.Sprintf(" Total %s: %5.1f %s", label, totalConverted, unitTotal)
- self.Lines[i].Title2 = fmt.Sprintf(format, rate, recentConverted, unitRecent)
+ net.Lines[i].Title1 = fmt.Sprintf(" Total %s: %5.1f %s", label, totalConverted, unitTotal)
+ net.Lines[i].Title2 = fmt.Sprintf(format, rate, recentConverted, unitRecent)
}
}
diff --git a/widgets/proc.go b/widgets/proc.go
index c691755..f47f646 100644
--- a/widgets/proc.go
+++ b/widgets/proc.go
@@ -17,14 +17,13 @@ import (
)
const (
- UP_ARROW = "▲"
- DOWN_ARROW = "▼"
+ _downArrow = "▼"
)
type ProcSortMethod string
const (
- ProcSortCpu ProcSortMethod = "c"
+ ProcSortCPU ProcSortMethod = "c"
ProcSortMem = "m"
ProcSortPid = "p"
)
@@ -33,7 +32,7 @@ type Proc struct {
Pid int
CommandName string
FullCommand string
- Cpu float64
+ CPU float64
Mem float64
}
@@ -58,7 +57,7 @@ func NewProcWidget() *ProcWidget {
Table: ui.NewTable(),
updateInterval: time.Second,
cpuCount: cpuCount,
- sortMethod: ProcSortCpu,
+ sortMethod: ProcSortCPU,
showGroupedProcs: true,
filter: "",
}
@@ -100,42 +99,42 @@ func NewProcWidget() *ProcWidget {
return self
}
-func (p *ProcWidget) EnableMetric() {
+func (proc *ProcWidget) EnableMetric() {
// There's (currently) no metric for this
}
-func (self *ProcWidget) SetEditingFilter(editing bool) {
- self.entry.SetEditing(editing)
+func (proc *ProcWidget) SetEditingFilter(editing bool) {
+ proc.entry.SetEditing(editing)
}
-func (self *ProcWidget) HandleEvent(e tui.Event) bool {
- return self.entry.HandleEvent(e)
+func (proc *ProcWidget) HandleEvent(e tui.Event) bool {
+ return proc.entry.HandleEvent(e)
}
-func (self *ProcWidget) SetRect(x1, y1, x2, y2 int) {
- self.Table.SetRect(x1, y1, x2, y2)
- self.entry.SetRect(x1+2, y2-1, x2-2, y2)
+func (proc *ProcWidget) SetRect(x1, y1, x2, y2 int) {
+ proc.Table.SetRect(x1, y1, x2, y2)
+ proc.entry.SetRect(x1+2, y2-1, x2-2, y2)
}
-func (self *ProcWidget) Draw(buf *tui.Buffer) {
- self.Table.Draw(buf)
- self.entry.Draw(buf)
+func (proc *ProcWidget) Draw(buf *tui.Buffer) {
+ proc.Table.Draw(buf)
+ proc.entry.Draw(buf)
}
-func (self *ProcWidget) filterProcs(procs []Proc) []Proc {
- if self.filter == "" {
+func (proc *ProcWidget) filterProcs(procs []Proc) []Proc {
+ if proc.filter == "" {
return procs
}
var filtered []Proc
- for _, proc := range procs {
- if strings.Contains(proc.FullCommand, self.filter) || strings.Contains(fmt.Sprintf("%d", proc.Pid), self.filter) {
- filtered = append(filtered, proc)
+ for _, p := range procs {
+ if strings.Contains(p.FullCommand, proc.filter) || strings.Contains(fmt.Sprintf("%d", p.Pid), proc.filter) {
+ filtered = append(filtered, p)
}
}
return filtered
}
-func (self *ProcWidget) update() {
+func (proc *ProcWidget) update() {
procs, err := getProcs()
if err != nil {
log.Printf("failed to retrieve processes: %v", err)
@@ -144,103 +143,103 @@ func (self *ProcWidget) update() {
// have to iterate over the entry number in order to modify the array in place
for i := range procs {
- procs[i].Cpu /= float64(self.cpuCount)
+ procs[i].CPU /= float64(proc.cpuCount)
}
- procs = self.filterProcs(procs)
- self.ungroupedProcs = procs
- self.groupedProcs = groupProcs(procs)
+ procs = proc.filterProcs(procs)
+ proc.ungroupedProcs = procs
+ proc.groupedProcs = groupProcs(procs)
- self.sortProcs()
- self.convertProcsToTableRows()
+ proc.sortProcs()
+ proc.convertProcsToTableRows()
}
// sortProcs sorts either the grouped or ungrouped []Process based on the sortMethod.
// Called with every update, when the sort method is changed, and when processes are grouped and ungrouped.
-func (self *ProcWidget) sortProcs() {
- self.Header = []string{"Count", "Command", "CPU%", "Mem%"}
+func (proc *ProcWidget) sortProcs() {
+ proc.Header = []string{"Count", "Command", "CPU%", "Mem%"}
- if !self.showGroupedProcs {
- self.Header[0] = "PID"
+ if !proc.showGroupedProcs {
+ proc.Header[0] = "PID"
}
var procs *[]Proc
- if self.showGroupedProcs {
- procs = &self.groupedProcs
+ if proc.showGroupedProcs {
+ procs = &proc.groupedProcs
} else {
- procs = &self.ungroupedProcs
+ procs = &proc.ungroupedProcs
}
- switch self.sortMethod {
- case ProcSortCpu:
- sort.Sort(sort.Reverse(SortProcsByCpu(*procs)))
- self.Header[2] += DOWN_ARROW
+ switch proc.sortMethod {
+ case ProcSortCPU:
+ sort.Sort(sort.Reverse(SortProcsByCPU(*procs)))
+ proc.Header[2] += _downArrow
case ProcSortPid:
- if self.showGroupedProcs {
+ if proc.showGroupedProcs {
sort.Sort(sort.Reverse(SortProcsByPid(*procs)))
} else {
sort.Sort(SortProcsByPid(*procs))
}
- self.Header[0] += DOWN_ARROW
+ proc.Header[0] += _downArrow
case ProcSortMem:
sort.Sort(sort.Reverse(SortProcsByMem(*procs)))
- self.Header[3] += DOWN_ARROW
+ proc.Header[3] += _downArrow
}
}
// convertProcsToTableRows converts a []Proc to a [][]string and sets it to the table Rows
-func (self *ProcWidget) convertProcsToTableRows() {
+func (proc *ProcWidget) convertProcsToTableRows() {
var procs *[]Proc
- if self.showGroupedProcs {
- procs = &self.groupedProcs
+ if proc.showGroupedProcs {
+ procs = &proc.groupedProcs
} else {
- procs = &self.ungroupedProcs
+ procs = &proc.ungroupedProcs
}
strings := make([][]string, len(*procs))
for i := range *procs {
strings[i] = make([]string, 4)
strings[i][0] = strconv.Itoa(int((*procs)[i].Pid))
- if self.showGroupedProcs {
+ if proc.showGroupedProcs {
strings[i][1] = (*procs)[i].CommandName
} else {
strings[i][1] = (*procs)[i].FullCommand
}
- strings[i][2] = fmt.Sprintf("%4s", strconv.FormatFloat((*procs)[i].Cpu, 'f', 1, 64))
+ strings[i][2] = fmt.Sprintf("%4s", strconv.FormatFloat((*procs)[i].CPU, 'f', 1, 64))
strings[i][3] = fmt.Sprintf("%4s", strconv.FormatFloat(float64((*procs)[i].Mem), 'f', 1, 64))
}
- self.Rows = strings
+ proc.Rows = strings
}
-func (self *ProcWidget) ChangeProcSortMethod(method ProcSortMethod) {
- if self.sortMethod != method {
- self.sortMethod = method
- self.ScrollTop()
- self.sortProcs()
- self.convertProcsToTableRows()
+func (proc *ProcWidget) ChangeProcSortMethod(method ProcSortMethod) {
+ if proc.sortMethod != method {
+ proc.sortMethod = method
+ proc.ScrollTop()
+ proc.sortProcs()
+ proc.convertProcsToTableRows()
}
}
-func (self *ProcWidget) ToggleShowingGroupedProcs() {
- self.showGroupedProcs = !self.showGroupedProcs
- if self.showGroupedProcs {
- self.UniqueCol = 1
+func (proc *ProcWidget) ToggleShowingGroupedProcs() {
+ proc.showGroupedProcs = !proc.showGroupedProcs
+ if proc.showGroupedProcs {
+ proc.UniqueCol = 1
} else {
- self.UniqueCol = 0
+ proc.UniqueCol = 0
}
- self.ScrollTop()
- self.sortProcs()
- self.convertProcsToTableRows()
+ proc.ScrollTop()
+ proc.sortProcs()
+ proc.convertProcsToTableRows()
}
// KillProc kills a process or group of processes depending on if we're
// displaying the processes grouped or not.
-func (self *ProcWidget) KillProc(sigName string) {
- self.SelectedItem = ""
+func (proc *ProcWidget) KillProc(sigName string) {
+ proc.SelectedItem = ""
command := "kill"
- if self.UniqueCol == 1 {
+ if proc.UniqueCol == 1 {
command = "pkill"
}
- cmd := exec.Command(command, "--signal", sigName, self.Rows[self.SelectedRow][self.UniqueCol])
+ cmd := exec.Command(command, "--signal", sigName, proc.Rows[proc.SelectedRow][proc.UniqueCol])
cmd.Start()
cmd.Wait()
}
@@ -257,7 +256,7 @@ func groupProcs(procs []Proc) []Proc {
val.Pid + 1,
val.CommandName,
"",
- val.Cpu + proc.Cpu,
+ val.CPU + proc.CPU,
val.Mem + proc.Mem,
}
} else {
@@ -265,7 +264,7 @@ func groupProcs(procs []Proc) []Proc {
1,
proc.CommandName,
"",
- proc.Cpu,
+ proc.CPU,
proc.Mem,
}
}
@@ -283,53 +282,53 @@ func groupProcs(procs []Proc) []Proc {
// []Proc Sorting //////////////////////////////////////////////////////////////
-type SortProcsByCpu []Proc
+type SortProcsByCPU []Proc
// Len implements Sort interface
-func (self SortProcsByCpu) Len() int {
- return len(self)
+func (procs SortProcsByCPU) Len() int {
+ return len(procs)
}
// Swap implements Sort interface
-func (self SortProcsByCpu) Swap(i, j int) {
- self[i], self[j] = self[j], self[i]
+func (procs SortProcsByCPU) Swap(i, j int) {
+ procs[i], procs[j] = procs[j], procs[i]
}
// Less implements Sort interface
-func (self SortProcsByCpu) Less(i, j int) bool {
- return self[i].Cpu < self[j].Cpu
+func (procs SortProcsByCPU) Less(i, j int) bool {
+ return procs[i].CPU < procs[j].CPU
}
type SortProcsByPid []Proc
// Len implements Sort interface
-func (self SortProcsByPid) Len() int {
- return len(self)
+func (procs SortProcsByPid) Len() int {
+ return len(procs)
}
// Swap implements Sort interface
-func (self SortProcsByPid) Swap(i, j int) {
- self[i], self[j] = self[j], self[i]
+func (procs SortProcsByPid) Swap(i, j int) {
+ procs[i], procs[j] = procs[j], procs[i]
}
// Less implements Sort interface
-func (self SortProcsByPid) Less(i, j int) bool {
- return self[i].Pid < self[j].Pid
+func (procs SortProcsByPid) Less(i, j int) bool {
+ return procs[i].Pid < procs[j].Pid
}
type SortProcsByMem []Proc
// Len implements Sort interface
-func (self SortProcsByMem) Len() int {
- return len(self)
+func (procs SortProcsByMem) Len() int {
+ return len(procs)
}
// Swap implements Sort interface
-func (self SortProcsByMem) Swap(i, j int) {
- self[i], self[j] = self[j], self[i]
+func (procs SortProcsByMem) Swap(i, j int) {
+ procs[i], procs[j] = procs[j], procs[i]
}
// Less implements Sort interface
-func (self SortProcsByMem) Less(i, j int) bool {
- return self[i].Mem < self[j].Mem
+func (procs SortProcsByMem) Less(i, j int) bool {
+ return procs[i].Mem < procs[j].Mem
}
diff --git a/widgets/proc_linux.go b/widgets/proc_linux.go
index 0045234..164319d 100644
--- a/widgets/proc_linux.go
+++ b/widgets/proc_linux.go
@@ -35,7 +35,7 @@ func getProcs() ([]Proc, error) {
Pid: pid,
CommandName: strings.TrimSpace(line[11:61]),
FullCommand: line[74:],
- Cpu: cpu,
+ CPU: cpu,
Mem: mem,
}
procs = append(procs, proc)
diff --git a/widgets/statusbar.go b/widgets/statusbar.go
index efa3dbf..2242414 100644
--- a/widgets/statusbar.go
+++ b/widgets/statusbar.go
@@ -19,8 +19,8 @@ func NewStatusBar() *StatusBar {
return self
}
-func (self *StatusBar) Draw(buf *ui.Buffer) {
- self.Block.Draw(buf)
+func (sb *StatusBar) Draw(buf *ui.Buffer) {
+ sb.Block.Draw(buf)
hostname, err := os.Hostname()
if err != nil {
@@ -30,7 +30,7 @@ func (self *StatusBar) Draw(buf *ui.Buffer) {
buf.SetString(
hostname,
ui.Theme.Default,
- image.Pt(self.Inner.Min.X, self.Inner.Min.Y+(self.Inner.Dy()/2)),
+ image.Pt(sb.Inner.Min.X, sb.Inner.Min.Y+(sb.Inner.Dy()/2)),
)
currentTime := time.Now()
@@ -39,8 +39,8 @@ func (self *StatusBar) Draw(buf *ui.Buffer) {
formattedTime,
ui.Theme.Default,
image.Pt(
- self.Inner.Min.X+(self.Inner.Dx()/2)-len(formattedTime)/2,
- self.Inner.Min.Y+(self.Inner.Dy()/2),
+ sb.Inner.Min.X+(sb.Inner.Dx()/2)-len(formattedTime)/2,
+ sb.Inner.Min.Y+(sb.Inner.Dy()/2),
),
)
@@ -48,8 +48,8 @@ func (self *StatusBar) Draw(buf *ui.Buffer) {
"gotop",
ui.Theme.Default,
image.Pt(
- self.Inner.Max.X-6,
- self.Inner.Min.Y+(self.Inner.Dy()/2),
+ sb.Inner.Max.X-6,
+ sb.Inner.Min.Y+(sb.Inner.Dy()/2),
),
)
}
diff --git a/widgets/temp.go b/widgets/temp.go
index 2f1f2cb..05ad3c1 100644
--- a/widgets/temp.go
+++ b/widgets/temp.go
@@ -68,9 +68,9 @@ func NewTempWidget(tempScale TempScale, filter []string) *TempWidget {
return self
}
-func (self *TempWidget) EnableMetric() {
- self.tempsMetric = make(map[string]prometheus.Gauge)
- for k, v := range self.Data {
+func (temp *TempWidget) EnableMetric() {
+ temp.tempsMetric = make(map[string]prometheus.Gauge)
+ for k, v := range temp.Data {
gauge := prometheus.NewGauge(prometheus.GaugeOpts{
Namespace: "gotop",
Subsystem: "temp",
@@ -78,58 +78,58 @@ func (self *TempWidget) EnableMetric() {
})
gauge.Set(float64(v))
prometheus.MustRegister(gauge)
- self.tempsMetric[k] = gauge