1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69
|
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")
}
|