11use crate :: Args ;
2- use crate :: app:: Action :: { CommandCompleted , ResetHighlight , StdinRead , UserInput } ;
2+ use crate :: app:: Action :: { CommandCompleted , Debounced , ResetHighlight , StdinRead , UserInput } ;
3+ use crate :: completion:: ShCompleter ;
34use crate :: config:: { KeyBindingsConfig , ThemeConfig , history_path} ;
45use crate :: debouncer:: debouncer_task;
56use crate :: history:: History ;
@@ -8,11 +9,10 @@ use crate::rura::ExecuteType;
89use crate :: rura_widget:: RuraWidget ;
910use crate :: theme:: Theme ;
1011use crate :: uicmd:: { KeyBindings , UiCmd , to_ui_command} ;
11- use Action :: Debounced ;
1212use crossterm:: event:: KeyCode :: Char ;
1313use crossterm:: event:: { KeyCode , KeyModifiers } ;
1414use crossterm:: tty:: IsTty ;
15- use log:: debug;
15+ use log:: { debug, info } ;
1616use ratatui:: crossterm:: event;
1717use ratatui:: crossterm:: event:: Event ;
1818use ratatui:: layout:: { Constraint , Direction , Layout , Margin , Rect } ;
@@ -33,12 +33,11 @@ use std::thread;
3333use std:: time:: Duration ;
3434use tui_input:: Input ;
3535use tui_popup:: Popup ;
36- use crate :: completion:: BashCompleter ;
3736
3837pub struct App {
3938 rura_widget : RuraWidget ,
4039 output_widget : OutputWidget ,
41- stdin : String ,
40+ stdin : Output ,
4241 exit : bool ,
4342 action_rx : Receiver < Action > ,
4443 command_tx : Sender < ( String , String ) > ,
@@ -113,7 +112,7 @@ impl App {
113112 key_bindings : KeyBindings :: from_config ( & kb_config) ,
114113 highlight_reset_tx,
115114 completions : None ,
116- completer : Box :: new ( BashCompleter { } ) ,
115+ completer : Box :: new ( ShCompleter { } ) ,
117116 } ,
118117 output_widget : OutputWidget :: new (
119118 theme_config,
@@ -124,7 +123,7 @@ impl App {
124123 } ,
125124 error_display_mode,
126125 ) ,
127- stdin : "" . to_string ( ) ,
126+ stdin : Output :: ok ( "" ) ,
128127 action_rx,
129128 command_tx,
130129 debouncer_tx,
@@ -154,10 +153,13 @@ impl App {
154153 UserInput ( event) => self . handle_event ( & event) ,
155154 CommandCompleted ( output) => self . output_widget . handle_command_output ( output) ,
156155 ResetHighlight => self . rura_widget . highlight_until = None ,
157- StdinRead ( stdin) => {
158- self . stdin = stdin;
159- self . output_widget
160- . handle_command_output ( Output :: ok ( & self . stdin ) )
156+ StdinRead ( output) => {
157+ if output. ok {
158+ self . stdin = output. clone ( ) ;
159+ } else {
160+ self . stdin = Output :: ok ( "" ) ;
161+ }
162+ self . output_widget . handle_command_output ( output)
161163 }
162164 Debounced => {
163165 match self . input_mode {
@@ -256,8 +258,8 @@ impl App {
256258 self . handle_execute ( ExecuteType :: UntilCurrentPrev )
257259 }
258260 UiCmd :: ResetInput => {
259- let new_output = Output :: ok ( & self . stdin ) ;
260- self . output_widget . handle_command_output ( new_output ) ;
261+ let stdin = self . stdin . clone ( ) ;
262+ self . output_widget . handle_command_output ( stdin ) ;
261263 }
262264 UiCmd :: HistoryNext => {
263265 // disable history for live mode
@@ -297,10 +299,14 @@ impl App {
297299
298300 fn handle_execute ( & mut self , kind : ExecuteType ) {
299301 match self . rura_widget . execute ( kind) {
300- Some ( command) if command. is_empty ( ) => self
301- . output_widget
302- . handle_command_output ( Output :: ok ( & self . stdin ) ) ,
303- Some ( c) => self . command_tx . send ( ( c, self . stdin . clone ( ) ) ) . unwrap ( ) ,
302+ Some ( command) if command. is_empty ( ) => {
303+ let stdin = self . stdin . clone ( ) ;
304+ self . output_widget . handle_command_output ( stdin)
305+ }
306+ Some ( c) => self
307+ . command_tx
308+ . send ( ( c, self . stdin . lines . join ( "\n " ) ) )
309+ . unwrap ( ) ,
304310 None => { }
305311 }
306312 }
@@ -495,7 +501,7 @@ fn handle_command_task(
495501) -> Result < ( ) , Box < dyn Error > > {
496502 loop {
497503 if let Ok ( ( command, stdin) ) = command_rx. recv ( ) {
498- debug ! ( "executing command: {command}" ) ;
504+ info ! ( "executing command: {command}" ) ;
499505
500506 let mut cmd = Command :: new ( "sh" ) ;
501507 cmd. args ( [ "-c" , & command] ) ;
@@ -538,32 +544,41 @@ fn handle_command_task(
538544fn handle_input_task ( tx : Sender < Action > ) -> Result < ( ) , Box < dyn Error > > {
539545 loop {
540546 if let Ok ( event) = event:: read ( ) {
541- // debug!("event: {:?}", event);
547+ debug ! ( "event: {:?}" , event) ;
542548 tx. send ( UserInput ( event) ) ?
543549 }
544550 }
545551}
546552
547553fn read_stdin_task ( file_opt : Option < String > , tx : Sender < Action > ) -> Result < ( ) , Box < dyn Error > > {
548554 if let Some ( file) = file_opt {
549- debug ! ( "reading file {file}" ) ;
550- let file_content = std:: fs:: read_to_string ( file) . expect ( "Failed to read file" ) ;
551- tx. send ( StdinRead ( file_content) ) ?;
555+ info ! ( "reading file {file}" ) ;
556+ let file_content = std:: fs:: read_to_string ( file) ;
557+ match file_content {
558+ Ok ( content) => {
559+ tx. send ( StdinRead ( Output :: ok ( & content) ) ) ?;
560+ }
561+ Err ( err) => {
562+ tx. send ( StdinRead ( Output :: err ( & err. to_string ( ) , None ) ) ) ?;
563+ }
564+ }
552565 Ok ( ( ) )
553566 } else {
554- let mut input = String :: new ( ) ;
567+ let mut buff = String :: new ( ) ;
555568 let tty = stdin ( ) . is_tty ( ) ;
556- debug ! ( "tty? {tty}" ) ;
557569 if !tty {
558- debug ! ( "reading input" ) ;
559- stdin ( )
560- . read_to_string ( & mut input)
561- . expect ( "Failed to read input" ) ;
570+ let result = stdin ( ) . read_to_string ( & mut buff) ;
562571
563- tx. send ( StdinRead ( input) ) ?;
572+ match result {
573+ Ok ( _) => {
574+ tx. send ( StdinRead ( Output :: ok ( & buff) ) ) ?;
575+ }
576+ Err ( e) => {
577+ tx. send ( StdinRead ( Output :: err ( e. to_string ( ) . as_str ( ) , None ) ) ) ?;
578+ }
579+ }
564580 Ok ( ( ) )
565581 } else {
566- debug ! ( "skipping input" ) ;
567582 Ok ( ( ) )
568583 }
569584 }
@@ -585,7 +600,7 @@ fn reset_highlight_task(
585600enum Action {
586601 UserInput ( Event ) ,
587602 CommandCompleted ( Output ) ,
588- StdinRead ( String ) ,
603+ StdinRead ( Output ) ,
589604 ResetHighlight ,
590605 Debounced ,
591606}
@@ -641,15 +656,15 @@ mod tests {
641656 key_bindings : KeyBindings :: from_config ( & kb_config) ,
642657 highlight_reset_tx,
643658 completions : None ,
644- completer : Box :: new ( BashCompleter { } ) ,
659+ completer : Box :: new ( ShCompleter { } ) ,
645660 } ,
646661 output_widget : OutputWidget :: new (
647662 & theme_config,
648663 & kb_config,
649664 ErrorPanePlacement :: Bottom ,
650665 ErrorDisplayMode :: Pane ,
651666 ) ,
652- stdin : "" . into ( ) ,
667+ stdin : Output :: ok ( "" ) ,
653668 action_rx,
654669 command_tx,
655670 debouncer_tx,
0 commit comments