vendor/github.com/charmbracelet/x/ansi/osc.go
69 lines · 1834 bytes
1package ansi
2
3import (
4 "bytes"
5 "strings"
6)
7
8// OscSequence represents an OSC sequence.
9//
10// The sequence starts with a OSC sequence, OSC (0x9D) in a 8-bit environment
11// or ESC ] (0x1B 0x5D) in a 7-bit environment, followed by positive integer identifier,
12// then by arbitrary data terminated by a ST (0x9C) in a 8-bit environment,
13// ESC \ (0x1B 0x5C) in a 7-bit environment, or BEL (0x07) for backwards compatibility.
14//
15// OSC Ps ; Pt ST
16// OSC Ps ; Pt BEL
17//
18// See ECMA-48 § 5.7.
19type OscSequence struct {
20 // Data contains the raw data of the sequence including the identifier
21 // command.
22 Data []byte
23
24 // Cmd contains the raw command of the sequence.
25 Cmd int
26}
27
28var _ Sequence = OscSequence{}
29
30// Command returns the command of the OSC sequence.
31func (s OscSequence) Command() int {
32 return s.Cmd
33}
34
35// Params returns the parameters of the OSC sequence split by ';'.
36// The first element is the identifier command.
37func (s OscSequence) Params() []string {
38 return strings.Split(string(s.Data), ";")
39}
40
41// Clone returns a copy of the OSC sequence.
42func (s OscSequence) Clone() Sequence {
43 return OscSequence{
44 Data: append([]byte(nil), s.Data...),
45 Cmd: s.Cmd,
46 }
47}
48
49// String returns the string representation of the OSC sequence.
50// To be more compatible with different terminal, this will always return a
51// 7-bit formatted sequence, terminated by BEL.
52func (s OscSequence) String() string {
53 return s.buffer().String()
54}
55
56// Bytes returns the byte representation of the OSC sequence.
57// To be more compatible with different terminal, this will always return a
58// 7-bit formatted sequence, terminated by BEL.
59func (s OscSequence) Bytes() []byte {
60 return s.buffer().Bytes()
61}
62
63func (s OscSequence) buffer() *bytes.Buffer {
64 var b bytes.Buffer
65 b.WriteString("\x1b]")
66 b.Write(s.Data)
67 b.WriteByte(BEL)
68 return &b
69}