osc52

package module
v1.2.2 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Feb 26, 2023 License: MIT Imports: 5 Imported by: 6

README

go-osc52

Latest Release GoDoc

A Go library to work with the ANSI OSC52 terminal sequence.

Example

str := "Hello World!"
osc52.Copy(str) // Copies str to system clipboard
osc52.CopyPrimary(str) // Copies str to primary clipboard (X11 only)

SSH Example

You can use this over SSH using gliderlabs/ssh for instance:

envs := sshSession.Environ()
pty, _, _ := s.Pty()
envs = append(envs, "TERM="+pty.Term)
output := NewOutput(sshSession, envs)
// Copy text in your application
output.Copy("Hello awesome!")

If you're using tmux, you could pass the TMUX environment variable to help detect tmux:

ssh -o SendEnv=TMUX <host>
Tmux users

If you're using tmux, make sure you set set -g default-terminal in your tmux config, to a value that starts with tmux-. tmux-256color for instance. See this for more details.

go-osc52 will wrap the OSC52 sequence in a tmux escape sequence if tmux is detected. If you're running tmux >= 3.3, OSC52 won't work and you'll need to set the set -g allow-passthrough on in your tmux config.

set -g allow-passthrough on

or set set -g set-clipboard on in your tmux config and use your outer terminal in your code instead:

// Assuming this code is running in tmux >= 3.3 in kitty
seq := osc52.Sequence("Hello awesome!", "xterm-kitty", osc52.ClipboardC)
os.Stderr.WriteString(seq)

Credits

Documentation

Overview

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.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Clear added in v1.2.0

func Clear(term string, c Clipboard) string

Clear returns the OSC52 sequence to clear the clipboard.

func Contents added in v1.2.0

func Contents(term string, c Clipboard) string

Contents returns the contents of the clipboard.

func Copy

func Copy(str string)

Copy copies the OSC52 string to the output. This uses the system clipboard buffer.

func CopyClipboard added in v1.2.0

func CopyClipboard(str string, c Clipboard)

CopyClipboard copies the OSC52 string to the output. This uses the passed clipboard buffer.

func CopyPrimary added in v1.2.0

func CopyPrimary(str string)

CopyPrimary copies the OSC52 string to the output. This uses the primary clipboard buffer.

func Sequence added in v1.2.0

func Sequence(str string, term string, c Clipboard) string

Sequence returns the OSC52 sequence for the given string, terminal, and clipboard choice.

Types

type Clipboard added in v1.2.0

type Clipboard uint

Clipboard is the clipboard buffer to use.

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

func (Clipboard) String added in v1.2.0

func (c Clipboard) String() string

String implements the fmt.Stringer interface for Clipboard.

type Output

type Output struct {
	// contains filtered or unexported fields
}

Output is where the OSC52 string should be written.

func DefaultOutput added in v1.2.0

func DefaultOutput() *Output

DefaultOutput returns the default output for Copy.

func NewOutput

func NewOutput(out io.Writer, envs []string) *Output

NewOutput returns a new Output.

func (*Output) Copy

func (o *Output) Copy(str string)

Copy copies the OSC52 string to the output. This uses the system clipboard buffer.

func (*Output) CopyClipboard added in v1.2.0

func (o *Output) CopyClipboard(str string, c Clipboard)

CopyClipboard copies the OSC52 string to the output. This uses the passed clipboard buffer.

func (*Output) CopyPrimary added in v1.2.0

func (o *Output) CopyPrimary(str string)

CopyPrimary copies the OSC52 string to the output. This uses the primary clipboard buffer.

Directories

Path Synopsis
_examples
ssh

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL