Skip to content

Instantly share code, notes, and snippets.

@mattak
Created January 3, 2015 14:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mattak/fc521a924ef9afc6ed97 to your computer and use it in GitHub Desktop.
Save mattak/fc521a924ef9afc6ed97 to your computer and use it in GitHub Desktop.
Show current time using termbox-go
package main
import (
"fmt"
"github.com/nsf/termbox-go"
"time"
)
func drawLine(x, y int, str string) {
color := termbox.ColorDefault
backgroundColor := termbox.ColorDefault
runes := []rune(str)
for i := 0; i < len(runes); i += 1 {
termbox.SetCell(x+i, y, runes[i], color, backgroundColor)
}
}
func draw() {
termbox.Clear(termbox.ColorDefault, termbox.ColorDefault)
drawLine(0, 0, "Press ESC to exit.")
drawLine(2, 1, fmt.Sprintf("date: %v", time.Now()))
termbox.Flush()
}
func pollEvent() {
draw()
for {
switch ev := termbox.PollEvent(); ev.Type {
case termbox.EventKey:
switch ev.Key {
case termbox.KeyEsc:
return
default:
draw()
}
default:
draw()
}
}
}
func main() {
err := termbox.Init()
if err != nil {
panic(err)
}
defer termbox.Close()
pollEvent()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment