From 4fd0815c1d28bf6f0627f1e5e616d529602ef1ff Mon Sep 17 00:00:00 2001 From: eleksir Date: Mon, 7 Apr 2025 20:41:01 +0300 Subject: [PATCH] Use tickers instead of time.Sleep(), simplify main loop condition --- cmd/i3status-go/main.go | 720 +++++++++++++++++++------------------- internal/lib/batteries.go | 7 +- internal/lib/clock.go | 16 +- internal/lib/cputemp.go | 17 +- internal/lib/ifmon.go | 16 +- internal/lib/la.go | 16 +- internal/lib/memory.go | 16 +- internal/lib/vpn.go | 19 +- 8 files changed, 439 insertions(+), 388 deletions(-) diff --git a/cmd/i3status-go/main.go b/cmd/i3status-go/main.go index 17e7a2a..eb69c0f 100644 --- a/cmd/i3status-go/main.go +++ b/cmd/i3status-go/main.go @@ -160,413 +160,411 @@ func main() { fmt.Println("[ [],") - for { - if <-Conf.Channels.UpdateReady { - // Actually build json struct, marshal it and print result - var j []lib.I3BarOutBlock + for range Conf.Channels.UpdateReady { + // Actually build json struct, marshal it and print result + var j []lib.I3BarOutBlock - // TODO: впилить для первого и последнего батона separator - if Conf.AppButtons.Enabled { - for num, app := range Conf.Apps { - var b lib.I3BarOutBlock - - if num == 0 { - b.FullText = fmt.Sprintf( - "%s", - Conf.AppButtons.Separator.Left.Color, - Conf.AppButtons.Separator.Left.Background, - Conf.AppButtons.Separator.Left.Font, - Conf.AppButtons.Separator.Left.FontSize, - Conf.AppButtons.Separator.Left.Symbol, - ) - } - - b.FullText = app.FullText - - if num == len(Conf.Apps) { - b.FullText = fmt.Sprintf( - "%s", - Conf.AppButtons.Separator.Right.Color, - Conf.AppButtons.Separator.Right.Background, - Conf.AppButtons.Separator.Right.Font, - Conf.AppButtons.Separator.Right.FontSize, - Conf.AppButtons.Separator.Right.Symbol, - ) - } - - b.Background = app.Background - b.Color = app.Color - b.Instance = app.Instance - b.Markup = `pango` - b.Separator = app.Separator - b.SeparatorBlockWidth = app.SeparatorBlockWidth - b.Name = app.Name - - if lib.HasWindows(app.Class, app.Instance) { - b.Border = app.BorderActive - } else { - b.Border = app.Border - } - - b.BorderTop = 1 - b.BorderRight = 1 - b.BorderBottom = 1 - b.BorderLeft = 1 - - j = append(j, b) - } - } - - if Conf.CPUTemp.Enabled { + // TODO: впилить для первого и последнего батона separator + if Conf.AppButtons.Enabled { + for num, app := range Conf.Apps { var b lib.I3BarOutBlock - b.Color = Conf.CPUTemp.Color - b.Background = Conf.CPUTemp.Background - - if Conf.CPUTemp.Separator.Left.Enabled { + if num == 0 { b.FullText = fmt.Sprintf( "%s", - Conf.CPUTemp.Separator.Left.Color, - Conf.CPUTemp.Separator.Left.Background, - Conf.CPUTemp.Separator.Left.Font, - Conf.CPUTemp.Separator.Left.FontSize, - Conf.CPUTemp.Separator.Left.Symbol, + Conf.AppButtons.Separator.Left.Color, + Conf.AppButtons.Separator.Left.Background, + Conf.AppButtons.Separator.Left.Font, + Conf.AppButtons.Separator.Left.FontSize, + Conf.AppButtons.Separator.Left.Symbol, ) } - b.FullText += fmt.Sprintf( - "CPU: %d°", - Conf.CPUTemp.Color, - Conf.CPUTemp.Background, - Conf.CPUTemp.Font, - Conf.CPUTemp.FontSize, - Conf.Values.CPUTemperature, - ) + b.FullText = app.FullText - if Conf.CPUTemp.Separator.Right.Enabled { - b.FullText += fmt.Sprintf( - "%s", - Conf.CPUTemp.Separator.Right.Color, - Conf.CPUTemp.Separator.Right.Background, - Conf.CPUTemp.Separator.Right.Font, - Conf.CPUTemp.Separator.Right.FontSize, - Conf.CPUTemp.Separator.Right.Symbol, - ) - } - - b.Markup = "pango" - b.Separator = false - - j = append(j, b) - } - - if Conf.Mem.Enabled { - var b lib.I3BarOutBlock - - b.Color = Conf.Mem.Color - b.Background = Conf.Mem.Background - - if Conf.Mem.Separator.Left.Enabled { + if num == len(Conf.Apps) { b.FullText = fmt.Sprintf( "%s", - Conf.Mem.Separator.Left.Color, - Conf.Mem.Separator.Left.Background, - Conf.Mem.Separator.Left.Font, - Conf.Mem.Separator.Left.FontSize, - Conf.Mem.Separator.Left.Symbol, + Conf.AppButtons.Separator.Right.Color, + Conf.AppButtons.Separator.Right.Background, + Conf.AppButtons.Separator.Right.Font, + Conf.AppButtons.Separator.Right.FontSize, + Conf.AppButtons.Separator.Right.Symbol, ) } - if Conf.Mem.ShowSwap { - b.FullText += fmt.Sprintf( - "M:%d%% SHM:%dM SW:%dM", - Conf.Mem.Color, - Conf.Mem.Background, - Conf.Mem.Font, - Conf.Mem.FontSize, - Conf.Values.Memory.Usedpct, - Conf.Values.Memory.Shared, - Conf.Values.Memory.Swap, - ) + b.Background = app.Background + b.Color = app.Color + b.Instance = app.Instance + b.Markup = `pango` + b.Separator = app.Separator + b.SeparatorBlockWidth = app.SeparatorBlockWidth + b.Name = app.Name + + if lib.HasWindows(app.Class, app.Instance) { + b.Border = app.BorderActive } else { - b.FullText += fmt.Sprintf( - "M:%d%% SHM:%dM", - Conf.Mem.Color, - Conf.Mem.Background, - Conf.Mem.Font, - Conf.Mem.FontSize, - Conf.Values.Memory.Usedpct, - Conf.Values.Memory.Shared, - ) + b.Border = app.Border } - if Conf.Mem.Separator.Right.Enabled { - b.FullText += fmt.Sprintf( - "%s", - Conf.Mem.Separator.Right.Color, - Conf.Mem.Separator.Right.Background, - Conf.Mem.Separator.Right.Font, - Conf.Mem.Separator.Right.FontSize, - Conf.Mem.Separator.Right.Symbol, - ) - } - - b.Markup = "pango" - b.Separator = false + b.BorderTop = 1 + b.BorderRight = 1 + b.BorderBottom = 1 + b.BorderLeft = 1 j = append(j, b) } + } - if Conf.LA.Enabled { - var b lib.I3BarOutBlock + if Conf.CPUTemp.Enabled { + var b lib.I3BarOutBlock - b.Color = Conf.LA.Color - b.Background = Conf.LA.Background + b.Color = Conf.CPUTemp.Color + b.Background = Conf.CPUTemp.Background - if Conf.LA.Separator.Left.Enabled { - b.FullText = fmt.Sprintf( - "%s", - Conf.LA.Separator.Left.Color, - Conf.LA.Separator.Left.Background, - Conf.LA.Separator.Left.Font, - Conf.LA.Separator.Left.FontSize, - Conf.LA.Separator.Left.Symbol, - ) - } - - b.FullText += fmt.Sprintf( - "LA:%s", - Conf.LA.Color, - Conf.LA.Background, - Conf.LA.Font, - Conf.LA.FontSize, - Conf.Values.La, + if Conf.CPUTemp.Separator.Left.Enabled { + b.FullText = fmt.Sprintf( + "%s", + Conf.CPUTemp.Separator.Left.Color, + Conf.CPUTemp.Separator.Left.Background, + Conf.CPUTemp.Separator.Left.Font, + Conf.CPUTemp.Separator.Left.FontSize, + Conf.CPUTemp.Separator.Left.Symbol, ) - - if Conf.LA.Separator.Right.Enabled { - b.FullText += fmt.Sprintf( - "%s", - Conf.LA.Separator.Right.Color, - Conf.LA.Separator.Right.Background, - Conf.LA.Separator.Right.Font, - Conf.LA.Separator.Right.FontSize, - Conf.LA.Separator.Right.Symbol, - ) - } - - b.Markup = "pango" - b.Separator = false - - j = append(j, b) } - if Conf.NetIf.Enabled { - var b lib.I3BarOutBlock - - b.Color = Conf.NetIf.Color - b.Background = Conf.NetIf.Background - - if Conf.NetIf.Separator.Left.Enabled { - b.FullText = fmt.Sprintf( - "%s", - Conf.NetIf.Separator.Left.Color, - Conf.NetIf.Separator.Left.Background, - Conf.NetIf.Separator.Left.Font, - Conf.NetIf.Separator.Left.FontSize, - Conf.NetIf.Separator.Left.Symbol, - ) - } - - b.FullText += fmt.Sprintf( - "%s", - Conf.NetIf.Color, - Conf.NetIf.Background, - Conf.NetIf.Font, - Conf.NetIf.FontSize, - Conf.Values.IfStatus, - ) - - if Conf.NetIf.Separator.Right.Enabled { - b.FullText += fmt.Sprintf( - "%s", - Conf.NetIf.Separator.Right.Color, - Conf.NetIf.Separator.Right.Background, - Conf.NetIf.Separator.Right.Font, - Conf.NetIf.Separator.Right.FontSize, - Conf.NetIf.Separator.Right.Symbol, - ) - } - - b.Markup = "pango" - b.Separator = false - - j = append(j, b) - } - - if Conf.Vpn.Enabled { - var b lib.I3BarOutBlock - - b.Color = Conf.Vpn.Color - b.Background = Conf.Vpn.Background - - if Conf.Vpn.Separator.Left.Enabled { - b.FullText = fmt.Sprintf( - "%s", - Conf.Vpn.Separator.Left.Color, - Conf.Vpn.Separator.Left.Background, - Conf.Vpn.Separator.Left.Font, - Conf.Vpn.Separator.Left.FontSize, - Conf.Vpn.Separator.Left.Symbol, - ) - } + b.FullText += fmt.Sprintf( + "CPU: %d°", + Conf.CPUTemp.Color, + Conf.CPUTemp.Background, + Conf.CPUTemp.Font, + Conf.CPUTemp.FontSize, + Conf.Values.CPUTemperature, + ) + if Conf.CPUTemp.Separator.Right.Enabled { b.FullText += fmt.Sprintf( "%s", - Conf.Vpn.Color, - Conf.Vpn.Background, - Conf.Vpn.Font, - Conf.Vpn.FontSize, - Conf.Values.VPNStatus, + Conf.CPUTemp.Separator.Right.Color, + Conf.CPUTemp.Separator.Right.Background, + Conf.CPUTemp.Separator.Right.Font, + Conf.CPUTemp.Separator.Right.FontSize, + Conf.CPUTemp.Separator.Right.Symbol, ) - - if Conf.Vpn.Separator.Right.Enabled { - b.FullText += fmt.Sprintf( - "%s", - Conf.Vpn.Separator.Right.Color, - Conf.Vpn.Separator.Right.Background, - Conf.Vpn.Separator.Right.Font, - Conf.Vpn.Separator.Right.FontSize, - Conf.Vpn.Separator.Right.Symbol, - ) - } - - b.Markup = "pango" - b.Separator = false - - j = append(j, b) } - if Conf.Battery.Enabled { - var b lib.I3BarOutBlock + b.Markup = "pango" + b.Separator = false - b.Color = Conf.Battery.Color - b.Background = Conf.Battery.Background + j = append(j, b) + } - if Conf.Battery.Separator.Left.Enabled { - b.FullText = fmt.Sprintf( - "%s", - Conf.Battery.Separator.Left.Color, - Conf.Battery.Separator.Left.Background, - Conf.Battery.Separator.Left.Font, - Conf.Battery.Separator.Left.FontSize, - Conf.Battery.Separator.Left.Symbol, - ) - } + if Conf.Mem.Enabled { + var b lib.I3BarOutBlock - b.FullText += Conf.Values.BatteryString + b.Color = Conf.Mem.Color + b.Background = Conf.Mem.Background - if Conf.Battery.Separator.Right.Enabled { - b.FullText += fmt.Sprintf( - "%s", - Conf.Battery.Separator.Right.Color, - Conf.Battery.Separator.Right.Background, - Conf.Battery.Separator.Right.Font, - Conf.Battery.Separator.Right.FontSize, - Conf.Battery.Separator.Right.Symbol, - ) - } - - b.Markup = "pango" - b.Separator = false - - j = append(j, b) + if Conf.Mem.Separator.Left.Enabled { + b.FullText = fmt.Sprintf( + "%s", + Conf.Mem.Separator.Left.Color, + Conf.Mem.Separator.Left.Background, + Conf.Mem.Separator.Left.Font, + Conf.Mem.Separator.Left.FontSize, + Conf.Mem.Separator.Left.Symbol, + ) } - if Conf.SimpleVolumePa.Enabled { - var b lib.I3BarOutBlock - - b.Name = "simple-volume-pa" - b.Color = Conf.SimpleVolumePa.Color - b.Background = Conf.SimpleVolumePa.Background - - if Conf.SimpleVolumePa.Separator.Left.Enabled { - b.FullText = fmt.Sprintf( - "%s", - Conf.SimpleVolumePa.Separator.Left.Color, - Conf.SimpleVolumePa.Separator.Left.Background, - Conf.SimpleVolumePa.Separator.Left.Font, - Conf.SimpleVolumePa.Separator.Left.FontSize, - Conf.SimpleVolumePa.Separator.Left.Symbol, - ) - } - - // Pango format is already applied in plugin src. - b.FullText += Conf.Values.SoundVolume - - if Conf.SimpleVolumePa.Separator.Right.Enabled { - b.FullText += fmt.Sprintf( - "%s", - Conf.SimpleVolumePa.Separator.Right.Color, - Conf.SimpleVolumePa.Separator.Right.Background, - Conf.SimpleVolumePa.Separator.Right.Font, - Conf.SimpleVolumePa.Separator.Right.FontSize, - Conf.SimpleVolumePa.Separator.Right.Symbol, - ) - } - - b.Markup = "pango" - b.Separator = false - - j = append(j, b) + if Conf.Mem.ShowSwap { + b.FullText += fmt.Sprintf( + "M:%d%% SHM:%dM SW:%dM", + Conf.Mem.Color, + Conf.Mem.Background, + Conf.Mem.Font, + Conf.Mem.FontSize, + Conf.Values.Memory.Usedpct, + Conf.Values.Memory.Shared, + Conf.Values.Memory.Swap, + ) + } else { + b.FullText += fmt.Sprintf( + "M:%d%% SHM:%dM", + Conf.Mem.Color, + Conf.Mem.Background, + Conf.Mem.Font, + Conf.Mem.FontSize, + Conf.Values.Memory.Usedpct, + Conf.Values.Memory.Shared, + ) } - if Conf.Clock.Enabled { - var b lib.I3BarOutBlock - - b.Name = `wallclock` - b.Color = Conf.Clock.Color - b.Background = Conf.Clock.Background - - if Conf.Clock.Separator.Left.Enabled { - b.FullText = fmt.Sprintf( - "%s", - Conf.Clock.Separator.Left.Color, - Conf.Clock.Separator.Left.Background, - Conf.Clock.Separator.Left.Font, - Conf.Clock.Separator.Left.FontSize, - Conf.Clock.Separator.Left.Symbol, - ) - } - + if Conf.Mem.Separator.Right.Enabled { b.FullText += fmt.Sprintf( "%s", - Conf.Clock.Color, - Conf.Clock.Background, - Conf.Clock.Font, - Conf.Clock.FontSize, - Conf.Values.ClockTime, + Conf.Mem.Separator.Right.Color, + Conf.Mem.Separator.Right.Background, + Conf.Mem.Separator.Right.Font, + Conf.Mem.Separator.Right.FontSize, + Conf.Mem.Separator.Right.Symbol, ) - - if Conf.Clock.Separator.Right.Enabled { - b.FullText += fmt.Sprintf( - "%s", - Conf.Clock.Separator.Right.Color, - Conf.Clock.Separator.Right.Background, - Conf.Clock.Separator.Right.Font, - Conf.Clock.Separator.Right.FontSize, - Conf.Clock.Separator.Right.Symbol, - ) - } - - b.Markup = "pango" - b.Separator = false - - j = append(j, b) } - if Conf.Values.PrintOutput && len(j) > 0 { - Conf.Channels.MsgChan <- j + b.Markup = "pango" + b.Separator = false + + j = append(j, b) + } + + if Conf.LA.Enabled { + var b lib.I3BarOutBlock + + b.Color = Conf.LA.Color + b.Background = Conf.LA.Background + + if Conf.LA.Separator.Left.Enabled { + b.FullText = fmt.Sprintf( + "%s", + Conf.LA.Separator.Left.Color, + Conf.LA.Separator.Left.Background, + Conf.LA.Separator.Left.Font, + Conf.LA.Separator.Left.FontSize, + Conf.LA.Separator.Left.Symbol, + ) } + + b.FullText += fmt.Sprintf( + "LA:%s", + Conf.LA.Color, + Conf.LA.Background, + Conf.LA.Font, + Conf.LA.FontSize, + Conf.Values.La, + ) + + if Conf.LA.Separator.Right.Enabled { + b.FullText += fmt.Sprintf( + "%s", + Conf.LA.Separator.Right.Color, + Conf.LA.Separator.Right.Background, + Conf.LA.Separator.Right.Font, + Conf.LA.Separator.Right.FontSize, + Conf.LA.Separator.Right.Symbol, + ) + } + + b.Markup = "pango" + b.Separator = false + + j = append(j, b) + } + + if Conf.NetIf.Enabled { + var b lib.I3BarOutBlock + + b.Color = Conf.NetIf.Color + b.Background = Conf.NetIf.Background + + if Conf.NetIf.Separator.Left.Enabled { + b.FullText = fmt.Sprintf( + "%s", + Conf.NetIf.Separator.Left.Color, + Conf.NetIf.Separator.Left.Background, + Conf.NetIf.Separator.Left.Font, + Conf.NetIf.Separator.Left.FontSize, + Conf.NetIf.Separator.Left.Symbol, + ) + } + + b.FullText += fmt.Sprintf( + "%s", + Conf.NetIf.Color, + Conf.NetIf.Background, + Conf.NetIf.Font, + Conf.NetIf.FontSize, + Conf.Values.IfStatus, + ) + + if Conf.NetIf.Separator.Right.Enabled { + b.FullText += fmt.Sprintf( + "%s", + Conf.NetIf.Separator.Right.Color, + Conf.NetIf.Separator.Right.Background, + Conf.NetIf.Separator.Right.Font, + Conf.NetIf.Separator.Right.FontSize, + Conf.NetIf.Separator.Right.Symbol, + ) + } + + b.Markup = "pango" + b.Separator = false + + j = append(j, b) + } + + if Conf.Vpn.Enabled { + var b lib.I3BarOutBlock + + b.Color = Conf.Vpn.Color + b.Background = Conf.Vpn.Background + + if Conf.Vpn.Separator.Left.Enabled { + b.FullText = fmt.Sprintf( + "%s", + Conf.Vpn.Separator.Left.Color, + Conf.Vpn.Separator.Left.Background, + Conf.Vpn.Separator.Left.Font, + Conf.Vpn.Separator.Left.FontSize, + Conf.Vpn.Separator.Left.Symbol, + ) + } + + b.FullText += fmt.Sprintf( + "%s", + Conf.Vpn.Color, + Conf.Vpn.Background, + Conf.Vpn.Font, + Conf.Vpn.FontSize, + Conf.Values.VPNStatus, + ) + + if Conf.Vpn.Separator.Right.Enabled { + b.FullText += fmt.Sprintf( + "%s", + Conf.Vpn.Separator.Right.Color, + Conf.Vpn.Separator.Right.Background, + Conf.Vpn.Separator.Right.Font, + Conf.Vpn.Separator.Right.FontSize, + Conf.Vpn.Separator.Right.Symbol, + ) + } + + b.Markup = "pango" + b.Separator = false + + j = append(j, b) + } + + if Conf.Battery.Enabled { + var b lib.I3BarOutBlock + + b.Color = Conf.Battery.Color + b.Background = Conf.Battery.Background + + if Conf.Battery.Separator.Left.Enabled { + b.FullText = fmt.Sprintf( + "%s", + Conf.Battery.Separator.Left.Color, + Conf.Battery.Separator.Left.Background, + Conf.Battery.Separator.Left.Font, + Conf.Battery.Separator.Left.FontSize, + Conf.Battery.Separator.Left.Symbol, + ) + } + + b.FullText += Conf.Values.BatteryString + + if Conf.Battery.Separator.Right.Enabled { + b.FullText += fmt.Sprintf( + "%s", + Conf.Battery.Separator.Right.Color, + Conf.Battery.Separator.Right.Background, + Conf.Battery.Separator.Right.Font, + Conf.Battery.Separator.Right.FontSize, + Conf.Battery.Separator.Right.Symbol, + ) + } + + b.Markup = "pango" + b.Separator = false + + j = append(j, b) + } + + if Conf.SimpleVolumePa.Enabled { + var b lib.I3BarOutBlock + + b.Name = "simple-volume-pa" + b.Color = Conf.SimpleVolumePa.Color + b.Background = Conf.SimpleVolumePa.Background + + if Conf.SimpleVolumePa.Separator.Left.Enabled { + b.FullText = fmt.Sprintf( + "%s", + Conf.SimpleVolumePa.Separator.Left.Color, + Conf.SimpleVolumePa.Separator.Left.Background, + Conf.SimpleVolumePa.Separator.Left.Font, + Conf.SimpleVolumePa.Separator.Left.FontSize, + Conf.SimpleVolumePa.Separator.Left.Symbol, + ) + } + + // Pango format is already applied in plugin src. + b.FullText += Conf.Values.SoundVolume + + if Conf.SimpleVolumePa.Separator.Right.Enabled { + b.FullText += fmt.Sprintf( + "%s", + Conf.SimpleVolumePa.Separator.Right.Color, + Conf.SimpleVolumePa.Separator.Right.Background, + Conf.SimpleVolumePa.Separator.Right.Font, + Conf.SimpleVolumePa.Separator.Right.FontSize, + Conf.SimpleVolumePa.Separator.Right.Symbol, + ) + } + + b.Markup = "pango" + b.Separator = false + + j = append(j, b) + } + + if Conf.Clock.Enabled { + var b lib.I3BarOutBlock + + b.Name = `wallclock` + b.Color = Conf.Clock.Color + b.Background = Conf.Clock.Background + + if Conf.Clock.Separator.Left.Enabled { + b.FullText = fmt.Sprintf( + "%s", + Conf.Clock.Separator.Left.Color, + Conf.Clock.Separator.Left.Background, + Conf.Clock.Separator.Left.Font, + Conf.Clock.Separator.Left.FontSize, + Conf.Clock.Separator.Left.Symbol, + ) + } + + b.FullText += fmt.Sprintf( + "%s", + Conf.Clock.Color, + Conf.Clock.Background, + Conf.Clock.Font, + Conf.Clock.FontSize, + Conf.Values.ClockTime, + ) + + if Conf.Clock.Separator.Right.Enabled { + b.FullText += fmt.Sprintf( + "%s", + Conf.Clock.Separator.Right.Color, + Conf.Clock.Separator.Right.Background, + Conf.Clock.Separator.Right.Font, + Conf.Clock.Separator.Right.FontSize, + Conf.Clock.Separator.Right.Symbol, + ) + } + + b.Markup = "pango" + b.Separator = false + + j = append(j, b) + } + + if Conf.Values.PrintOutput && len(j) > 0 { + Conf.Channels.MsgChan <- j } } } diff --git a/internal/lib/batteries.go b/internal/lib/batteries.go index 17076c0..fe6c9a1 100644 --- a/internal/lib/batteries.go +++ b/internal/lib/batteries.go @@ -16,11 +16,10 @@ func (c *MyConfig) UpdateBatteryInfo() { Batts string InitialDelay = 100 * time.Millisecond LoopIterationDelay = 5 * time.Second + Delay = InitialDelay + ticker = time.NewTicker(Delay) ) - Delay := InitialDelay - ticker := time.NewTicker(Delay) - for range ticker.C { if Delay == InitialDelay { Delay = LoopIterationDelay @@ -170,7 +169,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..d88a815 100644 --- a/internal/lib/clock.go +++ b/internal/lib/clock.go @@ -7,7 +7,19 @@ import ( // UpdateClock get and updates (on i3bar) info about system clock. func (c *MyConfig) UpdateClock() { - for { + var ( + InitialDelay = 100 * time.Millisecond + LoopIterationDelay = 1 * time.Second + Delay = InitialDelay + ticker = time.NewTicker(Delay) + ) + + for range ticker.C { + if Delay == InitialDelay { + Delay = LoopIterationDelay + ticker.Reset(Delay) + } + currentTime := time.Now() hours, minutes, _ := currentTime.Clock() year, month, day := currentTime.Date() @@ -21,7 +33,5 @@ func (c *MyConfig) UpdateClock() { c.Values.ClockTime = myclock c.Channels.UpdateReady <- true } - - time.Sleep(1 * time.Second) } } diff --git a/internal/lib/cputemp.go b/internal/lib/cputemp.go index e4290c0..3bc6d22 100644 --- a/internal/lib/cputemp.go +++ b/internal/lib/cputemp.go @@ -10,15 +10,26 @@ import ( // UpdateCPUTemperature gets and updates average CPU cores temperature. func (c *MyConfig) UpdateCPUTemperature() { - n := len(c.CPUTemp.File) + var ( + n = len(c.CPUTemp.File) + InitialDelay = 100 * time.Millisecond + LoopIterationDelay = 3 * time.Second + Delay = InitialDelay + ticker = time.NewTicker(Delay) + ) - for { + for range ticker.C { var ( temperature = make([]int64, n) tSum int64 tAvg int64 ) + if Delay == InitialDelay { + Delay = LoopIterationDelay + ticker.Reset(Delay) + } + for i, filename := range c.CPUTemp.File { file, err := os.Open(filename) @@ -71,7 +82,5 @@ func (c *MyConfig) UpdateCPUTemperature() { c.Values.CPUTemperature = tAvg c.Channels.UpdateReady <- true } - - time.Sleep(3 * time.Second) } } diff --git a/internal/lib/ifmon.go b/internal/lib/ifmon.go index d975d38..6165ae8 100644 --- a/internal/lib/ifmon.go +++ b/internal/lib/ifmon.go @@ -11,7 +11,19 @@ import ( // UpdateIfStatus updates network interfaces status for i3bar. func (c *MyConfig) UpdateIfStatus() { - for { + var ( + InitialDelay = 100 * time.Millisecond + LoopIterationDelay = 3 * time.Second + Delay = InitialDelay + ticker = time.NewTicker(Delay) + ) + + for range ticker.C { + if Delay == InitialDelay { + Delay = LoopIterationDelay + ticker.Reset(Delay) + } + var statusSum string for _, item := range c.NetIf.If { @@ -53,7 +65,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..fcb0770 100644 --- a/internal/lib/la.go +++ b/internal/lib/la.go @@ -9,7 +9,19 @@ import ( // UpdateLaStats вытаскивает показание LA за последнюю минуту. func (c *MyConfig) UpdateLaStats() { - for { + var ( + InitialDelay = 100 * time.Millisecond + LoopIterationDelay = 3 * time.Second + Delay = InitialDelay + ticker = time.NewTicker(Delay) + ) + + for range ticker.C { + if Delay == InitialDelay { + Delay = LoopIterationDelay + ticker.Reset(Delay) + } + l, _ := load.Avg() lav := fmt.Sprintf("%.2f", l.Load1) @@ -18,7 +30,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..128ae05 100644 --- a/internal/lib/memory.go +++ b/internal/lib/memory.go @@ -16,7 +16,19 @@ type Mem struct { // UpdateMemStats parses mem info stats. func (c *MyConfig) UpdateMemStats() { - for { + var ( + InitialDelay = 100 * time.Millisecond + LoopIterationDelay = 3 * time.Second + Delay = InitialDelay + ticker = time.NewTicker(Delay) + ) + + for range ticker.C { + if Delay == InitialDelay { + Delay = LoopIterationDelay + ticker.Reset(Delay) + } + v, err := mem.VirtualMemory() if err != nil { @@ -49,7 +61,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 df5b55a..bad81bf 100644 --- a/internal/lib/vpn.go +++ b/internal/lib/vpn.go @@ -10,12 +10,21 @@ import ( // UpdateVPNStatus periodically update status of openvpn daemon. func (c *MyConfig) UpdateVPNStatus() { var ( - vpnCStatus string - tcpCheck string - vpnCheck string + vpnCStatus string + tcpCheck string + vpnCheck string + InitialDelay = 100 * time.Millisecond + LoopIterationDelay = 3 * time.Second + Delay = InitialDelay + ticker = time.NewTicker(Delay) ) - for { + for range ticker.C { + if Delay == InitialDelay { + Delay = LoopIterationDelay + ticker.Reset(Delay) + } + if c.VPNFileCheck() { if c.Vpn.UpColor == "" { vpnCheck = `⍋` @@ -54,8 +63,6 @@ func (c *MyConfig) UpdateVPNStatus() { c.Values.VPNStatus = vpnCStatus c.Channels.UpdateReady <- true } - - time.Sleep(3 * time.Second) } }