58 lines
1.4 KiB
Go
58 lines
1.4 KiB
Go
package widgets
|
|
|
|
import (
|
|
"ArinDash/util"
|
|
"context"
|
|
"strings"
|
|
"time"
|
|
|
|
"github.com/mum4k/termdash/align"
|
|
"github.com/mum4k/termdash/cell"
|
|
"github.com/mum4k/termdash/terminal/terminalapi"
|
|
"github.com/mum4k/termdash/widgetapi"
|
|
"github.com/mum4k/termdash/widgets/segmentdisplay"
|
|
)
|
|
|
|
type ClockOptions struct {
|
|
}
|
|
|
|
func Clock() ClockOptions {
|
|
widgetOptions["ClockOptions"] = createClock
|
|
return ClockOptions{}
|
|
|
|
}
|
|
|
|
func createClock(ctx context.Context, _ terminalapi.Terminal, _ interface{}) widgetapi.Widget {
|
|
widget := util.PanicOnErrorWithResult(segmentdisplay.New())
|
|
|
|
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()
|
|
nowStr := now.Format("15 04")
|
|
parts := strings.Split(nowStr, " ")
|
|
|
|
spacer := " "
|
|
if now.Second()%2 == 0 {
|
|
spacer = ":"
|
|
}
|
|
chunks := []*segmentdisplay.TextChunk{
|
|
segmentdisplay.NewChunk(parts[0], segmentdisplay.WriteCellOpts(cell.FgColor(cell.ColorWhite))),
|
|
segmentdisplay.NewChunk(spacer),
|
|
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 {
|
|
panic(err)
|
|
}
|
|
|
|
case <-ctx.Done():
|
|
return nil
|
|
}
|
|
}
|
|
})
|
|
return widget
|
|
}
|