-
Notifications
You must be signed in to change notification settings - Fork 0
/
screen.go
70 lines (55 loc) · 987 Bytes
/
screen.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
package main
import (
"image"
"image/png"
"os"
)
type Screen struct {
cells [][]*Color
W int
H int
}
func NewScreen(w, h int) *Screen {
s := &Screen{
cells: make([][]*Color, h),
W: w,
H: h,
}
for y, _ := range s.cells {
s.cells[y] = make([]*Color, w)
}
return s
}
func (s *Screen) Push() {
final := ""
for _, row := range s.cells {
for _, pix := range row {
final += pix.ToChar()
}
final += "\n"
}
os.Stdout.Write([]byte("\033[?25l" + "\033[0;0H" + final))
}
func (s *Screen) PushFile(to string) {
rendered := image.NewRGBA(image.Rectangle{
Min: image.Point{X: 0, Y: 0},
Max: image.Point{X: s.W, Y: s.H},
})
for y, row := range s.cells {
for x, pix := range row {
rendered.Set(x, y, pix.ToRGBA())
}
}
file, err := os.Create(to)
if err != nil {
panic(err)
}
err = png.Encode(file, rendered)
if err != nil {
panic(err)
}
file.Close()
}
func (s *Screen) Set(x, y int, c *Color) {
s.cells[y][x] = c
}