-
Notifications
You must be signed in to change notification settings - Fork 16
/
load.go
204 lines (173 loc) · 4.27 KB
/
load.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
package component
import (
"context"
"fmt"
"image"
"image/gif"
"image/png"
"io"
"io/ioutil"
"mime"
"net/http"
"os"
"path/filepath"
"strings"
tea "github.com/charmbracelet/bubbletea"
"github.com/disintegration/imageorient"
"github.com/lucasb-eyer/go-colorful"
"github.com/muesli/termenv"
"github.com/nfnt/resize"
"github.com/srwiley/oksvg"
"github.com/srwiley/rasterx"
)
type loadMsg struct {
io.ReadCloser
}
func loadUrl(url string) tea.Cmd {
var r io.ReadCloser
var err error
if strings.HasPrefix(url, "http") {
var resp *http.Response
resp, err = http.Get(url)
r = resp.Body
} else {
r, err = os.Open(url)
}
if err != nil {
return func() tea.Msg {
return errMsg{err}
}
}
return load(r)
}
func load(r io.ReadCloser) tea.Cmd {
return func() tea.Msg {
return loadMsg{r}
}
}
func handleLoadMsg(m Model, msg loadMsg) (Model, tea.Cmd) {
if m.cancelAnimation != nil {
m.cancelAnimation()
}
// blank out image so it says "loading..."
m.image = ""
selected := m.url
ext := filepath.Ext(selected)
t := mime.TypeByExtension(ext)
if strings.Contains(t, "gif") {
return handleLoadMsgAnimation(m, msg)
}
return handleLoadMsgStatic(m, msg)
}
func handleLoadMsgStatic(m Model, msg loadMsg) (Model, tea.Cmd) {
defer msg.Close()
img, err := readerToimage(m.width, m.height, m.url, msg)
if err != nil {
return m, func() tea.Msg { return errMsg{err} }
}
m.image = img
return m, nil
}
func handleLoadMsgAnimation(m Model, msg loadMsg) (Model, tea.Cmd) {
defer msg.Close()
// decode the gif
gimg, err := gif.DecodeAll(msg)
if err != nil {
return m, wrapErrCmd(err)
}
ctx := context.Background()
ctx, cancel := context.WithCancel(ctx)
m.cancelAnimation = cancel
// precompute the frames for performance reasons
var frames []string
for _, img := range gimg.Image {
str, err := imageToString(m.width, m.height, m.url, img)
if err != nil {
return m, wrapErrCmd(err)
}
frames = append(frames, str)
}
return m, func() tea.Msg {
return gifMsg{
gif: gimg,
frames: frames,
frame: 0,
ctx: ctx,
}
}
}
func imageToString(width, height uint, url string, img image.Image) (string, error) {
img = resize.Thumbnail(width, height*2-4, img, resize.Lanczos3)
b := img.Bounds()
w := b.Max.X
h := b.Max.Y
p := termenv.ColorProfile()
str := strings.Builder{}
for y := 0; y < h; y += 2 {
for x := w; x < int(width); x = x + 2 {
str.WriteString(" ")
}
for x := 0; x < w; x++ {
c1, _ := colorful.MakeColor(img.At(x, y))
color1 := p.Color(c1.Hex())
c2, _ := colorful.MakeColor(img.At(x, y+1))
color2 := p.Color(c2.Hex())
str.WriteString(termenv.String("▀").
Foreground(color1).
Background(color2).
String())
}
str.WriteString("\n")
}
str.WriteString(fmt.Sprintf("q to quit | %s\n", url))
return str.String(), nil
}
func readerToimage(width uint, height uint, url string, r io.Reader) (string, error) {
if strings.HasSuffix(strings.ToLower(url), ".svg") {
return svgToimage(width, height, url, r)
}
img, _, err := imageorient.Decode(r)
if err != nil {
return "", err
}
return imageToString(width, height, url, img)
}
func svgToimage(width uint, height uint, url string, r io.Reader) (string, error) {
// Original author: https://stackoverflow.com/users/10826783/usual-human
// https://stackoverflow.com/questions/42993407/how-to-create-and-export-svg-to-png-jpeg-in-golang
// Adapted to use size from SVG, and to use temp file.
tmpPngFile, err := ioutil.TempFile("", "imgcat.*.png")
if err != nil {
return "", err
}
tmpPngPath := tmpPngFile.Name()
defer os.Remove(tmpPngPath)
defer tmpPngFile.Close()
// Rasterize the SVG:
icon, err := oksvg.ReadIconStream(r)
if err != nil {
return "", err
}
w := int(icon.ViewBox.W)
h := int(icon.ViewBox.H)
icon.SetTarget(0, 0, float64(w), float64(h))
rgba := image.NewRGBA(image.Rect(0, 0, w, h))
icon.Draw(rasterx.NewDasher(w, h, rasterx.NewScannerGV(w, h, rgba, rgba.Bounds())), 1)
// Write rasterized image as PNG:
err = png.Encode(tmpPngFile, rgba)
if err != nil {
tmpPngFile.Close()
return "", err
}
tmpPngFile.Close()
rPng, err := os.Open(tmpPngPath)
if err != nil {
return "", err
}
defer rPng.Close()
img, _, err := imageorient.Decode(rPng)
if err != nil {
return "", err
}
return imageToString(width, height, url, img)
}