-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathkittyChunkWri.go
60 lines (48 loc) · 941 Bytes
/
kittyChunkWri.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
package rasterm
import (
"fmt"
"io"
)
type kittyChunkWri struct {
iWri io.Writer
nChunkSize int
nMod int
}
// signal last chunk to Kitty on close
func (c *kittyChunkWri) Close() error {
_, err := fmt.Fprint(c.iWri, KITTY_IMG_HDR, "m=0;", KITTY_IMG_FTR)
return err
}
func (c *kittyChunkWri) Write(buf []byte) (int, error) {
l := len(buf)
var toWrite, nWritten int
for l > 0 {
if (c.nMod + l) >= c.nChunkSize {
toWrite = c.nChunkSize - c.nMod
c.nMod = 0
} else {
toWrite = l
c.nMod += l
}
// prefix
_, err := fmt.Fprint(c.iWri, KITTY_IMG_HDR, "m=1;")
if err != nil {
return nWritten, err
}
// data
var n int
n, err = c.iWri.Write(buf[:toWrite])
nWritten += n
if err != nil {
return nWritten, err
}
// suffix
_, err = fmt.Fprint(c.iWri, KITTY_IMG_FTR)
if err != nil {
return nWritten, err
}
buf = buf[toWrite:]
l -= toWrite
}
return nWritten, nil
}