Skip to content

Commit 884c25d

Browse files
committed
Extract handle_key_press into a function.
1 parent e0a3929 commit 884c25d

File tree

1 file changed

+67
-66
lines changed

1 file changed

+67
-66
lines changed

src/main.rs

Lines changed: 67 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -743,72 +743,7 @@ async fn send(args: SendArgs) -> anyhow::Result<()> {
743743
println!("sendme receive {ticket}");
744744

745745
#[cfg(feature = "clipboard")]
746-
{
747-
use crossterm::event::{Event, EventStream, KeyCode, KeyEvent, KeyEventKind, KeyModifiers};
748-
749-
#[cfg(any(unix, windows))]
750-
use std::io;
751-
752-
#[cfg(unix)]
753-
use libc::{raise, SIGINT};
754-
755-
#[cfg(windows)]
756-
use windows_sys::Win32::System::Console::{GenerateConsoleCtrlEvent, CTRL_C_EVENT};
757-
758-
// Add command to the clipboard
759-
if args.clipboard {
760-
add_to_clipboard(&ticket);
761-
}
762-
763-
let _keyboard = tokio::task::spawn(async move {
764-
println!("press c to copy command to clipboard, or use the --clipboard argument");
765-
766-
// `enable_raw_mode` will remember the current terminal mode
767-
// and restore it when `disable_raw_mode` is called.
768-
enable_raw_mode().unwrap_or_else(|err| eprintln!("Failed to enable raw mode: {err}"));
769-
let event_stream = EventStream::new();
770-
event_stream
771-
.for_each(move |e| match e {
772-
Err(err) => eprintln!("Failed to process event: {err}"),
773-
// c is pressed
774-
Ok(Event::Key(KeyEvent {
775-
code: KeyCode::Char('c'),
776-
modifiers: KeyModifiers::NONE,
777-
kind: KeyEventKind::Press,
778-
..
779-
})) => add_to_clipboard(&ticket),
780-
// Ctrl+c is pressed
781-
Ok(Event::Key(KeyEvent {
782-
code: KeyCode::Char('c'),
783-
modifiers: KeyModifiers::CONTROL,
784-
kind: KeyEventKind::Press,
785-
..
786-
})) => {
787-
disable_raw_mode()
788-
.unwrap_or_else(|e| eprintln!("Failed to disable raw mode: {e}"));
789-
790-
#[cfg(unix)]
791-
// Safety: Raw syscall to re-send the SIGINT signal to the console.
792-
// `raise` returns nonzero for failure.
793-
if unsafe { raise(SIGINT) } != 0 {
794-
eprintln!("Failed to raise signal: {}", io::Error::last_os_error());
795-
}
796-
797-
#[cfg(windows)]
798-
// Safety: Raw syscall to re-send the Ctrl+C event to the console.
799-
// `GenerateConsoleCtrlEvent` returns 0 for failure.
800-
if unsafe { GenerateConsoleCtrlEvent(CTRL_C_EVENT, 0) } == 0 {
801-
eprintln!(
802-
"Failed to generate console event: {}",
803-
io::Error::last_os_error()
804-
);
805-
}
806-
}
807-
_ => {}
808-
})
809-
.await
810-
});
811-
}
746+
handle_key_press(args.clipboard, ticket);
812747

813748
tokio::signal::ctrl_c().await?;
814749

@@ -825,6 +760,72 @@ async fn send(args: SendArgs) -> anyhow::Result<()> {
825760
Ok(())
826761
}
827762

763+
#[cfg(feature = "clipboard")]
764+
fn handle_key_press(set_clipboard: bool, ticket: BlobTicket) {
765+
use crossterm::event::{Event, EventStream, KeyCode, KeyEvent, KeyEventKind, KeyModifiers};
766+
767+
#[cfg(any(unix, windows))]
768+
use std::io;
769+
770+
#[cfg(unix)]
771+
use libc::{raise, SIGINT};
772+
773+
#[cfg(windows)]
774+
use windows_sys::Win32::System::Console::{GenerateConsoleCtrlEvent, CTRL_C_EVENT};
775+
776+
if set_clipboard {
777+
add_to_clipboard(&ticket);
778+
}
779+
780+
let _keyboard = tokio::task::spawn(async move {
781+
println!("press c to copy command to clipboard, or use the --clipboard argument");
782+
783+
// `enable_raw_mode` will remember the current terminal mode
784+
// and restore it when `disable_raw_mode` is called.
785+
enable_raw_mode().unwrap_or_else(|err| eprintln!("Failed to enable raw mode: {err}"));
786+
EventStream::new()
787+
.for_each(move |e| match e {
788+
Err(err) => eprintln!("Failed to process event: {err}"),
789+
// c is pressed
790+
Ok(Event::Key(KeyEvent {
791+
code: KeyCode::Char('c'),
792+
modifiers: KeyModifiers::NONE,
793+
kind: KeyEventKind::Press,
794+
..
795+
})) => add_to_clipboard(&ticket),
796+
// Ctrl+c is pressed
797+
Ok(Event::Key(KeyEvent {
798+
code: KeyCode::Char('c'),
799+
modifiers: KeyModifiers::CONTROL,
800+
kind: KeyEventKind::Press,
801+
..
802+
})) => {
803+
disable_raw_mode()
804+
.unwrap_or_else(|e| eprintln!("Failed to disable raw mode: {e}"));
805+
806+
#[cfg(unix)]
807+
// Safety: Raw syscall to re-send the SIGINT signal to the console.
808+
// `raise` returns nonzero for failure.
809+
if unsafe { raise(SIGINT) } != 0 {
810+
eprintln!("Failed to raise signal: {}", io::Error::last_os_error());
811+
}
812+
813+
#[cfg(windows)]
814+
// Safety: Raw syscall to re-send the `CTRL_C_EVENT` to the console.
815+
// `GenerateConsoleCtrlEvent` returns 0 for failure.
816+
if unsafe { GenerateConsoleCtrlEvent(CTRL_C_EVENT, 0) } == 0 {
817+
eprintln!(
818+
"Failed to generate console event: {}",
819+
io::Error::last_os_error()
820+
);
821+
}
822+
}
823+
_ => {}
824+
})
825+
.await
826+
});
827+
}
828+
828829
#[cfg(feature = "clipboard")]
829830
fn add_to_clipboard(ticket: &BlobTicket) {
830831
use std::io::stdout;

0 commit comments

Comments
 (0)