File: osc52.go

package info (click to toggle)
golang-github-aymanbagabas-go-osc52 1.2.1-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bookworm-backports, forky, sid, trixie
  • size: 88 kB
  • sloc: makefile: 2
file content (219 lines) | stat: -rwxr-xr-x 6,008 bytes parent folder | download
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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
// OSC52 is a terminal escape sequence that allows copying text to the clipboard.
//
// The sequence consists of the following:
//
//	OSC 52 ; Pc ; Pd BEL
//
// Pc is the clipboard choice:
//
//	c: clipboard
//	p: primary
//	q: secondary (not supported)
//	s: select (not supported)
//	0-7: cut-buffers (not supported)
//
// Pd is the data to copy to the clipboard. This string should be encoded in
// base64 (RFC-4648).
//
// If Pd is "?", the terminal replies to the host with the current contents of
// the clipboard.
//
// If Pd is neither a base64 string nor "?", the terminal clears the clipboard.
//
// See https://invisible-island.net/xterm/ctlseqs/ctlseqs.html#h3-Operating-System-Commands
// where Ps = 52 => Manipulate Selection Data.
package osc52

import (
	"encoding/base64"
	"fmt"
	"io"
	"os"
	"strings"
)

// Clipboard is the clipboard buffer to use.
type Clipboard uint

const (
	// SystemClipboard is the system clipboard buffer.
	SystemClipboard Clipboard = iota
	// PrimaryClipboard is the primary clipboard buffer (X11).
	PrimaryClipboard
)

// String implements the fmt.Stringer interface for [Clipboard].
func (c Clipboard) String() string {
	return []string{
		"c", "p",
	}[c]
}

// output is the default output for Copy which uses os.Stdout and os.Environ.
var output = NewOutput(os.Stdout, os.Environ())

// envs is a map of environment variables.
type envs map[string]string

// Get returns the value of the environment variable named by the key.
func (e envs) Get(key string) string {
	v, ok := e[key]
	if !ok {
		return ""
	}
	return v
}

// Output is where the OSC52 string should be written.
type Output struct {
	out  io.Writer
	envs envs
}

// NewOutput returns a new Output.
func NewOutput(out io.Writer, envs []string) *Output {
	e := make(map[string]string, 0)
	for _, env := range envs {
		s := strings.Split(env, "=")
		k := s[0]
		v := strings.Join(s[1:], "=")
		e[k] = v
	}
	o := &Output{
		out:  out,
		envs: e,
	}
	return o
}

// DefaultOutput returns the default output for Copy.
func DefaultOutput() *Output {
	return output
}

// Copy copies the OSC52 string to the output. This uses the system clipboard buffer.
func Copy(str string) {
	output.Copy(str)
}

// Copy copies the OSC52 string to the output. This uses the system clipboard buffer.
func (o *Output) Copy(str string) {
	o.CopyClipboard(str, SystemClipboard)
}

// CopyPrimary copies the OSC52 string to the output. This uses the primary clipboard buffer.
func CopyPrimary(str string) {
	output.CopyPrimary(str)
}

// CopyPrimary copies the OSC52 string to the output. This uses the primary clipboard buffer.
func (o *Output) CopyPrimary(str string) {
	o.CopyClipboard(str, PrimaryClipboard)
}

// CopyClipboard copies the OSC52 string to the output. This uses the passed clipboard buffer.
func CopyClipboard(str string, c Clipboard) {
	output.CopyClipboard(str, c)
}

// CopyClipboard copies the OSC52 string to the output. This uses the passed clipboard buffer.
func (o *Output) CopyClipboard(str string, c Clipboard) {
	o.osc52Write(str, c)
}

func (o *Output) osc52Write(str string, c Clipboard) {
	var seq string
	term := strings.ToLower(o.envs.Get("TERM"))
	switch {
	case o.envs.Get("TMUX") != "", strings.HasPrefix(term, "tmux"):
		seq = Sequence(str, "tmux", c)
	case strings.HasPrefix(term, "screen"):
		seq = Sequence(str, "screen", c)
	case strings.Contains(term, "kitty"):
		// First, we flush the keyboard before copying, this is required for
		// Kitty < 0.22.0.
		o.out.Write([]byte(Clear(term, c)))
		seq = Sequence(str, "kitty", c)
	default:
		seq = Sequence(str, term, c)
	}
	o.out.Write([]byte(seq))
}

func seqStart(term string, c Clipboard) string {
	var seq strings.Builder
	switch {
	case strings.Contains(term, "tmux"):
		// Write the start of a tmux escape sequence.
		seq.WriteString("\x1bPtmux;\x1b")
	case strings.Contains(term, "screen"):
		// Write the start of a DCS sequence.
		seq.WriteString("\x1bP")
	}
	// OSC52 sequence start.
	seq.WriteString(fmt.Sprintf("\x1b]52;%s;", c))
	return seq.String()
}

func seqEnd(term string) string {
	var seq strings.Builder
	// OSC52 sequence end.
	seq.WriteString("\x07")
	switch {
	case strings.Contains(term, "tmux"):
		// Terminate the tmux escape sequence.
		seq.WriteString("\x1b\\")
	case strings.Contains(term, "screen"):
		// Write the end of a DCS sequence.
		seq.WriteString("\x1b\x5c")
	}
	return seq.String()
}

// sequence returns the OSC52 sequence for the passed content.
// Beware that the string here is not base64 encoded.
func sequence(contents string, term string, c Clipboard) string {
	var seq strings.Builder
	term = strings.ToLower(term)
	seq.WriteString(seqStart(term, c))
	switch {
	case strings.Contains(term, "screen"):
		// Screen doesn't support OSC52 but will pass the contents of a DCS sequence to
		// the outer terminal unchanged.
		//
		// Here, we split the encoded string into 76 bytes chunks and then join the
		// chunks with <end-dsc><start-dsc> sequences. Finally, wrap the whole thing in
		// <start-dsc><start-osc52><joined-chunks><end-osc52><end-dsc>.
		s := strings.SplitN(contents, "", 76)
		seq.WriteString(strings.Join(s, "\x1b\\\x1bP"))
	default:
		seq.WriteString(contents)
	}
	seq.WriteString(seqEnd(term))
	return seq.String()
}

// Sequence returns the OSC52 sequence for the given string, terminal, and clipboard choice.
func Sequence(str string, term string, c Clipboard) string {
	b64 := base64.StdEncoding.EncodeToString([]byte(str))
	return sequence(b64, term, c)
}

// Contents returns the contents of the clipboard.
func Contents(term string, c Clipboard) string {
	var seq strings.Builder
	seq.WriteString(seqStart(term, c))
	seq.WriteString("?")
	seq.WriteString(seqEnd(term))
	return seq.String()
}

// Clear returns the OSC52 sequence to clear the clipboard.
func Clear(term string, c Clipboard) string {
	var seq strings.Builder
	seq.WriteString(seqStart(term, c))
	// Clear the clipboard
	seq.WriteString("!")
	seq.WriteString(seqEnd(term))
	return seq.String()
}