Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
95 changes: 81 additions & 14 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 3 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,9 @@ tracing-subscriber = { version = "0.3.18", features = ["env-filter"] }
walkdir = "2.4.0"
data-encoding = "2.6.0"
n0-future = "0.1.2"
base64 = { version = "0.22.1", optional = true }
hex = "0.4.3"
crossterm = { version = "0.29.0", features = ["event-stream", "osc52"], optional = true }
nix = { version = "0.29", features = ["signal"] }

[dev-dependencies]
duct = "0.13.6"
Expand All @@ -46,7 +47,7 @@ serde_json = "1.0.108"
tempfile = "3.8.1"

[features]
clipboard = ["dep:base64"]
clipboard = ["dep:crossterm"]
default = ["clipboard"]

[patch.crates-io]
Expand Down
61 changes: 43 additions & 18 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ use clap::{
CommandFactory, Parser, Subcommand,
};
use console::style;
use crossterm::terminal::{disable_raw_mode, enable_raw_mode};
use data_encoding::HEXLOWER;
use futures_buffered::BufferedStreamExt;
use indicatif::{
Expand Down Expand Up @@ -743,21 +744,49 @@ async fn send(args: SendArgs) -> anyhow::Result<()> {

#[cfg(feature = "clipboard")]
{
use console::{Key, Term};
use crossterm::event::{Event, EventStream, KeyCode, KeyEvent, KeyEventKind, KeyModifiers};
use nix::{
sys::signal::{kill, Signal},
unistd::Pid,
};

// Add command to the clipboard
if args.clipboard {
add_to_clipboard(&ticket);
}

let _keyboard = tokio::task::spawn(async move {
let term = Term::stdout();
println!("press c to copy command to clipboard, or use the --clipboard argument");
loop {
if let Ok(Key::Char('c')) = term.read_key() {
add_to_clipboard(&ticket);
}
}

// `enable_raw_mode` will remember the current terminal mode
Comment thread
dignifiedquire marked this conversation as resolved.
Outdated
// and restore it when `disable_raw_mode` is called.
enable_raw_mode().unwrap_or_else(|err| eprintln!("Failed to enable raw mode: {err}"));
let event_stream = EventStream::new();
event_stream
.for_each(move |e| match e {
Err(err) => eprintln!("Failed to process event: {err}\r"),
// c is pressed
Ok(Event::Key(KeyEvent {
code: KeyCode::Char('c'),
modifiers: KeyModifiers::NONE,
kind: KeyEventKind::Press,
..
})) => add_to_clipboard(&ticket),
// Ctrl+c is pressed
Ok(Event::Key(KeyEvent {
code: KeyCode::Char('c'),
modifiers: KeyModifiers::CONTROL,
kind: KeyEventKind::Press,
..
})) => {
disable_raw_mode()
.unwrap_or_else(|e| eprintln!("Failed to disable raw mode: {e}\r"));
kill(Pid::from_raw(0), Some(Signal::SIGINT))
.unwrap_or_else(|e| eprintln!("Failed to end process: {e}"));
}
Comment thread
Jisu-Woniu marked this conversation as resolved.
Outdated
_ => {}
})
.await
});
}

Expand All @@ -778,19 +807,15 @@ async fn send(args: SendArgs) -> anyhow::Result<()> {

#[cfg(feature = "clipboard")]
fn add_to_clipboard(ticket: &BlobTicket) {
use std::io::{stdout, Write};

use base64::prelude::{Engine, BASE64_STANDARD};
use std::io::stdout;

// Use OSC 52 to copy content to clipboard.
print!(
"\x1B]52;c;{}\x07",
BASE64_STANDARD.encode(format!("sendme receive {ticket}"))
);
use crossterm::{clipboard::CopyToClipboard, execute};

stdout()
.flush()
.unwrap_or_else(|e| eprintln!("Failed to flush stdout: {e}"));
execute!(
stdout(),
CopyToClipboard::to_clipboard_from(format!("sendme receive {ticket}"))
)
.unwrap_or_else(|e| eprintln!("Failed to copy to clipboard: {e}"));
}

const TICK_MS: u64 = 250;
Expand Down
Loading