1 Commits
v1.4 ... del

Author SHA1 Message Date
eleksir
b8b9ba2c2c Use tickers instead of for loops with sleep, replace interface{} with any 2025-03-28 02:34:32 +03:00
9 changed files with 31 additions and 33 deletions

View File

@@ -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)
}
}

View File

@@ -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)
}
}

View File

@@ -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)
}

View File

@@ -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)
}
}

View File

@@ -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

View File

@@ -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)
}
}

View File

@@ -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)
}
}

View File

@@ -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)
}
}

View File

@@ -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)
}
}