mirror of https://github.com/gdamore/tcell.git
fix stress demo (race on close)
This commit is contained in:
parent
2c305c06d7
commit
b02dac3826
|
@ -23,7 +23,6 @@ package main
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"math/rand"
|
"math/rand"
|
||||||
"os"
|
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/gdamore/tcell/v2"
|
"github.com/gdamore/tcell/v2"
|
||||||
|
@ -38,28 +37,8 @@ func main() {
|
||||||
panic(err)
|
panic(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
startTime := time.Now()
|
|
||||||
var frames int
|
var frames int
|
||||||
|
|
||||||
go func() {
|
|
||||||
for {
|
|
||||||
event := screen.PollEvent()
|
|
||||||
if event, ok := event.(*tcell.EventKey); ok {
|
|
||||||
if event.Key() == tcell.KeyCtrlC || event.Key() == tcell.KeyESC {
|
|
||||||
break
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
screen.Fini()
|
|
||||||
|
|
||||||
duration := time.Since(startTime)
|
|
||||||
fps := int(float64(frames) / duration.Seconds())
|
|
||||||
fmt.Println("FPS:", fps)
|
|
||||||
|
|
||||||
os.Exit(0)
|
|
||||||
}()
|
|
||||||
|
|
||||||
type cell struct {
|
type cell struct {
|
||||||
c rune
|
c rune
|
||||||
style tcell.Style
|
style tcell.Style
|
||||||
|
@ -97,17 +76,36 @@ func main() {
|
||||||
patterns = append(patterns, pattern)
|
patterns = append(patterns, pattern)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
evCh := make(chan tcell.Event)
|
||||||
|
quitCh := make(chan struct{})
|
||||||
|
|
||||||
|
go screen.ChannelEvents(evCh, quitCh)
|
||||||
|
startTime := time.Now()
|
||||||
|
loop:
|
||||||
for {
|
for {
|
||||||
for i := 0; i < len(patterns); i++ {
|
select {
|
||||||
pattern := patterns[i]
|
case event := <-evCh:
|
||||||
for h := 0; h < height; h++ {
|
if event, ok := event.(*tcell.EventKey); ok {
|
||||||
for w := 0; w < width; w++ {
|
if event.Key() == tcell.KeyCtrlC || event.Key() == tcell.KeyESC {
|
||||||
c := pattern[h][w]
|
close(quitCh)
|
||||||
screen.SetContent(w, h, c.c, nil, c.style)
|
break loop
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
screen.Show()
|
break
|
||||||
frames++
|
default:
|
||||||
}
|
}
|
||||||
|
pattern := patterns[frames%len(patterns)]
|
||||||
|
for h := 0; h < height; h++ {
|
||||||
|
for w := 0; w < width; w++ {
|
||||||
|
c := pattern[h][w]
|
||||||
|
screen.SetContent(w, h, c.c, nil, c.style)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
screen.Show()
|
||||||
|
frames++
|
||||||
}
|
}
|
||||||
|
duration := time.Since(startTime)
|
||||||
|
screen.Fini()
|
||||||
|
fps := int(float64(frames) / duration.Seconds())
|
||||||
|
fmt.Println("FPS:", fps)
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue