| 1 |
package ansi |
| 2 |
|
| 3 |
import ( |
| 4 |
"bytes" |
| 5 |
"strconv" |
| 6 |
"strings" |
| 7 |
) |
| 8 |
|
| 9 |
// SixelGraphics returns a sequence that encodes the given sixel image payload to |
| 10 |
// a DCS sixel sequence. |
| 11 |
// |
| 12 |
// DCS p1; p2; p3; q [sixel payload] ST |
| 13 |
// |
| 14 |
// p1 = pixel aspect ratio, deprecated and replaced by pixel metrics in the payload |
| 15 |
// |
| 16 |
// p2 = This is supposed to be 0 for transparency, but terminals don't seem to |
| 17 |
// to use it properly. Value 0 leaves an unsightly black bar on all terminals |
| 18 |
// I've tried and looks correct with value 1. |
| 19 |
// |
| 20 |
// p3 = Horizontal grid size parameter. Everyone ignores this and uses a fixed grid |
| 21 |
// size, as far as I can tell. |
| 22 |
// |
| 23 |
// See https://shuford.invisible-island.net/all_about_sixels.txt |
| 24 |
func SixelGraphics(p1, p2, p3 int, payload []byte) string { |
| 25 |
var buf bytes.Buffer |
| 26 |
|
| 27 |
buf.WriteString("\x1bP") |
| 28 |
if p1 >= 0 { |
| 29 |
buf.WriteString(strconv.Itoa(p1)) |
| 30 |
} |
| 31 |
buf.WriteByte(';') |
| 32 |
if p2 >= 0 { |
| 33 |
buf.WriteString(strconv.Itoa(p2)) |
| 34 |
} |
| 35 |
if p3 > 0 { |
| 36 |
buf.WriteByte(';') |
| 37 |
buf.WriteString(strconv.Itoa(p3)) |
| 38 |
} |
| 39 |
buf.WriteByte('q') |
| 40 |
buf.Write(payload) |
| 41 |
buf.WriteString("\x1b\\") |
| 42 |
|
| 43 |
return buf.String() |
| 44 |
} |
| 45 |
|
| 46 |
// KittyGraphics returns a sequence that encodes the given image in the Kitty |
| 47 |
// graphics protocol. |
| 48 |
// |
| 49 |
// APC G [comma separated options] ; [base64 encoded payload] ST |
| 50 |
// |
| 51 |
// See https://sw.kovidgoyal.net/kitty/graphics-protocol/ |
| 52 |
func KittyGraphics(payload []byte, opts ...string) string { |
| 53 |
var buf bytes.Buffer |
| 54 |
buf.WriteString("\x1b_G") |
| 55 |
buf.WriteString(strings.Join(opts, ",")) |
| 56 |
if len(payload) > 0 { |
| 57 |
buf.WriteString(";") |
| 58 |
buf.Write(payload) |
| 59 |
} |
| 60 |
buf.WriteString("\x1b\\") |
| 61 |
return buf.String() |
| 62 |
} |