Skip to content

Commit

Permalink
feat: add support for osc52 system command
Browse files Browse the repository at this point in the history
support copying text on supported terminals
  • Loading branch information
aymanbagabas committed Jun 1, 2022
1 parent 196940f commit d1fa5a5
Show file tree
Hide file tree
Showing 7 changed files with 71 additions and 23 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,4 @@
*.out

# Dependency directories (remove the comment below to include it)
# vendor/
# vendor/
51 changes: 29 additions & 22 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -284,27 +284,34 @@ output.DisableMouseAllMotion()

## Optional Feature Support

| Terminal | Alt Screen | Query Color Scheme | Query Cursor Position | Set Window Title | Change Cursor Color | Change Default Foreground Setting | Change Default Background Setting |
| ---------------- | :--------: | :----------------: | :-------------------: | :--------------: | :-----------------: | :-------------------------------: | :-------------------------------: |
| alacritty ||||||||
| foot ||||||||
| kitty ||||||||
| Konsole ||||||||
| rxvt ||||||||
| screen ||[^mux] ||||||
| st ||||||||
| tmux ||[^mux] ||||||
| vte-based[^vte] ||||||||
| wezterm ||||||||
| xterm ||||||||
| Linux Console ||||||||
| Apple Terminal ||||||||
| iTerm ||||||||
| Windows cmd ||||||||
| Windows Terminal ||||||||

[^vte]: This covers all vte-based terminals, including Gnome Terminal, guake, Pantheon Terminal, Terminator, Tilix, XFCE Terminal.
| Terminal | Alt Screen | Query Color Scheme | Query Cursor Position | Set Window Title | Change Cursor Color | Change Default Foreground Setting | Change Default Background Setting | Copy (OSC52) | Hyperlinks (OSC8) |
| ---------------- | :--------: | :----------------: | :-------------------: | :--------------: | :-----------------: | :-------------------------------: | :-------------------------------: | :----------: | :---------------: |
| alacritty |||||||||[^alacritty] |
| foot ||||||||||
| kitty ||||||||||
| Konsole ||||||||[^konsole] ||
| rxvt ||||||||||
| urxvt ||||||||[^urxvt] ||
| screen ||[^mux] |||||||[^screen] |
| st ||||||||||
| tmux ||[^mux] |||||||[^tmux] |
| vte-based[^vte] ||||||||[^vte] ||
| wezterm ||||||||||
| xterm ||||||||||
| Linux Console ||||||||||
| Apple Terminal ||||||||[^apple] ||
| iTerm ||||||||||
| Windows cmd ||||||||||
| Windows Terminal ||||||||||

[^vte]: This covers all vte-based terminals, including Gnome Terminal, guake, Pantheon Terminal, Terminator, Tilix, XFCE Terminal. OSC52 is not supported, see [issue#2495](https://gitlab.gnome.org/GNOME/vte/-/issues/2495).
[^mux]: Unavailable as multiplexers (like tmux or screen) can be connected to multiple terminals (with different color settings) at the same time.
[^urxvt]: Workaround for urxvt not supporting OSC52. See [this](https://unix.stackexchange.com/a/629485) for more information.
[^konsole]: OSC52 is not supported, for more info see [bug#372116](https://bugs.kde.org/show_bug.cgi?id=372116).
[^apple]: OSC52 works with a [workaround](https://github.com/roy2220/osc52pty).
[^tmux]: OSC8 is not supported, for more info see [issue#911](https://github.com/tmux/tmux/issues/911).
[^screen]: OSC8 is not supported, for more info see [bug#50952](https://savannah.gnu.org/bugs/index.php?50952).
[^alacritty]: OSC8 is not supported, for more info see [issue#922](https://github.com/alacritty/alacritty/issues/922).

You can help improve this list! Check out [how to](ansi_compat.md) and open an issue or pull request.

Expand Down Expand Up @@ -356,8 +363,8 @@ out these projects:

Got some feedback or suggestions? Please open an issue or drop me a note!

* [Twitter](https://twitter.com/mueslix)
* [The Fediverse](https://mastodon.social/@fribbledom)
- [Twitter](https://twitter.com/mueslix)
- [The Fediverse](https://mastodon.social/@fribbledom)

## License

Expand Down
16 changes: 16 additions & 0 deletions copy.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package termenv

import (
"github.com/aymanbagabas/go-osc52"
)

// Copy copies text to clipboard using OSC 52 escape sequence.
func (o Output) Copy(str string) {
out := osc52.NewOutput(o.tty, o.environ.Environ())
out.Copy(str)
}

// Copy copies text to clipboard using OSC 52 escape sequence.
func Copy(str string) {
output.Copy(str)
}
7 changes: 7 additions & 0 deletions examples/hello-world/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,4 +40,11 @@ func main() {
fmt.Printf("\n\t%s %s\n", termenv.String("Has foreground color").Bold(), termenv.ForegroundColor())
fmt.Printf("\t%s %s\n", termenv.String("Has background color").Bold(), termenv.BackgroundColor())
fmt.Printf("\t%s %t\n", termenv.String("Has dark background?").Bold(), termenv.HasDarkBackground())

hw := "Hello, world!"
termenv.Copy(hw)
fmt.Println()
fmt.Printf("\t%q copied to clipboard\n", hw)

fmt.Printf("\t%s", termenv.Hyperlink("http://example.com", "This is a link"))
}
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ module github.com/muesli/termenv
go 1.13

require (
github.com/aymanbagabas/go-osc52 v1.0.3
github.com/lucasb-eyer/go-colorful v1.2.0
github.com/mattn/go-isatty v0.0.14
github.com/mattn/go-runewidth v0.0.13
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
github.com/aymanbagabas/go-osc52 v1.0.3 h1:DTwqENW7X9arYimJrPeGZcV0ln14sGMt3pHZspWD+Mg=
github.com/aymanbagabas/go-osc52 v1.0.3/go.mod h1:zT8H+Rk4VSabYN90pWyugflM3ZhpTZNC7cASDfUCdT4=
github.com/lucasb-eyer/go-colorful v1.2.0 h1:1nnpGOrhyZZuNyfu1QjKiUICQ74+3FNCN69Aj6K7nkY=
github.com/lucasb-eyer/go-colorful v1.2.0/go.mod h1:R4dSotOR9KMtayYi1e77YzuveK+i7ruzyGqttikkLy0=
github.com/mattn/go-isatty v0.0.14 h1:yVuAays6BHfxijgZPzw+3Zlu5yQgKGP2/hcQbHb7S9Y=
Expand Down
15 changes: 15 additions & 0 deletions hyperlink.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package termenv

import (
"fmt"
)

// Hyperlink creates a hyperlink using OSC8.
func Hyperlink(link, name string) string {
return output.Hyperlink(link, name)
}

// Hyperlink creates a hyperlink using OSC8.
func (o *Output) Hyperlink(link, name string) string {
return fmt.Sprintf("\x1b]8;;%s\x1b\\%s\x1b]8;;\x1b\\", link, name)
}

0 comments on commit d1fa5a5

Please sign in to comment.