Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Async Clipboard #982

Closed
meowgorithm opened this issue Apr 19, 2024 · 8 comments · Fixed by #1086
Closed

Async Clipboard #982

meowgorithm opened this issue Apr 19, 2024 · 8 comments · Fixed by #1086
Labels
enhancement New feature or request

Comments

@meowgorithm
Copy link
Member

meowgorithm commented Apr 19, 2024

I've been seeing reports that clipboard operations can take a long time in WSL. While moving copy/paste to Cmds won't solve this issue, it's probably worth doing it regardless, both because clipboard stuff is I/O, and to provide a unified copy/paste API to all Bubble Tea applications.

Note that we also discussed this in @aymanbagabas's upcoming Kitty Keyboard work.

@meowgorithm meowgorithm added the enhancement New feature or request label Apr 22, 2024
@meowgorithm meowgorithm changed the title Aync Clipboard Async Clipboard Apr 22, 2024
@ajayd-san
Copy link

Hey can I take this up ?

@meowgorithm
Copy link
Member Author

Sounds good to me. @aymanbagabas, whatcha think?

@aymanbagabas
Copy link
Member

I think that would be wonderful to add to Bubble Tea!

Looking at implementation details, there are a couple of golang clipboard libraries such as https://github.com/atotto/clipboard and https://github.com/golang-design/clipboard, but each has its own shortcomings. The former always assumes programs running locally (no remote sessions support) and the latter uses CGO.

Given this, I'm proposing porting atotto/clipboard to bubbletea with a few changes to recognize the program environment variables and use OSC52 when working with remote sessions like SSH. Something along the lines of this API

type ClipboardWriteMsg bool

func ClipboardWrite(text string) tea.Cmd {
  return func() tea.Msg {
    err := copyClipboard()
    return ClipboardWriteMsg(err == nil)
  }
}

type ClipboardReadMsg string

func ClipboardRead() tea.Cmd {
  return func() tea.Msg {
    text := readClipboard()
    return ClipboardReadMsg(text)
  }
}
  

@ajayd-san
Copy link

Hey, So tell me if I'm going in the right direction.

I'm thinking of adding ClipboardRead and ClipboardWrite functions in commands.go and I'll be using atotto/clipboard for normal sessions and x/clipboard for ssh session.

How do i know if I'm in a ssh session? Check if SSH_CLIENT or SSH_TTY is set in p.environ.

Does this look good?

@aymanbagabas
Copy link
Member

I'll be using atotto/clipboard for normal sessions

That wouldn't work because atotto/clipboard uses os.Getenv() instead of p.environ to determine environment variables. We need to port atotto/clipboard to bubbletea and make it use the program's environment variables.

How do i know if I'm in a ssh session? Check if SSH_CLIENT or SSH_TTY is set in p.environ.

Correct, check for SSH_TTY to determine if the remote session is a terminal and use ansi.SetClipboard to use OSC52.

@ajayd-san
Copy link

Aight I'll fork atotto/clipboard make the required changes, and get back to you when I'm done.

@ajayd-san
Copy link

ajayd-san commented Jul 18, 2024

So, I've ported atotto/clipboard to use user defined env vars (p.environ) instead of os.GetEnv() :
ajayd-san/clipboard@b53899f

Have a look and let me know if it looks good.

@ajayd-san
Copy link

Hey, Can anyone tell me if I'm going in the right direction? Any input would be appreciated. Thanks

aymanbagabas added a commit that referenced this issue Aug 15, 2024
This adds support to setting the system and primary (X11 & Wayland)
clipboards using OSC52. This makes the clipboard commands work even on
remote session such as SSH.

While this doesn't work on all terminals, most modern terminals support
OSC52 including Alacritty, Kitty, Xterm.JS, etc. For terminals, that
don't support OSC52, application developers should consider using a
Golang clipboard library like https://github.com/atotto/clipboard.

OSC52 support can be detected if the terminal responds to a
`ReadClipboard` command.

Fixes: #982
aymanbagabas added a commit that referenced this issue Aug 19, 2024
…#1086)

This adds support to setting the system and primary (X11 & Wayland)
clipboards using OSC52. This makes the clipboard commands work even on
remote session such as SSH.

While this doesn't work on all terminals, most modern terminals support
OSC52 including Alacritty, Kitty, Xterm.JS, etc. For terminals, that
don't support OSC52, application developers should consider using a
Golang clipboard library like https://github.com/atotto/clipboard.

OSC52 support can be detected if the terminal responds to a
`ReadClipboard` command.

Fixes: #982
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants