|
| 1 | +package rasterm |
| 2 | + |
| 3 | +import ( |
| 4 | + "bytes" |
| 5 | + "encoding/base64" |
| 6 | + "fmt" |
| 7 | + "image" |
| 8 | + "image/jpeg" |
| 9 | + "image/png" |
| 10 | + "io" |
| 11 | + "os" |
| 12 | + "strings" |
| 13 | + "sync" |
| 14 | + |
| 15 | + "github.com/mattn/go-sixel" |
| 16 | +) |
| 17 | + |
| 18 | +// DefaultJPEGQuality is the default JPEG encode quality. |
| 19 | +var DefaultJPEGQuality = 93 |
| 20 | + |
| 21 | +// Encoder provides a common interface for terminal graphic encoders. |
| 22 | +type Encoder interface { |
| 23 | + String() string |
| 24 | + Available() bool |
| 25 | + Encode(io.Writer, image.Image) error |
| 26 | +} |
| 27 | + |
| 28 | +// KittyEncoder is a Kitty terminal graphics encoder. |
| 29 | +type KittyEncoder struct { |
| 30 | + NoNewline bool |
| 31 | +} |
| 32 | + |
| 33 | +// NewKittyEncoder creates a Kitty terminal graphics encoder. |
| 34 | +// |
| 35 | +// See: https://sw.kovidgoyal.net/kitty/graphics-protocol.html |
| 36 | +func NewKittyEncoder() Encoder { |
| 37 | + return KittyEncoder{} |
| 38 | +} |
| 39 | + |
| 40 | +// String satisfies the [Encoder] interface. |
| 41 | +func (KittyEncoder) String() string { |
| 42 | + return "kitty" |
| 43 | +} |
| 44 | + |
| 45 | +// Available satisfies the [Encoder] interface. |
| 46 | +func (KittyEncoder) Available() bool { |
| 47 | + return strings.ToLower(os.Getenv("TERM_GRAPHICS")) == "kitty" || |
| 48 | + strings.ToLower(os.Getenv("TERM")) == "xterm-kitty" |
| 49 | +} |
| 50 | + |
| 51 | +// Encode satisfies the [Encoder] interface. |
| 52 | +func (r KittyEncoder) Encode(w io.Writer, img image.Image) error { |
| 53 | + buf := new(bytes.Buffer) |
| 54 | + enc := base64.NewEncoder(base64.StdEncoding, buf) |
| 55 | + if err := png.Encode(enc, img); err != nil { |
| 56 | + return err |
| 57 | + } |
| 58 | + if err := enc.Close(); err != nil { |
| 59 | + return err |
| 60 | + } |
| 61 | + if err := chunkEncode(w, buf.Bytes(), 4096); err != nil { |
| 62 | + return err |
| 63 | + } |
| 64 | + if r.NoNewline { |
| 65 | + return nil |
| 66 | + } |
| 67 | + _, err := fmt.Fprintln(w) |
| 68 | + return err |
| 69 | +} |
| 70 | + |
| 71 | +// ITermEncoder is a iTerm terminal graphics encoder. |
| 72 | +// |
| 73 | +// See: https://iterm2.com/documentation-images.html |
| 74 | +type ITermEncoder struct { |
| 75 | + NoNewline bool |
| 76 | +} |
| 77 | + |
| 78 | +// NewITermEncoder creates a iTerm terminal graphics encoder. |
| 79 | +func NewITermEncoder() Encoder { |
| 80 | + return ITermEncoder{} |
| 81 | +} |
| 82 | + |
| 83 | +// String satisfies the [Encoder] interface. |
| 84 | +func (ITermEncoder) String() string { |
| 85 | + return "iterm" |
| 86 | +} |
| 87 | + |
| 88 | +// Available satisfies the [Encoder] interface. |
| 89 | +func (ITermEncoder) Available() bool { |
| 90 | + return strings.ToLower(os.Getenv("TERM_GRAPHICS")) == "iterm" || |
| 91 | + strings.ToLower(os.Getenv("TERM")) == "mintty" || |
| 92 | + strings.ToLower(os.Getenv("LC_TERMINAL")) == "iterm2" || |
| 93 | + strings.ToLower(os.Getenv("TERM_PROGRAM")) == "wezterm" |
| 94 | +} |
| 95 | + |
| 96 | +// Encode satisfies the [Encoder] interface. |
| 97 | +func (r ITermEncoder) Encode(w io.Writer, img image.Image) error { |
| 98 | + f := png.Encode |
| 99 | + if _, ok := img.(*image.Paletted); !ok { |
| 100 | + f = jpegEncode |
| 101 | + } |
| 102 | + buf := new(bytes.Buffer) |
| 103 | + enc := base64.NewEncoder(base64.StdEncoding, buf) |
| 104 | + if err := f(enc, img); err != nil { |
| 105 | + return err |
| 106 | + } |
| 107 | + if err := enc.Close(); err != nil { |
| 108 | + return err |
| 109 | + } |
| 110 | + if _, err := fmt.Fprintf(w, "\x1b]1337;File=inline=1:%s\a", buf.Bytes()); err != nil { |
| 111 | + return err |
| 112 | + } |
| 113 | + if r.NoNewline { |
| 114 | + return nil |
| 115 | + } |
| 116 | + _, err := fmt.Fprintln(w) |
| 117 | + return err |
| 118 | +} |
| 119 | + |
| 120 | +// SixelEncoder is a Sixel terminal graphics encoder. |
| 121 | +type SixelEncoder struct { |
| 122 | + NoNewline bool |
| 123 | +} |
| 124 | + |
| 125 | +// NewSixelEncoder creates a Sixel terminal graphics encoder. |
| 126 | +func NewSixelEncoder() Encoder { |
| 127 | + return SixelEncoder{} |
| 128 | +} |
| 129 | + |
| 130 | +// String satisfies the [Encoder] interface. |
| 131 | +func (SixelEncoder) String() string { |
| 132 | + return "sixel" |
| 133 | +} |
| 134 | + |
| 135 | +// Available satisfies the [Encoder] interface. |
| 136 | +func (SixelEncoder) Available() bool { |
| 137 | + if strings.ToLower(os.Getenv("TERM_GRAPHICS")) == "sixel" { |
| 138 | + return true |
| 139 | + } |
| 140 | + return hasSixelSupport() |
| 141 | +} |
| 142 | + |
| 143 | +// Encode satisfies the [Encoder] interface. |
| 144 | +func (r SixelEncoder) Encode(w io.Writer, img image.Image) error { |
| 145 | + if err := sixel.NewEncoder(w).Encode(img); err != nil { |
| 146 | + return err |
| 147 | + } |
| 148 | + if r.NoNewline { |
| 149 | + return nil |
| 150 | + } |
| 151 | + _, err := fmt.Fprintln(w) |
| 152 | + return err |
| 153 | +} |
| 154 | + |
| 155 | +// DefaultEncoder wraps multiple terminal graphic encoders. |
| 156 | +type DefaultEncoder struct { |
| 157 | + v []Encoder |
| 158 | + r Encoder |
| 159 | + err error |
| 160 | + once sync.Once |
| 161 | +} |
| 162 | + |
| 163 | +// NewDefaultEncoder creates a wrapper for multiple terminal graphic encoders. |
| 164 | +func NewDefaultEncoder(v ...Encoder) *DefaultEncoder { |
| 165 | + return &DefaultEncoder{ |
| 166 | + v: v, |
| 167 | + } |
| 168 | +} |
| 169 | + |
| 170 | +// init initializes the default encoder. |
| 171 | +func (r *DefaultEncoder) init() { |
| 172 | + for _, z := range r.v { |
| 173 | + if z.Available() { |
| 174 | + r.r = z |
| 175 | + return |
| 176 | + } |
| 177 | + } |
| 178 | + if r.r == nil { |
| 179 | + r.err = ErrTermGraphicsNotAvailable |
| 180 | + } |
| 181 | +} |
| 182 | + |
| 183 | +// String satisfies the [Encoder] interface. |
| 184 | +func (r *DefaultEncoder) String() string { |
| 185 | + r.once.Do(r.init) |
| 186 | + if r.err == nil && r.r != nil { |
| 187 | + return r.r.String() |
| 188 | + } |
| 189 | + return "<none>" |
| 190 | +} |
| 191 | + |
| 192 | +// Available satisfies the [Encoder] interface. |
| 193 | +func (r *DefaultEncoder) Available() bool { |
| 194 | + r.once.Do(r.init) |
| 195 | + return r.r != nil && r.err == nil |
| 196 | +} |
| 197 | + |
| 198 | +// Encode satisfies the [Encoder] interface. |
| 199 | +func (r *DefaultEncoder) Encode(w io.Writer, img image.Image) error { |
| 200 | + switch r.once.Do(r.init); { |
| 201 | + case r.err != nil: |
| 202 | + return r.err |
| 203 | + case r.r != nil: |
| 204 | + return r.r.Encode(w, img) |
| 205 | + } |
| 206 | + return ErrTermGraphicsNotAvailable |
| 207 | +} |
| 208 | + |
| 209 | +// jpegEncode encodes a image to w as a jpeg using [DefaultJPEGQuality]. |
| 210 | +func jpegEncode(w io.Writer, img image.Image) error { |
| 211 | + return jpeg.Encode(w, img, &jpeg.Options{ |
| 212 | + Quality: DefaultJPEGQuality, |
| 213 | + }) |
| 214 | +} |
| 215 | + |
| 216 | +// chunkEncode writes buf to w in chunks. |
| 217 | +func chunkEncode(w io.Writer, buf []byte, size int) error { |
| 218 | + if _, err := fmt.Fprintf(w, "\x1b_Ga=T,f=100,m=1;\x1b\\"); err != nil { |
| 219 | + return err |
| 220 | + } |
| 221 | + n := len(buf) |
| 222 | + for i, j, m := 0, min(size, n), 0; i < n; i, j = j, min(j+size, n) { |
| 223 | + if m = 0; j < n { |
| 224 | + m = 1 |
| 225 | + } |
| 226 | + if _, err := fmt.Fprintf(w, "\x1b_Gm=%d;%s\x1b\\", m, buf[i:j]); err != nil { |
| 227 | + return err |
| 228 | + } |
| 229 | + } |
| 230 | + return nil |
| 231 | +} |
0 commit comments