-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsmatrix.go
234 lines (192 loc) · 5.35 KB
/
smatrix.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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
package main
import ("fmt"
//"bufio"
"os"
/*"io"
"bytes"*/
"flag"
"time"
"math/rand"
"unsafe"
"syscall"
"os/signal"
"strconv"
)
/******************************************************************************/
// MARK: - Consts and Structures
const (
ESC_SAVE_SCREEN = "?47h"
ESC_RESTORE_SCREEN = "?47l"
ESC_SAVE_CURSOR = "s"
ESC_RESTORE_CURSOR = "u"
ESC_BOLD_ON = "1m"
ESC_BOLD_OFF = "0m"
ESC_CURSOR_ON = "?25h"
ESC_CURSOR_OFF = "?25l"
ESC_CLEAR_SCREEN = "2J"
ESC_CLEAR_LINE = "2K"
)
/****/
type winsize struct {
Row uint16
Col uint16
Xpixel uint16
Ypixel uint16
}
/******************************************************************************/
// MARK: - Terminal Functions
func getWidth() uint {
ws := &winsize{}
retCode, _, errno := syscall.Syscall(syscall.SYS_IOCTL,
uintptr(syscall.Stdin),
uintptr(syscall.TIOCGWINSZ),
uintptr(unsafe.Pointer(ws)))
if int(retCode) == -1 {
panic(errno)
}
return uint(ws.Col)
}
func getHeight() uint {
ws := &winsize{}
retCode, _, errno := syscall.Syscall(syscall.SYS_IOCTL,
uintptr(syscall.Stdin),
uintptr(syscall.TIOCGWINSZ),
uintptr(unsafe.Pointer(ws)))
if int(retCode) == -1 {
panic(errno)
}
return uint(ws.Row)
}
func TerminalCommand(command string) {
fmt.Printf("\033[%s", command)
}
func PrintAt(x, y int, text string) {
fmt.Printf("\033[%d;%dH%s", x, y, text)
}
func FormatAt(x,y int, text string) string {
return fmt.Sprintf("\033[%d;%dH%s", x, y, text)
}
func CommandAdd(command, text string) string {
return fmt.Sprintf("\033[%s%s", command, text)
}
func CommandWrap(begin, text, end string) string {
return fmt.Sprintf("\033[%s%s\033[%s", begin, text, end)
}
func ColorText(text string, color int) string {
encoded := fmt.Sprintf("\033[0;%dm%s\033[0m", color, text)
return encoded
}
/******************************************************************************/
// - MARK: Functions
/** create a string containing a number on the base 36 scale */
func letter(n int64) string {
return strconv.FormatInt(n, 36)
}
func run(n int, c chan string) {
loopcount := 1
for {
r := rand.Intn(42)//0<letters<36<spaces<50
r = rand.Intn(128-28)+36
rs := " "
//if r <= 35 {
if 32 < r && r < 128{
//rs = letter(int64(r))
rs = string(r)
c <- rs
} else {
//add some spaces
space_space := rand.Intn(16) + 16
for sp:=0; sp<space_space; sp++ {
c <- " "
}
}
loopcount += 1
}
}
func PaintScreen (screen [][]string) {
for row:=0 ; row<int(getHeight()); row++ {
for col:=0 ; col<int(getWidth()); col++ {
msg := FormatAt(row, col, screen[row][col])
r := rand.Intn(10)
if r==1 {
msg = CommandWrap(ESC_BOLD_ON, msg, ESC_BOLD_OFF)
} else if r==2 || r==3 {
msg = ColorText(msg, 0)
}
msg = ColorText(msg, 32)
fmt.Println(msg)
}
}
}
/******************************************************************************/
// MARK: - Application Functions
// Handle ctr-c events
func HandleControlC() {
c := make(chan os.Signal)
signal.Notify(c, os.Interrupt, syscall.SIGTERM)
go func() {
<-c
fmt.Printf("\033[2K\r") //clear line and move back to beginning
fmt.Printf("\n\033[2K\r") //clear line and move back to beginning
fmt.Printf("\n\033[2K\r") //clear line and move back to beginning
fmt.Printf("%dx%d\n", getWidth(), getHeight())
Shutdown()
os.Exit(0)
}()
}
func Startup() {
TerminalCommand(ESC_SAVE_SCREEN)
TerminalCommand(ESC_CURSOR_OFF)
TerminalCommand(ESC_CLEAR_SCREEN)
}
func Shutdown() {
TerminalCommand(ESC_CURSOR_ON)
TerminalCommand(ESC_RESTORE_SCREEN)
}
func main() {
//bothMode := flag.Bool("both", true, "display both width and height")
//heightMode := flag.Bool("height", false, "height mode")
//widthMode := flag.Bool("width", false, "width mode")
flag.Parse()
HandleControlC()
var c chan string = make(chan string)
Startup()
defer Shutdown() //defer this just in case
//setup the columns
var i uint
for i=0; i<getWidth(); i++ {
go run(int(i), c)
break
}
//draw a horizontal line down the left
for i=0; i<getHeight();i++ {
fmt.Println(" ")
}
screen := make([][]string, getHeight())
for i := range(screen) {
screen[i] = make([]string,getWidth())
}
/*****************/
// -- Main Loop --
//columns first
col := 1
for {
//dont move all columns down
if rand.Intn(10)<4 {
for row := int(getHeight()-1) ; 0<row; row-- {
if row==1 {
msg := <- c
screen[row][col] = msg
} else {
screen[row][col] = screen[row-1][col]
}
}
}
col += 2
if int(getWidth())<=col {
col = 1
PaintScreen(screen)
time.Sleep(time.Millisecond * 250)
}
}
}