mirror of
https://github.com/jesseduffield/lazydocker.git
synced 2026-07-21 23:01: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.Title = "Stats"
|
||||
|
||||
return gui.T.NewTask(func(stop chan struct{}) {
|
||||
tickChan := time.NewTicker(time.Second)
|
||||
for {
|
||||
select {
|
||||
case <-stop:
|
||||
return
|
||||
case <-tickChan.C:
|
||||
width, _ := mainView.Size()
|
||||
return gui.T.NewTickerTask(time.Second, func() {
|
||||
width, _ := mainView.Size()
|
||||
|
||||
contents, err := container.RenderStats(width)
|
||||
if err != nil {
|
||||
gui.createErrorPanel(gui.g, err.Error())
|
||||
}
|
||||
|
||||
gui.reRenderString(gui.g, "main", contents)
|
||||
}
|
||||
contents, err := container.RenderStats(width)
|
||||
if err != nil {
|
||||
gui.createErrorPanel(gui.g, err.Error())
|
||||
}
|
||||
|
||||
gui.reRenderString(gui.g, "main", contents)
|
||||
})
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -2,6 +2,7 @@ package tasks
|
|||
|
||||
import (
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
"github.com/sirupsen/logrus"
|
||||
)
|
||||
|
|
@ -53,3 +54,19 @@ func (t *Task) Stop() {
|
|||
<-t.notifyStopped
|
||||
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