-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrain.go
120 lines (112 loc) · 2.25 KB
/
rain.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
package main
import (
"fmt"
"math/rand"
"runtime"
"sync"
"time"
"golang.org/x/term"
)
var charset = "@#$%&?ABCDEF0123456789"
func s(format string, args ...interface{}) string {
return fmt.Sprintf(format, args...)
}
type Tty struct{}
type Glyph struct {
r string
x uint
y uint
v int
}
func clear_screen() {
fmt.Print("\x1b[2J")
}
func cursor_to_position(x uint, y uint) {
fmt.Printf("\x1b[%d;%dH", y+1, x+1)
}
func get_screen() (int, int) {
w, h, err := term.GetSize(0)
if err != nil {
return 0, 0
}
return w, h
}
func main() {
runtime.LockOSThread()
fmt.Printf("\x1b[?25l")
defer func() {
fmt.Printf("\x1b[?25h")
}()
w, h := get_screen()
clear_screen()
glyphs := make([]*Glyph, (w * h))
for y := 0; y < h; y++ {
for x := 0; x < w; x++ {
glyphs[x+(w*y)] = &Glyph{
r: " ",
x: uint(x),
y: uint(y),
v: 0,
}
cursor_to_position(uint(x), uint(y))
fmt.Printf("%s", s("\x1b[38;2;0;0;0m%s", glyphs[x+(w*y)].r))
}
}
wg := sync.WaitGroup{}
wg.Add(1)
lck := sync.Mutex{}
charset_len := len(charset)
go func() {
for {
cursor_to_position(0, 0)
for y := 0; y < h; y++ {
for x := 0; x < w; x++ {
lck.Lock()
idx := x + (w * y)
g := glyphs[idx]
g.v -= 1
if g.v <= 0 {
g.v = 0
}
if rand.Intn(10) == 9 {
r := rand.Intn(charset_len)
g.r = charset[r : r+1]
}
glyphs[idx] = g
lck.Unlock()
cursor_to_position(uint(x), uint(y))
r, b := 0, 0
if g.v > 250 {
r, b = g.v-50, g.v-50
}
fmt.Printf("%s", s("\x1b[38;2;%d;%d;%dm%s", r, g.v, b, g.r))
time.Sleep(100 * time.Nanosecond) // give the tty a little time to catch up to us.
}
cursor_to_position(0, uint(y))
}
time.Sleep(16 * time.Millisecond) // 1000ms / 60fps = 16.666666
}
}()
wg.Add(1)
go func() {
for {
go func() {
x := rand.Intn(w)
s := rand.Intn(250) + 50
for y := 0; y < h; y++ {
r := rand.Intn(charset_len)
lck.Lock()
idx := x + (w * y)
g := glyphs[idx]
g.r = charset[r : r+1]
g.v = 255
glyphs[idx] = g
lck.Unlock()
time.Sleep(time.Duration(s) * time.Millisecond) // look like it's struggling...
}
}()
time.Sleep(100 * time.Millisecond) // don't spawn too many too fast...
}
}()
wg.Wait()
}