Use tickers instead of time.Sleep(), simplify main loop condition
This commit is contained in:
@@ -160,8 +160,7 @@ func main() {
|
|||||||
|
|
||||||
fmt.Println("[ [],")
|
fmt.Println("[ [],")
|
||||||
|
|
||||||
for {
|
for range Conf.Channels.UpdateReady {
|
||||||
if <-Conf.Channels.UpdateReady {
|
|
||||||
// Actually build json struct, marshal it and print result
|
// Actually build json struct, marshal it and print result
|
||||||
var j []lib.I3BarOutBlock
|
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)
|
// PrintToI3bar prints info to stdout according to ipc docs (https://i3wm.org/docs/i3bar-protocol.html)
|
||||||
func PrintToI3bar(c *lib.MyConfig) {
|
func PrintToI3bar(c *lib.MyConfig) {
|
||||||
|
|||||||
@@ -16,11 +16,10 @@ func (c *MyConfig) UpdateBatteryInfo() {
|
|||||||
Batts string
|
Batts string
|
||||||
InitialDelay = 100 * time.Millisecond
|
InitialDelay = 100 * time.Millisecond
|
||||||
LoopIterationDelay = 5 * time.Second
|
LoopIterationDelay = 5 * time.Second
|
||||||
|
Delay = InitialDelay
|
||||||
|
ticker = time.NewTicker(Delay)
|
||||||
)
|
)
|
||||||
|
|
||||||
Delay := InitialDelay
|
|
||||||
ticker := time.NewTicker(Delay)
|
|
||||||
|
|
||||||
for range ticker.C {
|
for range ticker.C {
|
||||||
if Delay == InitialDelay {
|
if Delay == InitialDelay {
|
||||||
Delay = LoopIterationDelay
|
Delay = LoopIterationDelay
|
||||||
@@ -170,7 +169,5 @@ func (c *MyConfig) UpdateBatteryInfo() {
|
|||||||
c.Values.BatteryString = Batts
|
c.Values.BatteryString = Batts
|
||||||
c.Channels.UpdateReady <- true
|
c.Channels.UpdateReady <- true
|
||||||
}
|
}
|
||||||
|
|
||||||
time.Sleep(5 * time.Second)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -7,7 +7,19 @@ import (
|
|||||||
|
|
||||||
// UpdateClock get and updates (on i3bar) info about system clock.
|
// UpdateClock get and updates (on i3bar) info about system clock.
|
||||||
func (c *MyConfig) UpdateClock() {
|
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()
|
currentTime := time.Now()
|
||||||
hours, minutes, _ := currentTime.Clock()
|
hours, minutes, _ := currentTime.Clock()
|
||||||
year, month, day := currentTime.Date()
|
year, month, day := currentTime.Date()
|
||||||
@@ -21,7 +33,5 @@ func (c *MyConfig) UpdateClock() {
|
|||||||
c.Values.ClockTime = myclock
|
c.Values.ClockTime = myclock
|
||||||
c.Channels.UpdateReady <- true
|
c.Channels.UpdateReady <- true
|
||||||
}
|
}
|
||||||
|
|
||||||
time.Sleep(1 * time.Second)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -10,15 +10,26 @@ import (
|
|||||||
|
|
||||||
// UpdateCPUTemperature gets and updates average CPU cores temperature.
|
// UpdateCPUTemperature gets and updates average CPU cores temperature.
|
||||||
func (c *MyConfig) UpdateCPUTemperature() {
|
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 (
|
var (
|
||||||
temperature = make([]int64, n)
|
temperature = make([]int64, n)
|
||||||
tSum int64
|
tSum int64
|
||||||
tAvg int64
|
tAvg int64
|
||||||
)
|
)
|
||||||
|
|
||||||
|
if Delay == InitialDelay {
|
||||||
|
Delay = LoopIterationDelay
|
||||||
|
ticker.Reset(Delay)
|
||||||
|
}
|
||||||
|
|
||||||
for i, filename := range c.CPUTemp.File {
|
for i, filename := range c.CPUTemp.File {
|
||||||
file, err := os.Open(filename)
|
file, err := os.Open(filename)
|
||||||
|
|
||||||
@@ -71,7 +82,5 @@ func (c *MyConfig) UpdateCPUTemperature() {
|
|||||||
c.Values.CPUTemperature = tAvg
|
c.Values.CPUTemperature = tAvg
|
||||||
c.Channels.UpdateReady <- true
|
c.Channels.UpdateReady <- true
|
||||||
}
|
}
|
||||||
|
|
||||||
time.Sleep(3 * time.Second)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -11,7 +11,19 @@ import (
|
|||||||
|
|
||||||
// UpdateIfStatus updates network interfaces status for i3bar.
|
// UpdateIfStatus updates network interfaces status for i3bar.
|
||||||
func (c *MyConfig) UpdateIfStatus() {
|
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
|
var statusSum string
|
||||||
|
|
||||||
for _, item := range c.NetIf.If {
|
for _, item := range c.NetIf.If {
|
||||||
@@ -53,7 +65,5 @@ func (c *MyConfig) UpdateIfStatus() {
|
|||||||
c.Values.IfStatus = statusSum
|
c.Values.IfStatus = statusSum
|
||||||
c.Channels.UpdateReady <- true
|
c.Channels.UpdateReady <- true
|
||||||
}
|
}
|
||||||
|
|
||||||
time.Sleep(3 * time.Second)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -9,7 +9,19 @@ import (
|
|||||||
|
|
||||||
// UpdateLaStats вытаскивает показание LA за последнюю минуту.
|
// UpdateLaStats вытаскивает показание LA за последнюю минуту.
|
||||||
func (c *MyConfig) UpdateLaStats() {
|
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()
|
l, _ := load.Avg()
|
||||||
|
|
||||||
lav := fmt.Sprintf("%.2f", l.Load1)
|
lav := fmt.Sprintf("%.2f", l.Load1)
|
||||||
@@ -18,7 +30,5 @@ func (c *MyConfig) UpdateLaStats() {
|
|||||||
c.Values.La = lav
|
c.Values.La = lav
|
||||||
c.Channels.UpdateReady <- true
|
c.Channels.UpdateReady <- true
|
||||||
}
|
}
|
||||||
|
|
||||||
time.Sleep(3 * time.Second)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -16,7 +16,19 @@ type Mem struct {
|
|||||||
|
|
||||||
// UpdateMemStats parses mem info stats.
|
// UpdateMemStats parses mem info stats.
|
||||||
func (c *MyConfig) UpdateMemStats() {
|
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()
|
v, err := mem.VirtualMemory()
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -49,7 +61,5 @@ func (c *MyConfig) UpdateMemStats() {
|
|||||||
c.Channels.UpdateReady <- true
|
c.Channels.UpdateReady <- true
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
time.Sleep(3 * time.Second)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -13,9 +13,18 @@ func (c *MyConfig) UpdateVPNStatus() {
|
|||||||
vpnCStatus string
|
vpnCStatus string
|
||||||
tcpCheck string
|
tcpCheck string
|
||||||
vpnCheck 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.VPNFileCheck() {
|
||||||
if c.Vpn.UpColor == "" {
|
if c.Vpn.UpColor == "" {
|
||||||
vpnCheck = `⍋`
|
vpnCheck = `⍋`
|
||||||
@@ -54,8 +63,6 @@ func (c *MyConfig) UpdateVPNStatus() {
|
|||||||
c.Values.VPNStatus = vpnCStatus
|
c.Values.VPNStatus = vpnCStatus
|
||||||
c.Channels.UpdateReady <- true
|
c.Channels.UpdateReady <- true
|
||||||
}
|
}
|
||||||
|
|
||||||
time.Sleep(3 * time.Second)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user