From b8b9ba2c2cef8b883703a42be3efe7e050fde376 Mon Sep 17 00:00:00 2001 From: eleksir Date: Fri, 28 Mar 2025 02:34:32 +0300 Subject: [PATCH] Use tickers instead of for loops with sleep, replace interface{} with any --- internal/lib/batteries.go | 6 +++--- internal/lib/clock.go | 6 +++--- internal/lib/collection.go | 12 ++++++------ internal/lib/cputemp.go | 6 +++--- internal/lib/i3.go | 8 ++++---- internal/lib/ifmon.go | 6 +++--- internal/lib/la.go | 6 +++--- internal/lib/memory.go | 8 +++----- internal/lib/vpn.go | 6 +++--- 9 files changed, 31 insertions(+), 33 deletions(-) diff --git a/internal/lib/batteries.go b/internal/lib/batteries.go index 1be34c2..ef07cd0 100644 --- a/internal/lib/batteries.go +++ b/internal/lib/batteries.go @@ -16,7 +16,9 @@ func (c *MyConfig) UpdateBatteryInfo() { Batts string ) - for { + ticker := time.NewTicker(time.Second * 5) + + for range ticker.C { batteries, err := battery.GetAll() if err != nil { @@ -166,7 +168,5 @@ func (c *MyConfig) UpdateBatteryInfo() { c.Values.BatteryString = Batts c.Channels.UpdateReady <- true } - - time.Sleep(5 * time.Second) } } diff --git a/internal/lib/clock.go b/internal/lib/clock.go index 97042fc..116a2bc 100644 --- a/internal/lib/clock.go +++ b/internal/lib/clock.go @@ -7,7 +7,9 @@ import ( // UpdateClock get and updates (on i3bar) info about system clock. func (c *MyConfig) UpdateClock() { - for { + ticker := time.NewTicker(time.Second * 1) + + for range ticker.C { currentTime := time.Now() hours, minutes, _ := currentTime.Clock() year, month, day := currentTime.Date() @@ -21,7 +23,5 @@ func (c *MyConfig) UpdateClock() { c.Values.ClockTime = myclock c.Channels.UpdateReady <- true } - - time.Sleep(1 * time.Second) } } diff --git a/internal/lib/collection.go b/internal/lib/collection.go index 613d8da..6a9cba3 100644 --- a/internal/lib/collection.go +++ b/internal/lib/collection.go @@ -12,7 +12,7 @@ type Collection struct { // Item just interface for stoing any data. type item struct { - data interface{} + data any } // NewCollection creates new instance of "collection". @@ -25,7 +25,7 @@ func NewCollection() *Collection { } // Get returns value by given key. Or empty value. -func (collection *Collection) Get(key interface{}) (interface{}, bool) { +func (collection *Collection) Get(key any) (any, bool) { obj, exists := collection.items.Load(key) if !exists { @@ -38,15 +38,15 @@ func (collection *Collection) Get(key interface{}) (interface{}, bool) { } // Set saves value to given "collection". -func (collection *Collection) Set(key interface{}, value interface{}) { +func (collection *Collection) Set(key any, value any) { collection.items.Store(key, item{ data: value, }) } // Range apply function f to all keys/values in collection. Retuns immediatly on first error. -func (collection *Collection) Range(f func(key, value interface{}) bool) { - fn := func(key, value interface{}) bool { +func (collection *Collection) Range(f func(key, value any) bool) { + fn := func(key, value any) bool { item := value.(item) return f(key, item.data) @@ -56,7 +56,7 @@ func (collection *Collection) Range(f func(key, value interface{}) bool) { } // Delete deletes key and corresponding value from collection. -func (collection *Collection) Delete(key interface{}) { +func (collection *Collection) Delete(key any) { collection.items.Delete(key) } diff --git a/internal/lib/cputemp.go b/internal/lib/cputemp.go index e4290c0..8313935 100644 --- a/internal/lib/cputemp.go +++ b/internal/lib/cputemp.go @@ -12,7 +12,9 @@ import ( func (c *MyConfig) UpdateCPUTemperature() { n := len(c.CPUTemp.File) - for { + ticker := time.NewTicker(time.Second * 3) + + for range ticker.C { var ( temperature = make([]int64, n) tSum int64 @@ -71,7 +73,5 @@ func (c *MyConfig) UpdateCPUTemperature() { c.Values.CPUTemperature = tAvg c.Channels.UpdateReady <- true } - - time.Sleep(3 * time.Second) } } diff --git a/internal/lib/i3.go b/internal/lib/i3.go index a6c0a18..3dec3e1 100644 --- a/internal/lib/i3.go +++ b/internal/lib/i3.go @@ -105,9 +105,9 @@ func (c *MyConfig) I3EventParser(e *i3.WindowEvent) { // interfaceToUint превращает данный интерфейс в Uint64. // Если может, конечно :) . -func interfaceToUint64(iface interface{}) uint64 { - // А теперь мы начинаем дурдом, нам надо превратить ёбанный interface{} в []string - // Поскольку interface{} может быть чем угодно, перестрахуемся +func interfaceToUint64(iface any) uint64 { + // А теперь мы начинаем дурдом, нам надо превратить ёбанный any в []string + // Поскольку any может быть чем угодно, перестрахуемся if reflect.TypeOf(iface).Kind() == reflect.Uint64 { return reflect.ValueOf(iface).Uint() } @@ -152,7 +152,7 @@ func HasWindows(className string, instanceName string) bool { func FindWindowInstance(regexpStr string) bool { var found = false - f := func(key interface{}, value interface{}) bool { + f := func(key any, value any) bool { keyStr := reflect.ValueOf(key).String() re, err := regexp.Compile(regexpStr) //nolint:nolintlint, gocritic, we do not want shit our pants and panic on diff --git a/internal/lib/ifmon.go b/internal/lib/ifmon.go index d975d38..2c671fc 100644 --- a/internal/lib/ifmon.go +++ b/internal/lib/ifmon.go @@ -11,7 +11,9 @@ import ( // UpdateIfStatus updates network interfaces status for i3bar. func (c *MyConfig) UpdateIfStatus() { - for { + ticker := time.NewTicker(time.Second * 3) + + for range ticker.C { var statusSum string for _, item := range c.NetIf.If { @@ -53,7 +55,5 @@ func (c *MyConfig) UpdateIfStatus() { c.Values.IfStatus = statusSum c.Channels.UpdateReady <- true } - - time.Sleep(3 * time.Second) } } diff --git a/internal/lib/la.go b/internal/lib/la.go index 88c7456..641667a 100644 --- a/internal/lib/la.go +++ b/internal/lib/la.go @@ -9,7 +9,9 @@ import ( // UpdateLaStats вытаскивает показание LA за последнюю минуту. func (c *MyConfig) UpdateLaStats() { - for { + ticker := time.NewTicker(time.Second * 3) + + for range ticker.C { l, _ := load.Avg() lav := fmt.Sprintf("%.2f", l.Load1) @@ -18,7 +20,5 @@ func (c *MyConfig) UpdateLaStats() { c.Values.La = lav c.Channels.UpdateReady <- true } - - time.Sleep(3 * time.Second) } } diff --git a/internal/lib/memory.go b/internal/lib/memory.go index 148cc7b..b49cc17 100644 --- a/internal/lib/memory.go +++ b/internal/lib/memory.go @@ -16,12 +16,13 @@ type Mem struct { // UpdateMemStats parses mem info stats. func (c *MyConfig) UpdateMemStats() { - for { + ticker := time.NewTicker(time.Second * 3) + + for range ticker.C { v, err := mem.VirtualMemory() if err != nil { log.Printf("Unable to get memory statistics: %s", err) - time.Sleep(1 * time.Second) continue } @@ -30,7 +31,6 @@ func (c *MyConfig) UpdateMemStats() { if err != nil { log.Printf("Unable to get swap statistics: %s", err) - time.Sleep(1 * time.Second) continue } @@ -49,7 +49,5 @@ func (c *MyConfig) UpdateMemStats() { c.Channels.UpdateReady <- true } } - - time.Sleep(3 * time.Second) } } diff --git a/internal/lib/vpn.go b/internal/lib/vpn.go index 9e8add9..e8bebbc 100644 --- a/internal/lib/vpn.go +++ b/internal/lib/vpn.go @@ -15,7 +15,9 @@ func (c *MyConfig) UpdateVPNStatus() { vpnCheck string ) - for { + ticker := time.NewTicker(time.Second * 3) + + for range ticker.C { if c.VPNFileCheck() { if c.Vpn.UpColor == "" { vpnCheck = `⍋` @@ -54,8 +56,6 @@ func (c *MyConfig) UpdateVPNStatus() { c.Values.VPNStatus = vpnCStatus c.Channels.UpdateReady <- true } - - time.Sleep(3 * time.Second) } }