Debian Codegraph @

_demos/stress.go

raw symbols SCIP
1 //go:build ignore
2 // +build ignore
3
4 // Copyright 2025 The TCell Authors
5 //
6 // Licensed under the Apache License, Version 2.0 (the "License");
7 // you may not use this file except in compliance with the License.
8 // You may obtain a copy of the License at
9 //
10 // http://www.apache.org/licenses/LICENSE-2.0
11 //
12 // Unless required by applicable law or agreed to in writing, software
13 // distributed under the License is distributed on an "AS IS" BASIS,
14 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 // See the License for the specific language governing permissions and
16 // limitations under the License.
17
18 // stress will fill the whole screen with random characters, colors and
19 // formatting. The frames are pre-generated to draw as fast as possible.
20 // ESC and Ctrl-C will end the program. Note that resizing isn't supported.
21 package main
22
23 import (
24 "fmt"
25 "math/rand"
26 "time"
27
28 "github.com/gdamore/tcell/v3"
29 )
30
31 func main() {
32 screen, err := tcell.NewScreen()
33 if err != nil {
34 panic(err)
35 }
36 if err := screen.Init(); err != nil {
37 panic(err)
38 }
39
40 var frames int
41
42 type cell struct {
43 c string
44 style tcell.Style
45 }
46
47 width, height := screen.Size()
48 glyphs := []string{"@", "#", "&", "*", "=", "%", "Z", "A"}
49 attrs := []tcell.AttrMask{tcell.AttrBold, tcell.AttrReverse, tcell.AttrItalic, tcell.AttrNone}
50
51 // Pre-Generate 100 different frame patterns, so we stress the terminal as
52 // much as possible :D
53 var patterns [][][]*cell
54 for i := 0; i < 100; i++ {
55 pattern := make([][]*cell, height)
56 for h := 0; h < height; h++ {
57 row := make([]*cell, width)
58 for w := 0; w < width; w++ {
59 rF := int32(rand.Int() % 256)
60 gF := int32(rand.Int() % 256)
61 bF := int32(rand.Int() % 256)
62 rB := int32(rand.Int() % 256)
63 gB := int32(rand.Int() % 256)
64 bB := int32(rand.Int() % 256)
65
66 row[w] = &cell{
67 c: glyphs[rand.Int()%len(glyphs)],
68 style: tcell.StyleDefault.
69 Attributes(attrs[rand.Int()%len(attrs)]).
70 Background(tcell.NewRGBColor(rB, gB, bB)).
71 Foreground(tcell.NewRGBColor(rF, gF, bF)),
72 }
73 }
74 pattern[h] = row
75 }
76 patterns = append(patterns, pattern)
77 }
78
79 startTime := time.Now()
80 loop:
81 for {
82 select {
83 case event := <-screen.EventQ():
84 if event, ok := event.(*tcell.EventKey); ok {
85 if event.Key() == tcell.KeyCtrlC || event.Key() == tcell.KeyESC {
86 break loop
87 }
88 }
89 default:
90 }
91 pattern := patterns[frames%len(patterns)]
92 for h := 0; h < height; h++ {
93 for w := 0; w < width; w++ {
94 c := pattern[h][w]
95 screen.Put(w, h, c.c, c.style)
96 }
97 }
98 screen.Show()
99 frames++
100 }
101 duration := time.Since(startTime)
102 screen.Fini()
103 fps := int(float64(frames) / duration.Seconds())
104 fmt.Println("FPS:", fps)
105 }