Skip to content

Commit 85be9c3

Browse files
authored
fix: clean up chat changes (#293)
1 parent 93acda9 commit 85be9c3

File tree

2 files changed

+53
-51
lines changed

2 files changed

+53
-51
lines changed

crates/q_cli/src/cli/chat/mod.rs

Lines changed: 34 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ use spinners::{
4040
Spinner,
4141
Spinners,
4242
};
43+
use terminal::StdioOutput;
4344
use winnow::Partial;
4445
use 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! {"
134135
Hi, I'm Amazon Q. I can answer questions about your shell and CLI tools!
135136
You 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
}
Lines changed: 19 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,40 @@
11
use std::io::{
2+
self,
3+
Result,
24
Stderr,
35
Stdout,
46
Write,
57
};
68

7-
pub enum WriteOutput {
9+
pub enum StdioOutput {
10+
/// [Stderr] is used for interactive output
811
Interactive(Stderr),
12+
/// [Stdout] is used for non-interactive output
913
NonInteractive(Stdout),
1014
}
1115

12-
// wraps the stderr/stdout handle with a enum type.
13-
pub fn new(is_interactive: bool) -> WriteOutput {
14-
if is_interactive {
15-
WriteOutput::Interactive(std::io::stderr())
16-
} else {
17-
WriteOutput::NonInteractive(std::io::stdout())
16+
impl StdioOutput {
17+
pub fn new(is_interactive: bool) -> StdioOutput {
18+
if is_interactive {
19+
StdioOutput::Interactive(io::stderr())
20+
} else {
21+
StdioOutput::NonInteractive(io::stdout())
22+
}
1823
}
1924
}
2025

21-
impl Write for WriteOutput {
22-
fn write(&mut self, buf: &[u8]) -> std::io::Result<usize> {
26+
impl Write for StdioOutput {
27+
fn write(&mut self, buf: &[u8]) -> Result<usize> {
2328
match self {
24-
WriteOutput::Interactive(stderr) => stderr.write(buf),
25-
WriteOutput::NonInteractive(stdout) => stdout.write(buf),
29+
StdioOutput::Interactive(stderr) => stderr.write(buf),
30+
StdioOutput::NonInteractive(stdout) => stdout.write(buf),
2631
}
2732
}
2833

29-
fn flush(&mut self) -> std::io::Result<()> {
34+
fn flush(&mut self) -> Result<()> {
3035
match self {
31-
WriteOutput::Interactive(stderr) => stderr.flush(),
32-
WriteOutput::NonInteractive(stdout) => stdout.flush(),
36+
StdioOutput::Interactive(stderr) => stderr.flush(),
37+
StdioOutput::NonInteractive(stdout) => stdout.flush(),
3338
}
3439
}
3540
}

0 commit comments

Comments
 (0)