diff --git a/.gitignore b/.gitignore index 66fd13c..f7b3492 100644 --- a/.gitignore +++ b/.gitignore @@ -12,4 +12,4 @@ *.out # Dependency directories (remove the comment below to include it) -# vendor/ +# vendor/ \ No newline at end of file diff --git a/README.md b/README.md index 2df7d27..cb7c726 100644 --- a/README.md +++ b/README.md @@ -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. @@ -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 diff --git a/copy.go b/copy.go new file mode 100644 index 0000000..5c975d9 --- /dev/null +++ b/copy.go @@ -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) +} diff --git a/examples/hello-world/main.go b/examples/hello-world/main.go index 2637868..8d96c7b 100644 --- a/examples/hello-world/main.go +++ b/examples/hello-world/main.go @@ -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")) } diff --git a/go.mod b/go.mod index ad30855..e76c235 100644 --- a/go.mod +++ b/go.mod @@ -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 diff --git a/go.sum b/go.sum index 3135efa..cdd4e0b 100644 --- a/go.sum +++ b/go.sum @@ -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= diff --git a/hyperlink.go b/hyperlink.go new file mode 100644 index 0000000..9d45450 --- /dev/null +++ b/hyperlink.go @@ -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) +}