@@ -50,28 +50,36 @@ fn execute_string(string: &str, shell: &mut Shell) {
5050 }
5151}
5252
53- fn print_line ( line : & [ u8 ] , mut cursor_position : usize , shell : & mut Shell , print_ps2 : bool ) {
54- clear_line ( ) ;
53+ fn flush_stdout ( ) {
54+ // this is a basic operation, if this doesn't work,
55+ // there's nothing else we can do
56+ io:: stdout ( ) . flush ( ) . expect ( "could not flush stdout" ) ;
57+ }
58+
59+ fn write_stdout ( bytes : & [ u8 ] ) {
60+ io:: stdout ( )
61+ . write_all ( bytes)
62+ . expect ( "failed to write to stdout" ) ;
63+ }
64+
65+ fn print_prompt ( shell : & mut Shell , print_ps2 : bool ) -> usize {
5566 if print_ps2 {
5667 let ps2 = shell. get_ps2 ( ) ;
5768 print ! ( "{}" , ps2) ;
58- cursor_position += ps2. len ( ) ;
69+ ps2. len ( )
5970 } else {
6071 let ps1 = shell. get_ps1 ( ) ;
6172 print ! ( "{}" , ps1) ;
62- cursor_position += ps1. len ( ) ;
73+ ps1. len ( )
6374 }
64- std:: io:: stdout ( ) . write_all ( & line) . unwrap ( ) ;
65- set_cursor_pos ( cursor_position) ;
66- io:: stdout ( ) . flush ( ) . unwrap ( ) ;
6775}
6876
6977fn standard_repl ( shell : & mut Shell ) {
7078 let mut program_buffer = Vec :: new ( ) ;
7179 let mut line_buffer = Vec :: new ( ) ;
7280 let mut print_ps2 = false ;
7381 clear_line ( ) ;
74- io :: stdout ( ) . flush ( ) . unwrap ( ) ;
82+ flush_stdout ( ) ;
7583 eprint ! ( "{}" , shell. get_ps1( ) ) ;
7684 loop {
7785 while let Some ( c) = read_nonblocking_char ( ) {
@@ -124,7 +132,12 @@ fn standard_repl(shell: &mut Shell) {
124132 }
125133 _ => { }
126134 }
127- print_line ( & line_buffer, line_buffer. len ( ) , shell, print_ps2) ;
135+ let mut cursor_position = line_buffer. len ( ) ;
136+ clear_line ( ) ;
137+ cursor_position += print_prompt ( shell, print_ps2) ;
138+ write_stdout ( & line_buffer) ;
139+ set_cursor_pos ( cursor_position) ;
140+ flush_stdout ( ) ;
128141 }
129142 std:: thread:: sleep ( Duration :: from_millis ( 16 ) ) ;
130143 shell. update_global_state ( ) ;
@@ -139,7 +152,7 @@ fn vi_repl(shell: &mut Shell) {
139152 let mut buffer = Vec :: new ( ) ;
140153 let mut print_ps2 = false ;
141154 clear_line ( ) ;
142- io :: stdout ( ) . flush ( ) . unwrap ( ) ;
155+ flush_stdout ( ) ;
143156 eprint ! ( "{}" , shell. get_ps1( ) ) ;
144157 loop {
145158 while let Some ( c) = read_nonblocking_char ( ) {
@@ -186,18 +199,10 @@ fn vi_repl(shell: &mut Shell) {
186199 }
187200 let mut cursor_position = editor. cursor_position ( ) ;
188201 clear_line ( ) ;
189- if print_ps2 {
190- let ps2 = shell. get_ps2 ( ) ;
191- print ! ( "{}" , ps2) ;
192- cursor_position += ps2. len ( ) ;
193- } else {
194- let ps1 = shell. get_ps1 ( ) ;
195- print ! ( "{}" , ps1) ;
196- cursor_position += ps1. len ( ) ;
197- }
198- std:: io:: stdout ( ) . write_all ( editor. current_line ( shell) ) . unwrap ( ) ;
202+ cursor_position += print_prompt ( shell, print_ps2) ;
203+ write_stdout ( editor. current_line ( shell) ) ;
199204 set_cursor_pos ( cursor_position) ;
200- io :: stdout ( ) . flush ( ) . unwrap ( ) ;
205+ flush_stdout ( )
201206 }
202207 std:: thread:: sleep ( Duration :: from_millis ( 16 ) ) ;
203208 shell. update_global_state ( ) ;
0 commit comments