Refactor date and clock widgets to simplify periodic updates and remove redundant ticker logic.
This commit is contained in:
parent
4ac47429cd
commit
ec85f87377
@ -26,11 +26,6 @@ func createClock(ctx context.Context, _ terminalapi.Terminal, _ interface{}) wid
|
|||||||
widget := util.PanicOnErrorWithResult(segmentdisplay.New())
|
widget := util.PanicOnErrorWithResult(segmentdisplay.New())
|
||||||
|
|
||||||
go util.Periodic(ctx, util.RedrawInterval, func() error {
|
go util.Periodic(ctx, util.RedrawInterval, func() error {
|
||||||
ticker := time.NewTicker(1 * time.Second)
|
|
||||||
defer ticker.Stop()
|
|
||||||
for {
|
|
||||||
select {
|
|
||||||
case <-ticker.C:
|
|
||||||
now := time.Now()
|
now := time.Now()
|
||||||
nowStr := now.Format("15 04")
|
nowStr := now.Format("15 04")
|
||||||
parts := strings.Split(nowStr, " ")
|
parts := strings.Split(nowStr, " ")
|
||||||
@ -45,13 +40,9 @@ func createClock(ctx context.Context, _ terminalapi.Terminal, _ interface{}) wid
|
|||||||
segmentdisplay.NewChunk(parts[1], segmentdisplay.WriteCellOpts(cell.FgColor(cell.ColorWhite))),
|
segmentdisplay.NewChunk(parts[1], segmentdisplay.WriteCellOpts(cell.FgColor(cell.ColorWhite))),
|
||||||
}
|
}
|
||||||
if err := widget.Write(chunks, segmentdisplay.AlignHorizontal(align.HorizontalCenter), segmentdisplay.AlignVertical(align.VerticalBottom)); err != nil {
|
if err := widget.Write(chunks, segmentdisplay.AlignHorizontal(align.HorizontalCenter), segmentdisplay.AlignVertical(align.VerticalBottom)); err != nil {
|
||||||
panic(err)
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
case <-ctx.Done():
|
|
||||||
return nil
|
return nil
|
||||||
}
|
|
||||||
}
|
|
||||||
})
|
})
|
||||||
return widget
|
return widget
|
||||||
}
|
}
|
||||||
|
|||||||
@ -22,20 +22,12 @@ func createDate(ctx context.Context, _ terminalapi.Terminal, _ interface{}) widg
|
|||||||
widget := util.PanicOnErrorWithResult(text.New())
|
widget := util.PanicOnErrorWithResult(text.New())
|
||||||
|
|
||||||
go util.Periodic(ctx, 1*time.Second, func() error {
|
go util.Periodic(ctx, 1*time.Second, func() error {
|
||||||
ticker := time.NewTicker(1 * time.Second)
|
|
||||||
defer ticker.Stop()
|
|
||||||
for {
|
|
||||||
select {
|
|
||||||
case <-ticker.C:
|
|
||||||
now := time.Now()
|
now := time.Now()
|
||||||
if err := widget.Write(now.Format("Monday 2.1.2006"), text.WriteReplace()); err != nil {
|
|
||||||
panic(err)
|
|
||||||
}
|
|
||||||
|
|
||||||
case <-ctx.Done():
|
if err := widget.Write(now.Format("Monday 2.1.2006"), text.WriteReplace()); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
return nil
|
return nil
|
||||||
}
|
|
||||||
}
|
|
||||||
})
|
})
|
||||||
|
|
||||||
return widget
|
return widget
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user