Skip to content

Instantly share code, notes, and snippets.

@khubo
Created September 26, 2023 15:35
Show Gist options
  • Save khubo/d16fac1cb2e1e7cbbc778b12191f6ea6 to your computer and use it in GitHub Desktop.
Save khubo/d16fac1cb2e1e7cbbc778b12191f6ea6 to your computer and use it in GitHub Desktop.
package main
import (
"time"
"github.com/rivo/tview"
)
var app = tview.NewApplication()
var view = tview.NewModal().SetText("00:00:00")
func startTimer() func() {
done := make(chan int)
cancel := func() {
done <- 1
}
startTime := time.Now()
go func() {
for {
select {
case <-done:
return
case <-time.After(500 * time.Millisecond):
t2 := time.Now()
diff := t2.Sub(startTime)
out := time.Time{}.Add(diff)
app.QueueUpdateDraw(func() {
view.SetText(out.Format("15:04:05"))
})
}
}
}()
return cancel
}
func main() {
var cancelFunc func()
view.AddButtons([]string{"Start", "Reset", "Quit"}).
SetDoneFunc(func(buttonIndex int, buttonLabel string) {
switch buttonIndex {
case 0:
cancelFunc = startTimer()
case 1:
cancelFunc()
view.SetText("00:00:00")
case 2:
app.Stop()
}
})
err := app.SetRoot(view, false).EnableMouse(true).Run()
if err != nil {
panic(err)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment