Skip to content

Commit 6d144b0

Browse files
committed
Extract handle_key_press into a function.
1 parent e0a3929 commit 6d144b0

File tree

1 file changed

+66
-66
lines changed

1 file changed

+66
-66
lines changed

src/main.rs

Lines changed: 66 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,71 @@ async fn send(args: SendArgs) -> anyhow::Result<()> {
825760
Ok(())
826761
}
827762

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

0 commit comments

Comments
 (0)