Socket
Socket
Sign inDemoInstall

github.com/aymanbagabas/go-osc52/v2

Package Overview
Dependencies
0
Alerts
File Explorer

Install Socket

Detect and block malicious and high-risk dependencies

Install

    github.com/aymanbagabas/go-osc52/v2

OSC52 is a terminal escape sequence that allows copying text to the clipboard. The sequence consists of the following: Pc is the clipboard choice: 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. Examples:


Version published

Readme

Source

go-osc52

Latest Release GoDoc

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

Usage

You can use this small library to construct an ANSI OSC52 sequence suitable for your terminal.

Example

import (
  "os"
  "fmt"

  "github.com/aymanbagabas/go-osc52/v2"
)

func main() {
  s := "Hello World!"

  // Copy `s` to system clipboard
  osc52.New(s).WriteTo(os.Stderr)

  // Copy `s` to primary clipboard (X11)
  osc52.New(s).Primary().WriteTo(os.Stderr)

  // Query the clipboard
  osc52.Query().WriteTo(os.Stderr)

  // Clear system clipboard
  osc52.Clear().WriteTo(os.Stderr)

  // Use the fmt.Stringer interface to copy `s` to system clipboard
  fmt.Fprint(os.Stderr, osc52.New(s))

  // Or to primary clipboard
  fmt.Fprint(os.Stderr, osc52.New(s).Primary())
}

SSH Example

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

var sshSession ssh.Session
seq := osc52.New("Hello awesome!")
// Check if term is screen or tmux
pty, _, _ := s.Pty()
if pty.Term == "screen" {
  seq = seq.Screen()
} else if isTmux {
  seq = seq.Tmux()
}
seq.WriteTo(sshSession.Stderr())

Tmux

Make sure you have set-clipboard on in your config, otherwise, tmux won't allow your application to access the clipboard 1.

Using the tmux option, osc52.TmuxMode or osc52.New(...).Tmux(), wraps the OSC52 sequence in a special tmux DCS sequence and pass it to the outer terminal. This requires allow-passthrough on in your config. allow-passthrough is no longer enabled by default since tmux 3.3a 2.

Credits

  • vim-oscyank this is heavily inspired by vim-oscyank.

Footnotes

  1. See tmux clipboard

  2. What is allow-passthrough

FAQs

Last updated on 06 Mar 2023

Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc