2023-08-17 23:10:09 +00:00
|
|
|
package clock
|
|
|
|
|
|
|
|
import (
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/gdamore/tcell/v2"
|
|
|
|
"github.com/rivo/tview"
|
|
|
|
)
|
|
|
|
|
2023-08-18 16:52:40 +00:00
|
|
|
func New(format string, interval time.Duration) *tview.TextView {
|
2023-08-17 23:10:09 +00:00
|
|
|
view := tview.NewTextView()
|
|
|
|
view.SetTextAlign(tview.AlignCenter)
|
|
|
|
view.SetBackgroundColor(tcell.ColorDefault)
|
|
|
|
view.SetText(time.Now().Format(format))
|
|
|
|
view.SetBorderPadding(1, 1, 1, 1)
|
2023-08-18 16:52:40 +00:00
|
|
|
go refresh(view, format, interval)
|
2023-08-17 23:10:09 +00:00
|
|
|
return view
|
|
|
|
}
|
|
|
|
|
2023-08-18 16:52:40 +00:00
|
|
|
func refresh(view *tview.TextView, format string, interval time.Duration) {
|
2023-08-17 23:10:09 +00:00
|
|
|
tick := time.NewTicker(interval)
|
|
|
|
|
2023-08-18 11:17:46 +00:00
|
|
|
for range tick.C {
|
|
|
|
view.SetText(time.Now().Format(format))
|
2023-08-17 23:10:09 +00:00
|
|
|
}
|
|
|
|
}
|