Skip to content

Commit

Permalink
docs: update docs
Browse files Browse the repository at this point in the history
  • Loading branch information
aymanbagabas committed Feb 27, 2023
1 parent 0cffb26 commit 6a5f3ac
Showing 1 changed file with 54 additions and 34 deletions.
88 changes: 54 additions & 34 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,56 +8,76 @@

A Go library to work with the [ANSI OSC52](https://invisible-island.net/xterm/ctlseqs/ctlseqs.html#h3-Operating-System-Commands) terminal sequence.

## Example
## Usage

```go
str := "Hello World!"
osc52.Copy(str) // Copies str to system clipboard
osc52.CopyPrimary(str) // Copies str to primary clipboard (X11 only)
```
You can use this small library to construct an ANSI OSC52 sequence suitable for
your terminal.

## SSH Example

You can use this over SSH using [gliderlabs/ssh](https://github.com/gliderlabs/ssh) for instance:
### Example

```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!")
```
import (
"os"
"fmt"

If you're using tmux, you could pass the `TMUX` environment variable to help detect tmux:
"github.com/aymanbagabas/go-osc52"
)

```sh
ssh -o SendEnv=TMUX <host>
```
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)

### Tmux users
// Query the clipboard
osc52.Query().WriteTo(os.Stderr)

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.
// Clear system clipboard
osc52.Clear().WriteTo(os.Stderr)

`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.
// Use the fmt.Stringer interface to copy `s` to system clipboard
fmt.Fprint(os.Stderr, osc52.New(s))

```tmux
set -g allow-passthrough on
// Or to primary clipboard
fmt.Fprint(os.Stderr, osc52.New(s).Primary())
}
```

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

You can use this over SSH using [gliderlabs/ssh](https://github.com/gliderlabs/ssh) for instance:

```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)
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 2.4](https://github.com/tmux/tmux/issues/3218#issuecomment-1153089282) [^2].

[^1]: See [tmux clipboard](https://github.com/tmux/tmux/wiki/Clipboard)
[^2]: [What is allow-passthrough](https://github.com/tmux/tmux/wiki/FAQ#what-is-the-passthrough-escape-sequence-and-how-do-i-use-it)

## Credits

* [vim-oscyank](https://github.com/ojroques/vim-oscyank) this is heavily inspired by vim-oscyank.
* [vim-oscyank](https://github.com/ojroques/vim-oscyank) this is heavily inspired by vim-oscyank.

0 comments on commit 6a5f3ac

Please sign in to comment.