-
Notifications
You must be signed in to change notification settings - Fork 1.9k
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
feat(tui): terminal pane mouse copying #8713
Merged
NicholasLYang
merged 15 commits into
main
from
chrisolszewski/turbo-3680-tui-copy-paste
Jul 29, 2024
Merged
Changes from 1 commit
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
a041fb9
feat(vt100): add text selection capabilities
chris-olszewski 92823aa
feat(vt100): display selected cells differently
chris-olszewski 7dfed4b
feat(ui): implement mouse selection
chris-olszewski cffbf29
feat(ui): implement clipboard copying
chris-olszewski a8563b6
fix(tui): clear selection on click
chris-olszewski a66d507
fix(tui): properly handle scrolling while selecting
chris-olszewski fc2b823
feat(tui): add copy mode info
chris-olszewski b339d80
fix(tui): make it easier to select first column
chris-olszewski e28dfd8
chore(tui): remove unused copy methods
chris-olszewski 803fac7
chore(tui): only display copy message for non-empty selection
chris-olszewski dc5fd71
fix(tui): only intercept c if selection is made
chris-olszewski 08a242d
chore(tui): remove allocations for current task name
chris-olszewski 41d626d
fix(tui): account for pane header/footer
chris-olszewski e11612b
fix(tui): fix windows compilation
chris-olszewski 7c4535d
Fix clippy
NicholasLYang File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,113 @@ | ||
// Inspired by https://github.com/pvolok/mprocs/blob/master/src/clipboard.rs | ||
use std::process::Stdio; | ||
|
||
use base64::Engine; | ||
use which::which; | ||
|
||
pub fn copy_to_clipboard(s: &str) { | ||
match copy_impl(s, &PROVIDER) { | ||
Ok(()) => (), | ||
Err(err) => tracing::debug!("Unable to copy: {}", err.to_string()), | ||
} | ||
} | ||
|
||
#[allow(dead_code)] | ||
enum Provider { | ||
OSC52, | ||
Exec(&'static str, Vec<&'static str>), | ||
#[cfg(windows)] | ||
Win, | ||
NoOp, | ||
} | ||
|
||
#[cfg(windows)] | ||
fn detect_copy_provider() -> Provider { | ||
Provider::Win | ||
} | ||
|
||
#[cfg(target_os = "macos")] | ||
fn detect_copy_provider() -> Provider { | ||
if let Some(provider) = check_prog("pbcopy", &[]) { | ||
return provider; | ||
} | ||
Provider::OSC52 | ||
} | ||
|
||
#[cfg(not(any(target_os = "macos", target_os = "windows")))] | ||
fn detect_copy_provider() -> Provider { | ||
// Wayland | ||
if std::env::var("WAYLAND_DISPLAY").is_ok() { | ||
if let Some(provider) = check_prog("wl-copy", &["--type", "text/plain"]) { | ||
return provider; | ||
} | ||
} | ||
// X11 | ||
if std::env::var("DISPLAY").is_ok() { | ||
if let Some(provider) = check_prog("xclip", &["-i", "-selection", "clipboard"]) { | ||
return provider; | ||
} | ||
if let Some(provider) = check_prog("xsel", &["-i", "-b"]) { | ||
return provider; | ||
} | ||
} | ||
// Termux | ||
if let Some(provider) = check_prog("termux-clipboard-set", &[]) { | ||
return provider; | ||
} | ||
// Tmux | ||
if std::env::var("TMUX").is_ok() { | ||
if let Some(provider) = check_prog("tmux", &["load-buffer", "-"]) { | ||
return provider; | ||
} | ||
} | ||
|
||
Provider::OSC52 | ||
} | ||
|
||
#[allow(dead_code)] | ||
fn check_prog(cmd: &'static str, args: &[&'static str]) -> Option<Provider> { | ||
if which(cmd).is_ok() { | ||
Some(Provider::Exec(cmd, args.to_vec())) | ||
} else { | ||
None | ||
} | ||
} | ||
|
||
fn copy_impl(s: &str, provider: &Provider) -> std::io::Result<()> { | ||
match provider { | ||
Provider::OSC52 => { | ||
let mut stdout = std::io::stdout().lock(); | ||
use std::io::Write; | ||
write!( | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Just curious, what's the difference between this and |
||
&mut stdout, | ||
"\x1b]52;;{}\x07", | ||
base64::engine::general_purpose::STANDARD.encode(s) | ||
)?; | ||
} | ||
|
||
Provider::Exec(prog, args) => { | ||
let mut child = std::process::Command::new(prog) | ||
.args(args) | ||
.stdin(Stdio::piped()) | ||
.stdout(Stdio::null()) | ||
.stderr(Stdio::null()) | ||
.spawn() | ||
.unwrap(); | ||
std::io::Write::write_all(&mut child.stdin.as_ref().unwrap(), s.as_bytes())?; | ||
child.wait()?; | ||
} | ||
|
||
#[cfg(windows)] | ||
Provider::Win => { | ||
clipboard_win::set_clipboard_string(s).map_err(|e| anyhow::Error::msg(e.to_string()))? | ||
} | ||
|
||
Provider::NoOp => (), | ||
}; | ||
|
||
Ok(()) | ||
} | ||
|
||
lazy_static::lazy_static! { | ||
static ref PROVIDER: Provider = detect_copy_provider(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
would it make sense to fold these into an if statement with
cfg!
? That way the compiler can check the code paths