1
0
This repository has been archived on 2023-12-27. You can view files and clone it, but cannot push or open issues or pull requests.
beginning-go/vendor/github.com/nsf/termbox-go/_demos/interrupt.go
2017-10-26 10:16:01 -07:00

70 lines
1.2 KiB
Go

package main
import (
"fmt"
"github.com/nsf/termbox-go"
"time"
)
func tbPrint(x, y int, fg, bg termbox.Attribute, msg string) {
for _, c := range msg {
termbox.SetCell(x, y, c, fg, bg)
x++
}
}
func draw(i int) {
termbox.Clear(termbox.ColorDefault, termbox.ColorDefault)
defer termbox.Flush()
w, h := termbox.Size()
s := fmt.Sprintf("count = %d", i)
tbPrint((w/2)-(len(s)/2), h/2, termbox.ColorRed, termbox.ColorDefault, s)
}
func main() {
err := termbox.Init()
if err != nil {
panic(err)
}
termbox.SetInputMode(termbox.InputEsc)
go func() {
time.Sleep(5 * time.Second)
termbox.Interrupt()
// This should never run - the Interrupt(), above, should cause the event
// loop below to exit, which then exits the process. If something goes
// wrong, this panic will trigger and show what happened.
time.Sleep(1 * time.Second)
panic("this should never run")
}()
var count int
draw(count)
mainloop:
for {
switch ev := termbox.PollEvent(); ev.Type {
case termbox.EventKey:
if ev.Ch == '+' {
count++
} else if ev.Ch == '-' {
count--
}
case termbox.EventError:
panic(ev.Err)
case termbox.EventInterrupt:
break mainloop
}
draw(count)
}
termbox.Close()
fmt.Println("Finished")
}