tcell/_demos/boxes.go

119 lines
2.5 KiB
Go
Raw Normal View History

2020-08-26 07:20:58 +08:00
// +build ignore
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 (
"fmt"
2015-09-27 14:37:54 +08:00
"math/rand"
"os"
"time"
2020-08-26 07:20:58 +08:00
"github.com/gdamore/tcell/v2"
2015-09-27 14:37:54 +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-09-27 14:37:54 +08:00
lx := rand.Int() % w
ly := rand.Int() % h
lw := rand.Int() % (w - lx)
lh := rand.Int() % (h - ly)
st := tcell.StyleDefault
gl := ' '
if s.Colors() > 256 {
rgb := tcell.NewHexColor(int32(rand.Int() & 0xffffff))
st = st.Background(rgb)
} else if s.Colors() > 1 {
st = st.Background(tcell.Color(rand.Int() % s.Colors()) | tcell.ColorValid)
} else {
2015-10-04 02:29:55 +08:00
st = st.Reverse(rand.Int()%2 == 0)
gl = glyphs[rand.Int()%len(glyphs)]
}
2015-09-27 14:37:54 +08:00
for row := 0; row < lh; row++ {
for col := 0; col < lw; col++ {
s.SetCell(lx+col, ly+row, st, gl)
2015-09-27 14:37:54 +08:00
}
}
s.Show()
}
func main() {
tcell.SetEncodingFallback(tcell.EncodingFallbackASCII)
s, e := tcell.NewScreen()
2015-09-27 14:37:54 +08:00
if e != nil {
fmt.Fprintf(os.Stderr, "%v\n", e)
os.Exit(1)
2015-09-27 14:37:54 +08:00
}
if e = s.Init(); e != nil {
fmt.Fprintf(os.Stderr, "%v\n", e)
os.Exit(1)
2015-09-27 14:37:54 +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() {
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()
}
}
}()
cnt := 0
dur := time.Duration(0)
loop:
2015-09-27 14:37:54 +08:00
for {
select {
case <-quit:
break loop
case <-time.After(time.Millisecond * 50):
}
start := time.Now()
2015-09-27 14:37:54 +08:00
makebox(s)
cnt++
dur += time.Now().Sub(start)
2015-09-27 14:37:54 +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
}