Replace custom widget locking with termui's locking
This commit is contained in:
parent
96535ccadd
commit
903d33178d
61
main.go
61
main.go
|
@ -10,7 +10,6 @@ import (
|
|||
"path/filepath"
|
||||
"sort"
|
||||
"strconv"
|
||||
"sync"
|
||||
"syscall"
|
||||
"time"
|
||||
|
||||
|
@ -48,8 +47,6 @@ var (
|
|||
battery = false
|
||||
statusbar = false
|
||||
|
||||
renderLock sync.RWMutex
|
||||
|
||||
cpu *w.CpuWidget
|
||||
batt *w.BatteryWidget
|
||||
mem *w.MemWidget
|
||||
|
@ -275,29 +272,23 @@ func setWidgetColors() {
|
|||
}
|
||||
|
||||
func initWidgets() {
|
||||
cpu = w.NewCpuWidget(&renderLock, updateInterval, graphHorizontalScale, averageLoad, percpuLoad)
|
||||
mem = w.NewMemWidget(&renderLock, updateInterval, graphHorizontalScale)
|
||||
proc = w.NewProcWidget(&renderLock)
|
||||
cpu = w.NewCpuWidget(updateInterval, graphHorizontalScale, averageLoad, percpuLoad)
|
||||
mem = w.NewMemWidget(updateInterval, graphHorizontalScale)
|
||||
proc = w.NewProcWidget()
|
||||
help = w.NewHelpMenu()
|
||||
if !minimalMode {
|
||||
if battery {
|
||||
batt = w.NewBatteryWidget(&renderLock, graphHorizontalScale)
|
||||
batt = w.NewBatteryWidget(graphHorizontalScale)
|
||||
}
|
||||
net = w.NewNetWidget(&renderLock)
|
||||
disk = w.NewDiskWidget(&renderLock)
|
||||
temp = w.NewTempWidget(&renderLock, tempScale)
|
||||
net = w.NewNetWidget()
|
||||
disk = w.NewDiskWidget()
|
||||
temp = w.NewTempWidget(tempScale)
|
||||
}
|
||||
if statusbar {
|
||||
bar = w.NewStatusBar()
|
||||
}
|
||||
}
|
||||
|
||||
func render(drawable ...ui.Drawable) {
|
||||
renderLock.Lock()
|
||||
ui.Render(drawable...)
|
||||
renderLock.Unlock()
|
||||
}
|
||||
|
||||
func eventLoop() {
|
||||
drawTicker := time.NewTicker(updateInterval).C
|
||||
|
||||
|
@ -315,7 +306,7 @@ func eventLoop() {
|
|||
return
|
||||
case <-drawTicker:
|
||||
if !helpVisible {
|
||||
render(grid)
|
||||
ui.Render(grid)
|
||||
}
|
||||
case e := <-uiEvents:
|
||||
switch e.ID {
|
||||
|
@ -343,74 +334,74 @@ func eventLoop() {
|
|||
ui.Render(help)
|
||||
case "<Escape>":
|
||||
helpVisible = false
|
||||
render(grid)
|
||||
ui.Render(grid)
|
||||
case "<Resize>":
|
||||
ui.Render(help)
|
||||
}
|
||||
} else {
|
||||
switch e.ID {
|
||||
case "?":
|
||||
render(grid)
|
||||
ui.Render(grid)
|
||||
case "h":
|
||||
graphHorizontalScale += graphHorizontalScaleDelta
|
||||
cpu.HorizontalScale = graphHorizontalScale
|
||||
mem.HorizontalScale = graphHorizontalScale
|
||||
render(cpu, mem)
|
||||
ui.Render(cpu, mem)
|
||||
case "l":
|
||||
if graphHorizontalScale > graphHorizontalScaleDelta {
|
||||
graphHorizontalScale -= graphHorizontalScaleDelta
|
||||
cpu.HorizontalScale = graphHorizontalScale
|
||||
mem.HorizontalScale = graphHorizontalScale
|
||||
render(cpu, mem)
|
||||
ui.Render(cpu, mem)
|
||||
}
|
||||
case "<Resize>":
|
||||
render(grid)
|
||||
ui.Render(grid)
|
||||
if statusbar {
|
||||
render(bar)
|
||||
ui.Render(bar)
|
||||
}
|
||||
case "<MouseLeft>":
|
||||
payload := e.Payload.(ui.Mouse)
|
||||
proc.HandleClick(payload.X, payload.Y)
|
||||
render(proc)
|
||||
ui.Render(proc)
|
||||
case "k", "<Up>", "<MouseWheelUp>":
|
||||
proc.ScrollUp()
|
||||
render(proc)
|
||||
ui.Render(proc)
|
||||
case "j", "<Down>", "<MouseWheelDown>":
|
||||
proc.ScrollDown()
|
||||
render(proc)
|
||||
ui.Render(proc)
|
||||
case "<Home>":
|
||||
proc.ScrollTop()
|
||||
render(proc)
|
||||
ui.Render(proc)
|
||||
case "g":
|
||||
if previousKey == "g" {
|
||||
proc.ScrollTop()
|
||||
render(proc)
|
||||
ui.Render(proc)
|
||||
}
|
||||
case "G", "<End>":
|
||||
proc.ScrollBottom()
|
||||
render(proc)
|
||||
ui.Render(proc)
|
||||
case "<C-d>":
|
||||
proc.ScrollHalfPageDown()
|
||||
render(proc)
|
||||
ui.Render(proc)
|
||||
case "<C-u>":
|
||||
proc.ScrollHalfPageUp()
|
||||
render(proc)
|
||||
ui.Render(proc)
|
||||
case "<C-f>":
|
||||
proc.ScrollPageDown()
|
||||
render(proc)
|
||||
ui.Render(proc)
|
||||
case "<C-b>":
|
||||
proc.ScrollPageUp()
|
||||
render(proc)
|
||||
ui.Render(proc)
|
||||
case "d":
|
||||
if previousKey == "d" {
|
||||
proc.KillProc()
|
||||
}
|
||||
case "<Tab>":
|
||||
proc.ToggleShowingGroupedProcs()
|
||||
render(proc)
|
||||
ui.Render(proc)
|
||||
case "m", "c", "p":
|
||||
proc.ChangeProcSortMethod(w.ProcSortMethod(e.ID))
|
||||
render(proc)
|
||||
ui.Render(proc)
|
||||
}
|
||||
|
||||
if previousKey == e.ID {
|
||||
|
|
|
@ -5,7 +5,6 @@ import (
|
|||
"log"
|
||||
"math"
|
||||
"strconv"
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
"github.com/distatus/battery"
|
||||
|
@ -18,7 +17,7 @@ type BatteryWidget struct {
|
|||
updateInterval time.Duration
|
||||
}
|
||||
|
||||
func NewBatteryWidget(renderLock *sync.RWMutex, horizontalScale int) *BatteryWidget {
|
||||
func NewBatteryWidget(horizontalScale int) *BatteryWidget {
|
||||
self := &BatteryWidget{
|
||||
LineGraph: ui.NewLineGraph(),
|
||||
updateInterval: time.Minute,
|
||||
|
@ -33,9 +32,9 @@ func NewBatteryWidget(renderLock *sync.RWMutex, horizontalScale int) *BatteryWid
|
|||
|
||||
go func() {
|
||||
for range time.NewTicker(self.updateInterval).C {
|
||||
renderLock.RLock()
|
||||
self.Lock()
|
||||
self.update()
|
||||
renderLock.RUnlock()
|
||||
self.Unlock()
|
||||
}
|
||||
}()
|
||||
|
||||
|
|
|
@ -18,11 +18,10 @@ type CpuWidget struct {
|
|||
ShowPerCpuLoad bool
|
||||
updateInterval time.Duration
|
||||
formatString string
|
||||
renderLock *sync.RWMutex
|
||||
updateLock sync.Mutex
|
||||
}
|
||||
|
||||
func NewCpuWidget(renderLock *sync.RWMutex, updateInterval time.Duration, horizontalScale int, showAverageLoad bool, showPerCpuLoad bool) *CpuWidget {
|
||||
func NewCpuWidget(updateInterval time.Duration, horizontalScale int, showAverageLoad bool, showPerCpuLoad bool) *CpuWidget {
|
||||
cpuCount, err := psCpu.Counts(false)
|
||||
if err != nil {
|
||||
log.Printf("failed to get CPU count from gopsutil: %v", err)
|
||||
|
@ -38,7 +37,6 @@ func NewCpuWidget(renderLock *sync.RWMutex, updateInterval time.Duration, horizo
|
|||
ShowAverageLoad: showAverageLoad,
|
||||
ShowPerCpuLoad: showPerCpuLoad,
|
||||
formatString: formatString,
|
||||
renderLock: renderLock,
|
||||
}
|
||||
self.Title = " CPU Usage "
|
||||
self.HorizontalScale = horizontalScale
|
||||
|
@ -80,8 +78,8 @@ func (self *CpuWidget) update() {
|
|||
if err != nil {
|
||||
log.Printf("failed to get average CPU usage percent from gopsutil: %v. self.updateInterval: %v. percpu: %v", err, self.updateInterval, false)
|
||||
} else {
|
||||
self.renderLock.RLock()
|
||||
defer self.renderLock.RUnlock()
|
||||
self.Lock()
|
||||
defer self.Unlock()
|
||||
self.updateLock.Lock()
|
||||
defer self.updateLock.Unlock()
|
||||
self.Data["AVRG"] = append(self.Data["AVRG"], percent[0])
|
||||
|
@ -99,8 +97,8 @@ func (self *CpuWidget) update() {
|
|||
if len(percents) != int(self.CpuCount) {
|
||||
log.Printf("error: number of CPU usage percents from gopsutil doesn't match CPU count. percents: %v. self.Count: %v", percents, self.CpuCount)
|
||||
} else {
|
||||
self.renderLock.RLock()
|
||||
defer self.renderLock.RUnlock()
|
||||
self.Lock()
|
||||
defer self.Unlock()
|
||||
self.updateLock.Lock()
|
||||
defer self.updateLock.Unlock()
|
||||
for i, percent := range percents {
|
||||
|
|
|
@ -5,7 +5,6 @@ import (
|
|||
"log"
|
||||
"sort"
|
||||
"strings"
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
psDisk "github.com/shirou/gopsutil/disk"
|
||||
|
@ -31,7 +30,7 @@ type DiskWidget struct {
|
|||
Partitions map[string]*Partition
|
||||
}
|
||||
|
||||
func NewDiskWidget(renderLock *sync.RWMutex) *DiskWidget {
|
||||
func NewDiskWidget() *DiskWidget {
|
||||
self := &DiskWidget{
|
||||
Table: ui.NewTable(),
|
||||
updateInterval: time.Second,
|
||||
|
@ -52,9 +51,9 @@ func NewDiskWidget(renderLock *sync.RWMutex) *DiskWidget {
|
|||
|
||||
go func() {
|
||||
for range time.NewTicker(self.updateInterval).C {
|
||||
renderLock.RLock()
|
||||
self.Lock()
|
||||
self.update()
|
||||
renderLock.RUnlock()
|
||||
self.Unlock()
|
||||
}
|
||||
}()
|
||||
|
||||
|
|
|
@ -3,7 +3,6 @@ package widgets
|
|||
import (
|
||||
"fmt"
|
||||
"log"
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
psMem "github.com/shirou/gopsutil/mem"
|
||||
|
@ -17,7 +16,7 @@ type MemWidget struct {
|
|||
updateInterval time.Duration
|
||||
}
|
||||
|
||||
func NewMemWidget(renderLock *sync.RWMutex, updateInterval time.Duration, horizontalScale int) *MemWidget {
|
||||
func NewMemWidget(updateInterval time.Duration, horizontalScale int) *MemWidget {
|
||||
self := &MemWidget{
|
||||
LineGraph: ui.NewLineGraph(),
|
||||
updateInterval: updateInterval,
|
||||
|
@ -31,9 +30,9 @@ func NewMemWidget(renderLock *sync.RWMutex, updateInterval time.Duration, horizo
|
|||
|
||||
go func() {
|
||||
for range time.NewTicker(self.updateInterval).C {
|
||||
renderLock.RLock()
|
||||
self.Lock()
|
||||
self.update()
|
||||
renderLock.RUnlock()
|
||||
self.Unlock()
|
||||
}
|
||||
}()
|
||||
|
||||
|
|
|
@ -3,7 +3,6 @@ package widgets
|
|||
import (
|
||||
"fmt"
|
||||
"log"
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
psNet "github.com/shirou/gopsutil/net"
|
||||
|
@ -21,7 +20,7 @@ type NetWidget struct {
|
|||
totalBytesSent uint64
|
||||
}
|
||||
|
||||
func NewNetWidget(renderLock *sync.RWMutex) *NetWidget {
|
||||
func NewNetWidget() *NetWidget {
|
||||
recvSparkline := ui.NewSparkline()
|
||||
recvSparkline.Data = []int{}
|
||||
|
||||
|
@ -39,9 +38,9 @@ func NewNetWidget(renderLock *sync.RWMutex) *NetWidget {
|
|||
|
||||
go func() {
|
||||
for range time.NewTicker(self.updateInterval).C {
|
||||
renderLock.RLock()
|
||||
self.Lock()
|
||||
self.update()
|
||||
renderLock.RUnlock()
|
||||
self.Unlock()
|
||||
}
|
||||
}()
|
||||
|
||||
|
|
|
@ -6,7 +6,6 @@ import (
|
|||
"os/exec"
|
||||
"sort"
|
||||
"strconv"
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
psCPU "github.com/shirou/gopsutil/cpu"
|
||||
|
@ -46,7 +45,7 @@ type ProcWidget struct {
|
|||
showGroupedProcs bool
|
||||
}
|
||||
|
||||
func NewProcWidget(renderLock *sync.RWMutex) *ProcWidget {
|
||||
func NewProcWidget() *ProcWidget {
|
||||
cpuCount, err := psCPU.Counts(false)
|
||||
if err != nil {
|
||||
log.Printf("failed to get CPU count from gopsutil: %v", err)
|
||||
|
@ -77,9 +76,9 @@ func NewProcWidget(renderLock *sync.RWMutex) *ProcWidget {
|
|||
|
||||
go func() {
|
||||
for range time.NewTicker(self.updateInterval).C {
|
||||
renderLock.RLock()
|
||||
self.Lock()
|
||||
self.update()
|
||||
renderLock.RUnlock()
|
||||
self.Unlock()
|
||||
}
|
||||
}()
|
||||
|
||||
|
|
|
@ -4,7 +4,6 @@ import (
|
|||
"fmt"
|
||||
"image"
|
||||
"sort"
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
ui "github.com/gizak/termui/v3"
|
||||
|
@ -29,7 +28,7 @@ type TempWidget struct {
|
|||
TempScale TempScale
|
||||
}
|
||||
|
||||
func NewTempWidget(renderLock *sync.RWMutex, tempScale TempScale) *TempWidget {
|
||||
func NewTempWidget(tempScale TempScale) *TempWidget {
|
||||
self := &TempWidget{
|
||||
Block: ui.NewBlock(),
|
||||
updateInterval: time.Second * 5,
|
||||
|
@ -47,9 +46,9 @@ func NewTempWidget(renderLock *sync.RWMutex, tempScale TempScale) *TempWidget {
|
|||
|
||||
go func() {
|
||||
for range time.NewTicker(self.updateInterval).C {
|
||||
renderLock.RLock()
|
||||
self.Lock()
|
||||
self.update()
|
||||
renderLock.RUnlock()
|
||||
self.Unlock()
|
||||
}
|
||||
}()
|
||||
|
||||
|
|
Loading…
Reference in New Issue
Block a user