-
Notifications
You must be signed in to change notification settings - Fork 39
/
Copy pathsixel.go
102 lines (91 loc) · 1.94 KB
/
sixel.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
package term
import (
"bytes"
"image/png"
"os"
"sync"
"time"
"github.com/mattn/go-isatty"
"github.com/mattn/go-sixel"
"golang.org/x/crypto/ssh/terminal"
)
var sixelEnabled = false
func init() {
if os.Getenv("TERM_PROGRAM") != "iTerm.app" {
sixelEnabled = checkSixel()
}
}
func checkSixel() bool {
if isatty.IsCygwinTerminal(os.Stdout.Fd()) {
return true
}
s, err := terminal.MakeRaw(1)
if err == nil {
defer terminal.Restore(1, s)
}
_, err = os.Stdout.Write([]byte("\x1b[c"))
if err != nil {
return false
}
defer fileSetReadDeadline(os.Stdout, time.Time{})
var b [100]byte
n, err := os.Stdout.Read(b[:])
if err != nil {
return false
}
// Check tmux (sixel enabled)
// See: CSI Ps c, Ps = 4 (Sixel graphics)
// https://invisible-island.net/xterm/ctlseqs/ctlseqs.html
// NOTE: tmux does not return VT name.
if bytes.HasPrefix(b[:n], []byte("\x1b[?1;2;4c")) {
return true
}
var supportedTerminals = []string{
"\x1b[?62;", // VT240
"\x1b[?63;", // wsltty
"\x1b[?64;", // mintty
"\x1b[?65;", // RLogin
}
supported := false
for _, supportedTerminal := range supportedTerminals {
if bytes.HasPrefix(b[:n], []byte(supportedTerminal)) {
supported = true
break
}
}
if !supported {
return false
}
for _, t := range bytes.Split(b[6:n], []byte(";")) {
if len(t) == 1 && t[0] == '4' {
return true
}
}
return false
}
type sixelWriter struct {
Name string
Width int
Height int
once sync.Once
enc *sixel.Encoder
buf *bytes.Buffer
}
func (w *sixelWriter) init() {
w.buf = &bytes.Buffer{}
w.enc = sixel.NewEncoder(os.Stdout)
}
// Write writes the PNG image data into the imageWriter buffer.
func (w *sixelWriter) Write(p []byte) (n int, err error) {
w.once.Do(w.init)
return w.buf.Write(p)
}
// Close flushes the image to the terminal and close the writer.
func (w *sixelWriter) Close() error {
w.once.Do(w.init)
img, err := png.Decode(w.buf)
if err != nil {
return err
}
return w.enc.Encode(img)
}