Skip to content

Instantly share code, notes, and snippets.

@bisco
Created December 14, 2017 13:33
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 bisco/b21a39e7d9fdac1b05d51585a4c736ca to your computer and use it in GitHub Desktop.
Save bisco/b21a39e7d9fdac1b05d51585a4c736ca to your computer and use it in GitHub Desktop.
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, v := range runes {
termbox.SetCell(x+i, y, v, color, backgroundColor)
}
}
func drawLineColor(x, y int, str string, fg, bg termbox.Attribute) {
runes := []rune(str)
for i, v := range runes {
termbox.SetCell(x+i, y, v, fg, bg)
}
}
func draw() {
termbox.Clear(termbox.ColorDefault, termbox.ColorDefault)
drawLine(0, 0, "Press ESC to exit")
for i := 0; i < 16; i++ {
for j := 0; j < 16; j++ {
drawLineColor(j*4, i+3, fmt.Sprintf("%3d", i*16+j), termbox.Attribute(255-(i*16+j)), termbox.Attribute(i*16+j))
}
}
termbox.Flush()
}
func key(ev termbox.Event) bool {
switch ev.Type {
case termbox.EventKey:
switch ev.Key {
case termbox.KeyEsc:
return false
default:
draw()
}
default:
draw()
}
return true
}
func pollEvent() {
evc := make(chan termbox.Event)
go func() {
for {
evc <- termbox.PollEvent()
}
}()
draw()
for {
select {
case ev := <-evc:
if !key(ev) {
return
}
case <-time.After(1 * time.Second):
draw()
}
}
}
func main() {
err := termbox.Init()
if err != nil {
panic(err)
}
defer termbox.Close()
termbox.SetOutputMode(termbox.Output256)
pollEvent()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment