Skip to content

Instantly share code, notes, and snippets.

@lskbr
Created May 24, 2019 16:58
Show Gist options
  • Save lskbr/9fc28e999ad414edfaf8aef07eb316a9 to your computer and use it in GitHub Desktop.
Save lskbr/9fc28e999ad414edfaf8aef07eb316a9 to your computer and use it in GitHub Desktop.
package main
import (
"fmt"
"os"
"time"
"github.com/gdamore/tcell"
)
func printAt(s tcell.Screen, x int, y int, message string) {
var mrune []rune
for _, r := range message {
mrune = append(mrune, r)
}
s.SetContent(x, y, mrune[0], mrune[1:], tcell.StyleDefault)
}
func initScreen() tcell.Screen {
s, e := tcell.NewScreen()
if e != nil {
fmt.Fprintf(os.Stderr, "%v\n", e)
os.Exit(1)
}
if e = s.Init(); e != nil {
fmt.Fprintf(os.Stderr, "%v\n", e)
os.Exit(1)
}
return s
}
func main() {
s := initScreen()
s.SetStyle(tcell.StyleDefault.
Foreground(tcell.ColorBlack).
Background(tcell.ColorWhite))
s.Clear()
w, h := s.Size()
quit := make(chan struct{})
go func() {
for {
ev := s.PollEvent()
switch ev := ev.(type) {
case *tcell.EventKey:
switch ev.Key() {
case tcell.KeyEscape, tcell.KeyEnter:
close(quit)
return
case tcell.KeyCtrlL:
s.Sync()
}
case *tcell.EventResize:
s.Clear()
s.Sync()
w, h = s.Size()
}
}
}()
x, y := 0, 0
xd, yd := 1, 1
m := "Hello GO"
lm := len(m)
loop:
for {
var p = fmt.Sprintf("w=%d h=%d x=%d y=%d ", w, h, x, y)
printAt(s, x, y, m)
printAt(s, 0, 0, p)
s.Show()
select {
case <-quit:
break loop
case <-time.After(time.Millisecond * 50):
}
//s.Clear()
//s.Show()
if x < 0 {
x = -1
xd = 1
} else if x >= w-lm {
x = w - lm - 1
xd = -1
}
x += xd
if y < 0 {
yd = 1
y = 0
} else if y > h-1 {
y = h - 1
yd = -1
}
y += yd
}
s.Fini()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment