/
main.go
268 lines (249 loc) · 7.57 KB
/
main.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
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
package main
import (
"bytes"
"context"
"fmt"
"image"
_ "image/jpeg"
_ "image/png"
"io"
"io/ioutil"
"log"
"os"
"sync"
"github.com/badu/term"
"github.com/badu/term/color"
"github.com/badu/term/core"
enc "github.com/badu/term/encoding"
"github.com/badu/term/geom"
"github.com/badu/term/key"
initLog "github.com/badu/term/log"
)
type page struct {
sync.Once
incomingMouse chan term.MouseEvent // We need a channel on which we will listen for incoming events
incomingKey chan term.KeyEvent // We need a channel on which we will listen for incoming events
incomingResize chan term.ResizeEvent // We need a channel on which we will listen for incoming events
died chan struct{} // this is a buffered channel of size one
engine term.Engine //
image [][][2]color.Color
pixels [][]term.Pixel
imageOffsetRow int
imageOffsetColumn int
imageWidth int
imageHeight int
size *term.Size
}
func (p *page) MouseListen() chan term.MouseEvent {
return p.incomingMouse
}
func (p *page) KeyListen() chan term.KeyEvent {
return p.incomingKey
}
func (p *page) ResizeListen() chan term.ResizeEvent {
return p.incomingResize
}
func (p *page) DyingChan() chan struct{} {
return p.died
}
func (p *page) adjustAndRegisterPixels(ev term.ResizeEvent) {
p.size = ev.Size()
p.init()
}
func (p *page) init() {
getters := make([]term.PixelGetter, 0)
p.pixels = make([][]term.Pixel, p.size.Columns)
imageColumn := p.imageOffsetColumn
for column := 0; column < p.size.Columns; column++ {
imageRow := p.imageOffsetRow
p.pixels[column] = make([]term.Pixel, p.size.Rows)
for row := 0; row < p.size.Rows; row++ {
fg := geom.WithForeground(p.image[imageColumn][imageRow][0])
bg := geom.WithBackground(p.image[imageColumn][imageRow][1])
px, _ := geom.NewPixel(geom.WithRune('▀'), fg, bg, geom.WithPosition(term.NewPosition(column, row)))
getters = append(getters, px)
p.pixels[column][row] = px
imageRow++
}
imageColumn++
}
p.engine.ActivePixels(getters)
}
func (p *page) redraw() {
imageRow := p.imageOffsetRow
for row := 0; row < p.size.Rows; row++ {
imageColumn := p.imageOffsetColumn
for column := 0; column < p.size.Columns; column++ {
p.pixels[column][row].SetFgBg(p.image[imageColumn][imageRow][0], p.image[imageColumn][imageRow][1])
imageColumn++
}
imageRow++
}
p.engine.HideCursor()
}
// listen listens for incoming events
func (p *page) lifeCycle(ctx context.Context, cancel func()) {
p.Once.Do(
func() {
p.size = p.engine.Size()
p.engine.HideCursor() // hide cursor
p.engine.Clear() // clear
log.Printf("initial screen size w = %03d h = %03d", p.size.Columns, p.size.Rows)
imageWidth := len(p.image)
imageHeight := len(p.image[0])
p.imageOffsetRow = (imageHeight - p.size.Rows) / 2
p.imageOffsetColumn = (imageWidth - p.size.Columns) / 2
log.Printf("loaded image size %04d,%04d", imageWidth, imageHeight)
log.Printf("displaying %04d,%04d -> %04d,%04d", p.imageOffsetRow, p.imageOffsetColumn, p.imageOffsetRow+p.size.Columns, p.imageOffsetColumn+p.size.Rows)
p.init()
go func() {
escapeCount := 0
for {
select {
case <-ctx.Done():
log.Println("[app] context is done.")
p.died <- struct{}{}
return
case ev := <-p.incomingKey:
switch ev.Key() {
case key.Escape:
escapeCount++
if escapeCount > 1 {
log.Println("[app] waiting for engine to finalize correctly")
cancel()
return
}
case key.Up:
log.Printf("going up 10 px ? %04d,%04d", p.imageOffsetRow-10, imageHeight)
if p.imageOffsetRow-10 > 0 {
p.imageOffsetRow -= 10
p.redraw()
}
case key.Down:
log.Printf("going down 10 px ? %04d,%04d", p.imageOffsetRow+p.size.Rows+10, imageHeight)
if p.imageOffsetRow+p.size.Rows+10 < imageHeight {
p.imageOffsetRow += 10
p.redraw()
}
case key.Left:
log.Printf("going left 10 px ? %04d,%04d", p.imageOffsetColumn-10, imageWidth)
if p.imageOffsetColumn-10 > 0 {
p.imageOffsetColumn -= 10
p.redraw()
}
case key.Right:
log.Printf("going right 10 px ? %04d,%04d", p.imageOffsetColumn+p.size.Columns+10, imageWidth)
if p.imageOffsetColumn+p.size.Columns+10 < imageWidth {
p.imageOffsetColumn += 10
p.redraw()
}
}
case ev := <-p.incomingResize:
p.adjustAndRegisterPixels(ev)
}
}
}()
},
)
}
// creates the "Application"
func NewPage(ctx context.Context, engine term.Engine, cancel func(), image [][][2]color.Color, imageWidth, imageHeight int) *page {
result := &page{
died: make(chan struct{}), // init of died channel, a buffered channel of exactly one
incomingMouse: make(chan term.MouseEvent), // init of incoming channel
incomingKey: make(chan term.KeyEvent), // init of incoming channel
incomingResize: make(chan term.ResizeEvent),
engine: engine,
image: image,
imageWidth: imageWidth,
imageHeight: imageHeight,
}
result.lifeCycle(ctx, cancel)
return result
}
// gather each pixel color but each image row is condensed into one, which holds the two colors
func makeColors(r io.Reader) ([][][2]color.Color, int, int, error) {
img, format, err := image.Decode(r)
if err != nil {
log.Printf("decode error : %v %s\n", err, format)
return nil, 0, 0, err
}
bounds := img.Bounds()
width := bounds.Max.X
height := bounds.Max.Y
result := make([][][2]color.Color, width)
for col := 0; col < width; col++ {
result[col] = make([][2]color.Color, height/2+1)
rrow := 0
for row := 0; row < height; row += 2 {
result[col][rrow] = [2]color.Color{}
c1 := color.NewRGBAColor(img.At(col, row).RGBA())
result[col][rrow][0] = c1
if !color.IsRGB(c1) {
return nil, 0, 0, fmt.Errorf("error : color 0x%06X is not RGB", color.Hex(c1))
}
if row+1 < height {
c2 := color.NewRGBAColor(img.At(col, row+1).RGBA())
result[col][rrow][1] = c2
if !color.IsRGB(c2) {
return nil, 0, 0, fmt.Errorf("error : color 0x%06X is not RGB", color.Hex(c2))
}
}
rrow++
}
}
return result, width, height/2 + 1, nil
}
const usage = `piximage [pattern|url]
Examples:
piximage path/to/image.jpg
piximage https://example.com/image.jpg`
func main() {
if len(os.Args) == 1 {
fmt.Println(usage)
os.Exit(1)
}
if os.Args[1] == "-h" || os.Args[1] == "--help" {
fmt.Println(usage)
os.Exit(0)
}
var err error
var colors [][][2]color.Color
url := os.Args[1]
w, h := 0, 0
log.Printf("loading %q\n", url)
if file, err := ioutil.ReadFile(url); err != nil {
fmt.Printf("error loading image %q : %v", url, err)
} else {
colors, w, h, err = makeColors(bytes.NewReader(file))
if err != nil {
fmt.Printf("error reading image %q to colors : %v", url, err)
}
fmt.Printf("%d bytes were read. [w=%d,h=%d]\n", len(file), w, h)
}
enc.Register()
initLog.InitLogger()
engine, err := core.NewCore(
os.Getenv("TERM"),
core.WithFinalizer(func() {
log.Println("[app] core finalizer called")
}),
)
if err != nil {
log.Printf("error : %v", err)
os.Exit(1)
}
ctx, cancel := context.WithCancel(context.Background())
if err := engine.Start(ctx); err != nil {
log.Printf("error : %v", err)
os.Exit(1)
}
log.Printf("image %s has size %04d,%04d", url, w, h)
pCtx, _ := context.WithCancel(ctx)
page := NewPage(pCtx, engine, cancel, colors, w, h)
engine.KeyDispatcher().Register(page)
engine.ResizeDispatcher().Register(page)
log.Printf("[app] registered listeners.")
<-engine.DyingChan()
log.Println("[app] done.")
}