@@ -15,7 +15,6 @@ use clap::{
1515 CommandFactory , Parser , Subcommand ,
1616} ;
1717use console:: style;
18- use crossterm:: terminal:: { disable_raw_mode, enable_raw_mode} ;
1918use data_encoding:: HEXLOWER ;
2019use futures_buffered:: BufferedStreamExt ;
2120use indicatif:: {
@@ -743,72 +742,7 @@ async fn send(args: SendArgs) -> anyhow::Result<()> {
743742 println ! ( "sendme receive {ticket}" ) ;
744743
745744 #[ 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- }
745+ handle_key_press ( args. clipboard , ticket) ;
812746
813747 tokio:: signal:: ctrl_c ( ) . await ?;
814748
@@ -825,6 +759,75 @@ async fn send(args: SendArgs) -> anyhow::Result<()> {
825759 Ok ( ( ) )
826760}
827761
762+ #[ cfg( feature = "clipboard" ) ]
763+ fn handle_key_press ( set_clipboard : bool , ticket : BlobTicket ) {
764+ use crossterm:: {
765+ event:: { Event , EventStream , KeyCode , KeyEvent , KeyEventKind , KeyModifiers } ,
766+ terminal:: { disable_raw_mode, enable_raw_mode} ,
767+ } ;
768+
769+ #[ cfg( any( unix, windows) ) ]
770+ use std:: io;
771+
772+ #[ cfg( unix) ]
773+ use libc:: { raise, SIGINT } ;
774+
775+ #[ cfg( windows) ]
776+ use windows_sys:: Win32 :: System :: Console :: { GenerateConsoleCtrlEvent , CTRL_C_EVENT } ;
777+
778+ if set_clipboard {
779+ add_to_clipboard ( & ticket) ;
780+ }
781+
782+ let _keyboard = tokio:: task:: spawn ( async move {
783+ println ! ( "press c to copy command to clipboard, or use the --clipboard argument" ) ;
784+
785+ // `enable_raw_mode` will remember the current terminal mode
786+ // and restore it when `disable_raw_mode` is called.
787+ enable_raw_mode ( ) . unwrap_or_else ( |err| eprintln ! ( "Failed to enable raw mode: {err}" ) ) ;
788+ EventStream :: new ( )
789+ . for_each ( move |e| match e {
790+ Err ( err) => eprintln ! ( "Failed to process event: {err}" ) ,
791+ // c is pressed
792+ Ok ( Event :: Key ( KeyEvent {
793+ code : KeyCode :: Char ( 'c' ) ,
794+ modifiers : KeyModifiers :: NONE ,
795+ kind : KeyEventKind :: Press ,
796+ ..
797+ } ) ) => add_to_clipboard ( & ticket) ,
798+ // Ctrl+c is pressed
799+ Ok ( Event :: Key ( KeyEvent {
800+ code : KeyCode :: Char ( 'c' ) ,
801+ modifiers : KeyModifiers :: CONTROL ,
802+ kind : KeyEventKind :: Press ,
803+ ..
804+ } ) ) => {
805+ disable_raw_mode ( )
806+ . unwrap_or_else ( |e| eprintln ! ( "Failed to disable raw mode: {e}" ) ) ;
807+
808+ #[ cfg( unix) ]
809+ // Safety: Raw syscall to re-send the SIGINT signal to the console.
810+ // `raise` returns nonzero for failure.
811+ if unsafe { raise ( SIGINT ) } != 0 {
812+ eprintln ! ( "Failed to raise signal: {}" , io:: Error :: last_os_error( ) ) ;
813+ }
814+
815+ #[ cfg( windows) ]
816+ // Safety: Raw syscall to re-send the `CTRL_C_EVENT` to the console.
817+ // `GenerateConsoleCtrlEvent` returns 0 for failure.
818+ if unsafe { GenerateConsoleCtrlEvent ( CTRL_C_EVENT , 0 ) } == 0 {
819+ eprintln ! (
820+ "Failed to generate console event: {}" ,
821+ io:: Error :: last_os_error( )
822+ ) ;
823+ }
824+ }
825+ _ => { }
826+ } )
827+ . await
828+ } ) ;
829+ }
830+
828831#[ cfg( feature = "clipboard" ) ]
829832fn add_to_clipboard ( ticket : & BlobTicket ) {
830833 use std:: io:: stdout;
0 commit comments