@@ -40,6 +40,7 @@ use spinners::{
4040 Spinner ,
4141 Spinners ,
4242} ;
43+ use terminal:: StdioOutput ;
4344use winnow:: Partial ;
4445use winnow:: stream:: Offset ;
4546
@@ -74,9 +75,10 @@ pub async fn chat(mut input: String) -> Result<ExitCode> {
7475
7576 if !is_interactive {
7677 // append to input string any extra info that was provided.
77- stdin. lock ( ) . read_to_string ( & mut input) . unwrap ( ) ;
78+ stdin. lock ( ) . read_to_string ( & mut input) ? ;
7879 }
79- let mut output = terminal:: new ( is_interactive) ;
80+
81+ let mut output = StdioOutput :: new ( is_interactive) ;
8082 let result = try_chat ( & mut output, input, is_interactive) . await ;
8183
8284 if is_interactive {
@@ -129,27 +131,23 @@ async fn try_chat<W: Write>(output: &mut W, mut input: String, interactive: bool
129131 } else if fig_settings:: settings:: get_bool_or ( "chat.greeting.enabled" , true ) {
130132 execute ! (
131133 output,
132- style:: Print ( format!(
133- "
134+ style:: Print ( color_print:: cstr! { "
134135Hi, I'm Amazon Q. I can answer questions about your shell and CLI tools!
135136You can include additional context by adding the following to your prompt:
136137
137- {} to pass your shell history
138- {} to pass information about your current git repository
139- {} to pass your shell environment
140- " ,
141- "@history" . bold( ) ,
142- "@git" . bold( ) ,
143- "@env" . bold( )
144- ) )
138+ <em>@history</em> to pass your shell history
139+ <em>@git</em> to pass information about your current git repository
140+ <em>@env</em> to pass your shell environment
141+
142+ "
143+ } )
145144 ) ?;
146145 }
147146 }
148147
149148 // Print response as we receive it
150149 if let Some ( rx) = & mut rx {
151- // compiler complains about unused variable for spinner (bad global state usage)
152- let mut _spinner = if interactive {
150+ let mut spinner = if interactive {
153151 queue ! ( output, cursor:: Hide ) ?;
154152 Some ( Spinner :: new ( Spinners :: Dots , "Generating your answer..." . to_owned ( ) ) )
155153 } else {
@@ -193,7 +191,7 @@ You can include additional context by adding the following to your prompt:
193191 } ,
194192 ApiResponse :: Error ( error) => {
195193 if interactive {
196- _spinner = None ;
194+ drop ( spinner . take ( ) ) ;
197195 queue ! ( output, cursor:: MoveToColumn ( 0 ) ) ?;
198196
199197 match error {
@@ -232,7 +230,7 @@ You can include additional context by adding the following to your prompt:
232230 }
233231
234232 if !buf. is_empty ( ) && interactive {
235- _spinner = None ;
233+ drop ( spinner . take ( ) ) ;
236234 queue ! (
237235 output,
238236 crossterm:: terminal:: Clear ( crossterm:: terminal:: ClearType :: CurrentLine ) ,
@@ -248,7 +246,6 @@ You can include additional context by adding the following to your prompt:
248246 Ok ( parsed) => {
249247 offset += parsed. offset_from ( & input) ;
250248 output. flush ( ) ?;
251- // output.lock().flush()?;
252249 state. newline = state. set_newline ;
253250 state. set_newline = false ;
254251 } ,
@@ -295,26 +292,26 @@ You can include additional context by adding the following to your prompt:
295292 }
296293 }
297294
298- if !interactive {
299- break Ok ( ( ) ) ;
300- }
301- loop {
302- let readline = rl . as_mut ( ) . unwrap ( ) . readline ( PROMPT ) ;
303- match readline {
304- Ok ( line) => {
305- if line . trim ( ) . is_empty ( ) {
306- continue ;
307- }
308- let _ = rl . as_mut ( ) . unwrap ( ) . add_history_entry ( line. as_str ( ) ) ;
309- input = line ;
310- break ;
311- } ,
312- Err ( ReadlineError :: Interrupted | ReadlineError :: Eof ) => {
313- return Ok ( ( ) ) ;
314- } ,
315- Err ( err) => {
316- return Err ( err . into ( ) ) ;
317- } ,
295+ // rl is Some if the chat is interactive
296+ if let Some ( rl ) = rl . as_mut ( ) {
297+ loop {
298+ let readline = rl . readline ( PROMPT ) ;
299+ match readline {
300+ Ok ( line ) => {
301+ if line. trim ( ) . is_empty ( ) {
302+ continue ;
303+ }
304+ let _ = rl . add_history_entry ( line . as_str ( ) ) ;
305+ input = line;
306+ break ;
307+ } ,
308+ Err ( ReadlineError :: Interrupted | ReadlineError :: Eof ) => {
309+ return Ok ( ( ) ) ;
310+ } ,
311+ Err ( err ) => {
312+ return Err ( err. into ( ) ) ;
313+ } ,
314+ }
318315 }
319316 }
320317 }
0 commit comments