mirror of
https://github.com/jesseduffield/lazydocker.git
synced 2026-07-25 08:31:03 +00:00
add new ticker task convenience function
This commit is contained in:
parent
28b488b740
commit
69b11cbfd2
2 changed files with 24 additions and 15 deletions
|
|
@ -123,23 +123,15 @@ func (gui *Gui) renderContainerStats(mainView *gocui.View, container *commands.C
|
||||||
mainView.Autoscroll = false
|
mainView.Autoscroll = false
|
||||||
mainView.Title = "Stats"
|
mainView.Title = "Stats"
|
||||||
|
|
||||||
return gui.T.NewTask(func(stop chan struct{}) {
|
return gui.T.NewTickerTask(time.Second, func() {
|
||||||
tickChan := time.NewTicker(time.Second)
|
width, _ := mainView.Size()
|
||||||
for {
|
|
||||||
select {
|
|
||||||
case <-stop:
|
|
||||||
return
|
|
||||||
case <-tickChan.C:
|
|
||||||
width, _ := mainView.Size()
|
|
||||||
|
|
||||||
contents, err := container.RenderStats(width)
|
contents, err := container.RenderStats(width)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
gui.createErrorPanel(gui.g, err.Error())
|
gui.createErrorPanel(gui.g, err.Error())
|
||||||
}
|
|
||||||
|
|
||||||
gui.reRenderString(gui.g, "main", contents)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
gui.reRenderString(gui.g, "main", contents)
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -2,6 +2,7 @@ package tasks
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"sync"
|
"sync"
|
||||||
|
"time"
|
||||||
|
|
||||||
"github.com/sirupsen/logrus"
|
"github.com/sirupsen/logrus"
|
||||||
)
|
)
|
||||||
|
|
@ -53,3 +54,19 @@ func (t *Task) Stop() {
|
||||||
<-t.notifyStopped
|
<-t.notifyStopped
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// NewTickerTask is a convenience function for making a new task that repeats some action once per e.g. second
|
||||||
|
func (t *TaskManager) NewTickerTask(duration time.Duration, f func()) error {
|
||||||
|
return t.NewTask(func(stop chan struct{}) {
|
||||||
|
tickChan := time.NewTicker(time.Second)
|
||||||
|
f() // calling f first so that we're not waiting for the first tick
|
||||||
|
for {
|
||||||
|
select {
|
||||||
|
case <-stop:
|
||||||
|
return
|
||||||
|
case <-tickChan.C:
|
||||||
|
f()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue