| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
package main |
| 22 |
|
| 23 |
import ( |
| 24 |
"fmt" |
| 25 |
"math/rand" |
| 26 |
"os" |
| 27 |
"time" |
| 28 |
|
| 29 |
"github.com/gdamore/tcell/v3" |
| 30 |
"github.com/gdamore/tcell/v3/color" |
| 31 |
) |
| 32 |
|
| 33 |
var red = int32(rand.Int() % 256) |
| 34 |
var grn = int32(rand.Int() % 256) |
| 35 |
var blu = int32(rand.Int() % 256) |
| 36 |
var inc = int32(8) |
| 37 |
var redi = int32(inc) |
| 38 |
var grni = int32(inc) |
| 39 |
var blui = int32(inc) |
| 40 |
var interval = time.Millisecond * 50 |
| 41 |
|
| 42 |
func makeBox(s tcell.Screen) { |
| 43 |
w, h := s.Size() |
| 44 |
|
| 45 |
if w == 0 || h == 0 { |
| 46 |
return |
| 47 |
} |
| 48 |
|
| 49 |
glyphs := []string{"@", "#", "&", "*", "=", "%", "Z", "A"} |
| 50 |
|
| 51 |
lh := h / 2 |
| 52 |
lw := w / 2 |
| 53 |
lx := w / 4 |
| 54 |
ly := h / 4 |
| 55 |
st := tcell.StyleDefault |
| 56 |
gl := " " |
| 57 |
|
| 58 |
if s.Colors() == 0 { |
| 59 |
st = st.Reverse(rand.Int()%2 == 0) |
| 60 |
gl = glyphs[rand.Int()%len(glyphs)] |
| 61 |
} else { |
| 62 |
|
| 63 |
red += redi |
| 64 |
if (red >= 256) || (red < 0) { |
| 65 |
redi = -redi |
| 66 |
red += redi |
| 67 |
} |
| 68 |
grn += grni |
| 69 |
if (grn >= 256) || (grn < 0) { |
| 70 |
grni = -grni |
| 71 |
grn += grni |
| 72 |
} |
| 73 |
blu += blui |
| 74 |
if (blu >= 256) || (blu < 0) { |
| 75 |
blui = -blui |
| 76 |
blu += blui |
| 77 |
|
| 78 |
} |
| 79 |
st = st.Background(tcell.NewRGBColor(red, grn, blu)) |
| 80 |
} |
| 81 |
for row := range lh { |
| 82 |
for col := range lw { |
| 83 |
s.Put(lx+col, ly+row, gl, st) |
| 84 |
} |
| 85 |
} |
| 86 |
s.Show() |
| 87 |
} |
| 88 |
|
| 89 |
func flipCoin() bool { |
| 90 |
if rand.Int()&1 == 0 { |
| 91 |
return false |
| 92 |
} |
| 93 |
return true |
| 94 |
} |
| 95 |
|
| 96 |
func main() { |
| 97 |
|
| 98 |
s, e := tcell.NewScreen() |
| 99 |
if e != nil { |
| 100 |
fmt.Fprintf(os.Stderr, "%v\n", e) |
| 101 |
os.Exit(1) |
| 102 |
} |
| 103 |
if e = s.Init(); e != nil { |
| 104 |
fmt.Fprintf(os.Stderr, "%v\n", e) |
| 105 |
os.Exit(1) |
| 106 |
} |
| 107 |
|
| 108 |
s.SetStyle(tcell.StyleDefault. |
| 109 |
Foreground(color.Black). |
| 110 |
Background(color.White)) |
| 111 |
s.Clear() |
| 112 |
|
| 113 |
quit := make(chan struct{}) |
| 114 |
go func() { |
| 115 |
for { |
| 116 |
ev := <-s.EventQ() |
| 117 |
switch ev := ev.(type) { |
| 118 |
case *tcell.EventKey: |
| 119 |
switch ev.Key() { |
| 120 |
case tcell.KeyEscape, tcell.KeyEnter, tcell.KeyCtrlQ: |
| 121 |
close(quit) |
| 122 |
return |
| 123 |
case tcell.KeyCtrlL: |
| 124 |
s.Sync() |
| 125 |
} |
| 126 |
case *tcell.EventResize: |
| 127 |
s.Sync() |
| 128 |
} |
| 129 |
} |
| 130 |
}() |
| 131 |
|
| 132 |
cnt := 0 |
| 133 |
dur := time.Duration(0) |
| 134 |
loop: |
| 135 |
for { |
| 136 |
select { |
| 137 |
case <-quit: |
| 138 |
break loop |
| 139 |
case <-time.After(interval): |
| 140 |
} |
| 141 |
start := time.Now() |
| 142 |
makeBox(s) |
| 143 |
dur += time.Since(start) |
| 144 |
cnt++ |
| 145 |
if cnt%(256/int(inc)) == 0 { |
| 146 |
if flipCoin() { |
| 147 |
redi = -redi |
| 148 |
} |
| 149 |
if flipCoin() { |
| 150 |
grni = -grni |
| 151 |
} |
| 152 |
if flipCoin() { |
| 153 |
blui = -blui |
| 154 |
} |
| 155 |
} |
| 156 |
} |
| 157 |
|
| 158 |
s.Fini() |
| 159 |
fmt.Printf("Finished %d boxes in %s\n", cnt, dur) |
| 160 |
fmt.Printf("Average is %0.3f ms / box\n", (float64(dur)/float64(cnt))/1000000.0) |
| 161 |
} |