pax_global_header 0000666 0000000 0000000 00000000064 14322041612 0014505 g ustar 00root root 0000000 0000000 52 comment=64534a3e8e1c38973b62289e51553bafaf52d60c
go-osc52-1.2.1/ 0000775 0000000 0000000 00000000000 14322041612 0013044 5 ustar 00root root 0000000 0000000 go-osc52-1.2.1/.github/ 0000775 0000000 0000000 00000000000 14322041612 0014404 5 ustar 00root root 0000000 0000000 go-osc52-1.2.1/.github/workflows/ 0000775 0000000 0000000 00000000000 14322041612 0016441 5 ustar 00root root 0000000 0000000 go-osc52-1.2.1/.github/workflows/build.yml 0000664 0000000 0000000 00000001453 14322041612 0020266 0 ustar 00root root 0000000 0000000 name: build
on:
push:
pull_request:
branches:
- master
jobs:
build:
strategy:
matrix:
go-version: [^1]
os: [ubuntu-latest, macos-latest, windows-latest]
runs-on: ${{ matrix.os }}
env:
GO111MODULE: "on"
steps:
- name: Checkout code
uses: actions/checkout@v3
- name: Install Go
uses: actions/setup-go@v3
with:
go-version: ${{ matrix.go-version }}
- uses: actions/cache@v3
with:
path: ~/go/pkg/mod
key: ${{ runner.os }}-go-${{ hashFiles('**/go.sum') }}
restore-keys: |
${{ runner.os }}-go-
- name: Download Go modules
run: go mod download
- name: Build
run: go build -v ./...
- name: Test
run: go test ./...
go-osc52-1.2.1/LICENSE 0000664 0000000 0000000 00000002056 14322041612 0014054 0 ustar 00root root 0000000 0000000 MIT License
Copyright (c) 2022 Ayman Bagabas
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
go-osc52-1.2.1/README.md 0000664 0000000 0000000 00000003763 14322041612 0014334 0 ustar 00root root 0000000 0000000
# go-osc52
A Go library to work with the [ANSI OSC52](https://invisible-island.net/xterm/ctlseqs/ctlseqs.html#h3-Operating-System-Commands) terminal sequence.
## Example
```go
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](https://github.com/gliderlabs/ssh) for instance:
```go
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:
```sh
ssh -o SendEnv=TMUX
```
### 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](https://github.com/tmux/tmux/wiki/FAQ#why-do-you-use-the-screen-terminal-description-inside-tmux)
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.
```tmux
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:
```go
// 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
* [vim-oscyank](https://github.com/ojroques/vim-oscyank) this is heavily inspired by vim-oscyank. go-osc52-1.2.1/_examples/ 0000775 0000000 0000000 00000000000 14322041612 0015021 5 ustar 00root root 0000000 0000000 go-osc52-1.2.1/_examples/local/ 0000775 0000000 0000000 00000000000 14322041612 0016113 5 ustar 00root root 0000000 0000000 go-osc52-1.2.1/_examples/local/main.go 0000664 0000000 0000000 00000000234 14322041612 0017365 0 ustar 00root root 0000000 0000000 package main
import (
"fmt"
"github.com/aymanbagabas/go-osc52"
)
func main() {
str := "hello world"
osc52.Copy(str)
fmt.Printf("Copied %q!", str)
}
go-osc52-1.2.1/_examples/ssh/ 0000775 0000000 0000000 00000000000 14322041612 0015616 5 ustar 00root root 0000000 0000000 go-osc52-1.2.1/_examples/ssh/main.go 0000664 0000000 0000000 00000001447 14322041612 0017077 0 ustar 00root root 0000000 0000000 package main
import (
"fmt"
"log"
"github.com/aymanbagabas/go-osc52"
"github.com/charmbracelet/wish"
"github.com/gliderlabs/ssh"
)
func main() {
s, err := wish.NewServer(
wish.WithAddress(":2222"),
wish.WithHostKeyPath("ssh_host_key"),
wish.WithMiddleware(
middleware(),
),
)
if err != nil {
log.Fatal(err)
}
fmt.Printf("SSH into %s\n", s.Addr)
s.ListenAndServe()
}
func middleware() wish.Middleware {
return func(h ssh.Handler) ssh.Handler {
return func(s ssh.Session) {
environ := s.Environ()
pty, _, _ := s.Pty()
// Put TERM environment variable into environ.
environ = append(environ, fmt.Sprintf("TERM=%s", pty.Term))
out := osc52.NewOutput(s, environ)
str := "hello world"
out.Copy(str)
s.Write([]byte(fmt.Sprintf("Copied %q!\n", str)))
}
}
}
go-osc52-1.2.1/go.mod 0000775 0000000 0000000 00000000061 14322041612 0014152 0 ustar 00root root 0000000 0000000 module github.com/aymanbagabas/go-osc52
go 1.16
go-osc52-1.2.1/go.sum 0000664 0000000 0000000 00000000000 14322041612 0014165 0 ustar 00root root 0000000 0000000 go-osc52-1.2.1/osc52.go 0000775 0000000 0000000 00000013570 14322041612 0014337 0 ustar 00root root 0000000 0000000 // 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 sequences. Finally, wrap the whole thing in
// .
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()
}