Created
January 3, 2015 14:54
-
-
Save mattak/fc521a924ef9afc6ed97 to your computer and use it in GitHub Desktop.
Show current time using termbox-go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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