Implemented asynchronous event polling

master
Michael Dvorkin 12 years ago
parent beece9c4b0
commit 5ace0edc38
  1. 27
      lib/screen.go
  2. 50
      mop.go

@ -3,11 +3,10 @@
package mop
import (
"fmt"
"github.com/michaeldv/just"
"github.com/nsf/termbox-go"
"regexp"
"strings"
"github.com/michaeldv/just"
"github.com/nsf/termbox-go"
)
// Can combine attributes and a single color using bitwise OR.
@ -37,24 +36,6 @@ func Draw(stocks string) {
drawScreen(Format(message))
}
//-----------------------------------------------------------------------------
func Refresh(profile string) {
loop:
for {
switch ev := termbox.PollEvent(); ev.Type {
case termbox.EventKey:
if ev.Key == termbox.KeyEsc {
break loop
}
case termbox.EventResize:
// Draw(profile)
// x, y := termbox.Size()
str := fmt.Sprintf("(%d:%d)", ev.Width, ev.Height)
drawScreen(str + ": <red>Hello world</red>, how <white>are</white> <blue>you?</blue>")
}
}
}
//
// Return regular expression that matches all possible color tags, i.e.
// </?black>|</?red>| ... |</?white>
@ -121,3 +102,7 @@ func drawScreen(str string) {
}
termbox.Flush()
}
func DrawScreen(str string) {
drawScreen(str)
}

@ -3,20 +3,60 @@
package main
import (
"fmt"
"time"
"github.com/michaeldv/mop/lib"
"github.com/nsf/termbox-go"
)
//-----------------------------------------------------------------------------
func main() {
profile := mop.LoadProfile()
func initTermbox() {
err := termbox.Init()
if err != nil {
panic(err)
}
defer termbox.Close()
}
//-----------------------------------------------------------------------------
func mainLoop(profile string) {
event_queue := make(chan termbox.Event)
event_tick := time.NewTicker(1 * time.Second)
go func() {
for {
event_queue <- termbox.PollEvent()
}
}()
mop.Draw(profile)
mop.Refresh(profile)
loop:
for {
select {
case event := <- event_queue:
switch event.Type {
case termbox.EventKey:
if event.Key == termbox.KeyEsc {
break loop
}
case termbox.EventResize:
// Draw(profile)
// x, y := termbox.Size()
str := fmt.Sprintf("(%d:%d)", event.Width, event.Height)
mop.DrawScreen(str + ": <red>Hello world</red>, how <white>are</white> <blue>you?</blue>")
}
case <-event_tick.C:
mop.DrawScreen(time.Now().Format("3:04:05pm PST"))
//mop.Draw(profile)
}
}
}
//-----------------------------------------------------------------------------
func main() {
initTermbox()
defer termbox.Close()
profile := mop.LoadProfile()
mainLoop(profile)
}

Loading…
Cancel
Save