Use tickers instead of time.Sleep(), simplify main loop condition
This commit is contained in:
@@ -160,8 +160,7 @@ func main() {
|
||||
|
||||
fmt.Println("[ [],")
|
||||
|
||||
for {
|
||||
if <-Conf.Channels.UpdateReady {
|
||||
for range Conf.Channels.UpdateReady {
|
||||
// Actually build json struct, marshal it and print result
|
||||
var j []lib.I3BarOutBlock
|
||||
|
||||
@@ -569,7 +568,6 @@ func main() {
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// PrintToI3bar prints info to stdout according to ipc docs (https://i3wm.org/docs/i3bar-protocol.html)
|
||||
func PrintToI3bar(c *lib.MyConfig) {
|
||||
|
||||
@@ -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)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -13,9 +13,18 @@ func (c *MyConfig) UpdateVPNStatus() {
|
||||
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)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user