summaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/config.rs24
-rw-r--r--src/main.rs2
-rw-r--r--src/repl/handler.rs43
-rw-r--r--src/repl/mod.rs30
-rw-r--r--src/utils.rs8
5 files changed, 41 insertions, 66 deletions
diff --git a/src/config.rs b/src/config.rs
index 7e977be..dc07c95 100644
--- a/src/config.rs
+++ b/src/config.rs
@@ -19,14 +19,6 @@ const ROLES_FILE_NAME: &str = "roles.yaml";
const HISTORY_FILE_NAME: &str = "history.txt";
const MESSAGE_FILE_NAME: &str = "messages.md";
const TEMP_ROLE_NAME: &str = "%TEMP%";
-const UPDATE_KEYS: [&str; 6] = [
- "api_key",
- "temperature",
- "save",
- "highlight",
- "proxy",
- "dry_run",
-];
const SET_COMPLETIONS: [&str; 9] = [
".set api_key",
".set temperature",
@@ -186,18 +178,17 @@ impl Config {
Some(role) => {
let output = format!("{}>> {}", role.name, role.prompt.trim());
self.role = Some(role);
- output
+ format!("{}\n", output.trim())
}
- None => "Unknown role".into(),
+ None => "Error: Unknown role\n".into(),
}
}
- pub fn create_temp_role(&mut self, prompt: &str) -> String {
+ pub fn create_temp_role(&mut self, prompt: &str) {
self.role = Some(Role {
name: TEMP_ROLE_NAME.into(),
prompt: prompt.into(),
});
- "Done".into()
}
pub fn get_prompt(&self) -> Option<String> {
@@ -302,14 +293,9 @@ impl Config {
let value = value.parse().with_context(|| "Invalid value")?;
self.dry_run = value;
}
- _ => {
- return Ok(format!(
- "Unknown key, valid keys are {}",
- UPDATE_KEYS.join(", ")
- ))
- }
+ _ => return Ok(format!("Error: Unknown key `{key}`\n")),
}
- Ok("Done".into())
+ Ok("".into())
}
fn load_roles(&mut self) -> Result<()> {
diff --git a/src/main.rs b/src/main.rs
index 8f7b1a2..f1ae303 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -69,7 +69,7 @@ fn start_directive(client: ChatGptClient, config: SharedConfig, input: &str) ->
let output = output.trim();
if config.borrow().highlight && stdout().is_terminal() {
let mut markdown_render = MarkdownRender::new();
- markdown_render.render(output);
+ println!("{}", markdown_render.render(output))
} else {
println!("{output}");
}
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() {
diff --git a/src/utils.rs b/src/utils.rs
index 9dfbbd7..5a18948 100644
--- a/src/utils.rs
+++ b/src/utils.rs
@@ -1,6 +1,4 @@
-use anyhow::{anyhow, Result};
use chrono::prelude::*;
-use copypasta::{ClipboardContext, ClipboardProvider};
use std::io::{stdout, Write};
pub fn dump<T: ToString>(text: T, newlines: usize) {
@@ -8,12 +6,6 @@ pub fn dump<T: ToString>(text: T, newlines: usize) {
let _ = stdout().flush();
}
-pub fn copy(src: &str) -> Result<()> {
- ClipboardContext::new()
- .and_then(|mut ctx| ctx.set_contents(src.to_string()))
- .map_err(|err| anyhow!("Failed to copy, {err}"))
-}
-
pub fn now() -> String {
let now = Local::now();
now.to_rfc3339_opts(SecondsFormat::Secs, false)