2015-09-27 14:37:54 +08:00
|
|
|
// Copyright 2015 The TCell Authors
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the license at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
|
|
|
// boxes just displays random colored boxes on your terminal screen.
|
|
|
|
// Press ESC to exit the program.
|
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2015-10-03 12:49:13 +08:00
|
|
|
"fmt"
|
2015-09-27 14:37:54 +08:00
|
|
|
"math/rand"
|
|
|
|
"os"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/gdamore/tcell"
|
|
|
|
)
|
|
|
|
|
2015-10-03 12:49:13 +08:00
|
|
|
func makebox(s tcell.Screen) {
|
2015-09-27 14:37:54 +08:00
|
|
|
w, h := s.Size()
|
|
|
|
|
2015-09-29 13:28:41 +08:00
|
|
|
if w == 0 || h == 0 {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2015-10-04 02:29:55 +08:00
|
|
|
glyphs := []rune{'@', '#', '&', '*', '=', '%', 'Z', 'A'}
|
2015-10-03 12:49:13 +08:00
|
|
|
|
2015-09-27 14:37:54 +08:00
|
|
|
lx := rand.Int() % w
|
|
|
|
ly := rand.Int() % h
|
2015-10-03 12:49:13 +08:00
|
|
|
lw := rand.Int() % (w - lx)
|
|
|
|
lh := rand.Int() % (h - ly)
|
|
|
|
st := tcell.StyleDefault
|
|
|
|
gl := ' '
|
|
|
|
if s.Colors() > 1 {
|
|
|
|
st = st.Background(tcell.Color(rand.Int() % s.Colors()))
|
|
|
|
} else {
|
2015-10-04 02:29:55 +08:00
|
|
|
st = st.Reverse(rand.Int()%2 == 0)
|
|
|
|
gl = glyphs[rand.Int()%len(glyphs)]
|
2015-10-03 12:49:13 +08:00
|
|
|
}
|
2015-09-27 14:37:54 +08:00
|
|
|
|
|
|
|
for row := 0; row < lh; row++ {
|
|
|
|
for col := 0; col < lw; col++ {
|
2015-10-03 12:49:13 +08:00
|
|
|
s.SetCell(lx+col, ly+row, st, gl)
|
2015-09-27 14:37:54 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
s.Show()
|
|
|
|
}
|
|
|
|
|
|
|
|
func main() {
|
2015-10-28 23:22:22 +08:00
|
|
|
|
|
|
|
tcell.SetEncodingFallback(tcell.EncodingFallbackASCII)
|
2015-10-03 12:49:13 +08:00
|
|
|
s, e := tcell.NewScreen()
|
2015-09-27 14:37:54 +08:00
|
|
|
if e != nil {
|
2015-10-03 12:49:13 +08:00
|
|
|
fmt.Fprintf(os.Stderr, "%v\n", e)
|
|
|
|
os.Exit(1)
|
2015-09-27 14:37:54 +08:00
|
|
|
}
|
|
|
|
if e = s.Init(); e != nil {
|
2015-10-03 12:49:13 +08:00
|
|
|
fmt.Fprintf(os.Stderr, "%v\n", e)
|
|
|
|
os.Exit(1)
|
2015-09-27 14:37:54 +08:00
|
|
|
}
|
2015-10-03 12:49:13 +08:00
|
|
|
|
|
|
|
s.SetStyle(tcell.StyleDefault.
|
|
|
|
Foreground(tcell.ColorBlack).
|
|
|
|
Background(tcell.ColorWhite))
|
|
|
|
s.Clear()
|
|
|
|
|
|
|
|
quit := make(chan struct{})
|
2015-09-27 14:37:54 +08:00
|
|
|
go func() {
|
|
|
|
for {
|
|
|
|
ev := s.PollEvent()
|
|
|
|
switch ev := ev.(type) {
|
|
|
|
case *tcell.EventKey:
|
|
|
|
switch ev.Key() {
|
2015-10-03 12:49:13 +08:00
|
|
|
case tcell.KeyEscape, tcell.KeyEnter:
|
|
|
|
close(quit)
|
|
|
|
return
|
2015-09-27 14:37:54 +08:00
|
|
|
case tcell.KeyCtrlL:
|
|
|
|
s.Sync()
|
|
|
|
}
|
|
|
|
case *tcell.EventResize:
|
|
|
|
s.Sync()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
2015-10-03 12:49:13 +08:00
|
|
|
cnt := 0
|
|
|
|
dur := time.Duration(0)
|
|
|
|
loop:
|
2015-09-27 14:37:54 +08:00
|
|
|
for {
|
2015-10-03 12:49:13 +08:00
|
|
|
select {
|
|
|
|
case <-quit:
|
|
|
|
break loop
|
|
|
|
case <-time.After(time.Millisecond * 50):
|
|
|
|
}
|
|
|
|
start := time.Now()
|
2015-09-27 14:37:54 +08:00
|
|
|
makebox(s)
|
2015-10-03 12:49:13 +08:00
|
|
|
cnt++
|
|
|
|
dur += time.Now().Sub(start)
|
2015-09-27 14:37:54 +08:00
|
|
|
}
|
2015-10-03 12:49:13 +08:00
|
|
|
|
|
|
|
s.Fini()
|
|
|
|
fmt.Printf("Finished %d boxes in %s\n", cnt, dur)
|
|
|
|
fmt.Printf("Average is %0.3f ms / box\n", (float64(dur)/float64(cnt))/1000000.0)
|
2015-09-27 14:37:54 +08:00
|
|
|
}
|