summaryrefslogtreecommitdiffstats
path: root/src/repl/handler.rs
diff options
context:
space:
mode:
authorsigoden <sigoden@gmail.com>2023-03-07 19:04:54 +0800
committerGitHub <noreply@github.com>2023-03-07 19:04:54 +0800
commitc7fcdb174440817706b818154a6bbcaf58948caa (patch)
tree4dabd9df9142cdceeca044e166545fd2cc26df36 /src/repl/handler.rs
parentd73be65b07c22f69539b5819f0684090c32d3b60 (diff)
downloadaichat-c7fcdb174440817706b818154a6bbcaf58948caa.tar.gz
refactor: adjust repl (#30)
- abandon .copy - rename .multiple => .editor - if command successed, do not output `Done` - if command failed, output `Error: <error message>`
Diffstat (limited to 'src/repl/handler.rs')
-rw-r--r--src/repl/handler.rs43
1 files changed, 24 insertions, 19 deletions
diff --git a/src/repl/handler.rs b/src/repl/handler.rs
index f2c34e2..add3eac 100644
--- a/src/repl/handler.rs
+++ b/src/repl/handler.rs
@@ -59,10 +59,9 @@ impl ReplCmdHandler {
self.state.borrow_mut().reply.clear();
return Ok(());
}
- let prompt = self.config.borrow().get_prompt();
let wg = WaitGroup::new();
let highlight = self.config.borrow().highlight;
- let mut stream_handler = if highlight {
+ let stream_handler = if highlight {
let (tx, rx) = unbounded();
let abort = self.abort.clone();
let wg = wg.clone();
@@ -74,43 +73,49 @@ impl ReplCmdHandler {
} else {
ReplyStreamHandler::new(None, self.abort.clone())
};
- self.client
- .send_message_streaming(&input, prompt, &mut stream_handler)?;
- let buffer = stream_handler.get_buffer();
- self.config.borrow().save_message(
- self.state.borrow_mut().save_file.as_mut(),
- &input,
- buffer,
- )?;
+ let ret = self.handle_send_stream(&input, stream_handler);
wg.wait();
- self.state.borrow_mut().reply = buffer.to_string();
+ self.state.borrow_mut().reply = ret?;
}
ReplCmd::SetRole(name) => {
let output = self.config.borrow_mut().change_role(&name);
- dump(output.trim(), 2);
+ dump(output, 1);
}
ReplCmd::ClearRole => {
self.config.borrow_mut().role = None;
- dump("Done", 2);
+ dump("", 1);
}
ReplCmd::Prompt(prompt) => {
- let output = self.config.borrow_mut().create_temp_role(&prompt);
- dump(output.trim(), 2);
+ self.config.borrow_mut().create_temp_role(&prompt);
+ dump("", 1);
}
ReplCmd::Info => {
let output = self.config.borrow().info()?;
- dump(output.trim(), 2);
+ dump(output, 1);
}
ReplCmd::UpdateConfig(input) => {
let output = self.config.borrow_mut().update(&input)?;
- dump(output.trim(), 2);
+ dump(output, 1);
}
}
Ok(())
}
- pub fn get_reply(&self) -> String {
- self.state.borrow().reply.to_string()
+ fn handle_send_stream(
+ &self,
+ input: &str,
+ mut stream_handler: ReplyStreamHandler,
+ ) -> Result<String> {
+ let prompt = self.config.borrow().get_prompt();
+ self.client
+ .send_message_streaming(input, prompt, &mut stream_handler)?;
+ let buffer = stream_handler.get_buffer();
+ self.config.borrow().save_message(
+ self.state.borrow_mut().save_file.as_mut(),
+ input,
+ buffer,
+ )?;
+ Ok(buffer.to_string())
}
}