summaryrefslogtreecommitdiffstats
path: root/src/repl
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
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')
-rw-r--r--src/repl/handler.rs43
-rw-r--r--src/repl/mod.rs30
2 files changed, 35 insertions, 38 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())
}
}
diff --git a/src/repl/mod.rs b/src/repl/mod.rs
index 30e1c5e..e08b798 100644
--- a/src/repl/mod.rs
+++ b/src/repl/mod.rs
@@ -5,7 +5,7 @@ mod init;
use crate::client::ChatGptClient;
use crate::config::SharedConfig;
use crate::term;
-use crate::utils::{copy, dump};
+use crate::utils::dump;
use anyhow::{Context, Result};
use reedline::{DefaultPrompt, Reedline, Signal};
@@ -14,17 +14,15 @@ use std::sync::Arc;
pub use self::abort::*;
pub use self::handler::*;
-pub const REPL_COMMANDS: [(&str, &str, bool); 12] = [
+pub const REPL_COMMANDS: [(&str, &str, bool); 10] = [
(".info", "Print the information", false),
(".set", "Modify the configuration temporarily", false),
+ (".prompt", "Add a GPT prompt", true),
(".role", "Select a role", false),
(".clear role", "Clear the currently selected role", false),
- (".prompt", "Add prompt, aka create a temporary role", true),
(".history", "Print the history", false),
(".clear history", "Clear the history", false),
- (".clear screen", "Clear the screen", false),
- (".multiline", "Enter multiline editor mode", true),
- (".copy", "Copy last reply message", false),
+ (".editor", "Enter editor mode for multiline input", true),
(".help", "Print this help message", false),
(".exit", "Exit the REPL", false),
];
@@ -122,10 +120,10 @@ impl Repl {
".info" => {
handler.handle(ReplCmd::Info)?;
}
- ".multiline" => {
+ ".editor" => {
let mut text = args.unwrap_or_default().to_string();
if text.is_empty() {
- dump("Usage: .multiline { <your multiline content> }", 2);
+ dump("Usage: .editor { <your multiline/paste content> }", 2);
} else {
if text.starts_with('{') && text.ends_with('}') {
text = text[1..text.len() - 1].to_string()
@@ -133,22 +131,13 @@ impl Repl {
handler.handle(ReplCmd::Submit(text))?;
}
}
- ".copy" => {
- let reply = handler.get_reply();
- if reply.is_empty() {
- dump("No reply messages that can be copied", 1)
- } else {
- copy(&reply)?;
- dump("Copied", 1);
- }
- }
".set" => {
handler.handle(ReplCmd::UpdateConfig(args.unwrap_or_default().to_string()))?
}
".prompt" => {
let mut text = args.unwrap_or_default().to_string();
if text.is_empty() {
- dump("Usage: .prompt { <your multiline content> }.", 2);
+ dump("Usage: .prompt { <your multiline/paste content> }.", 2);
} else {
if text.starts_with('{') && text.ends_with('}') {
text = text[1..text.len() - 1].to_string()
@@ -167,7 +156,10 @@ impl Repl {
}
fn dump_unknown_command() {
- dump("Unknown command. Type \".help\" for more information.", 2);
+ dump(
+ "Error: Unknown command. Type \".help\" for more information.",
+ 2,
+ );
}
fn dump_repl_help() {