feat: add .read command (#145)

pull/151/head
Leo Bergnéhr 8 months ago committed by GitHub
parent 66fd547c0f
commit c3f9ef6c39
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -3,6 +3,8 @@ use crate::config::SharedConfig;
use crate::print_now; use crate::print_now;
use crate::render::render_stream; use crate::render::render_stream;
use crate::utils::copy; use crate::utils::copy;
use std::fs;
use std::io::Read;
use super::abort::SharedAbortSignal; use super::abort::SharedAbortSignal;
@ -22,6 +24,7 @@ pub enum ReplCmd {
StartConversation, StartConversation,
EndConversatoin, EndConversatoin,
Copy, Copy,
ReadFile(String),
} }
#[allow(clippy::module_name_repetitions)] #[allow(clippy::module_name_repetitions)]
@ -105,6 +108,12 @@ impl ReplCmdHandler {
copy(&self.reply.borrow()).with_context(|| "Failed to copy the last output")?; copy(&self.reply.borrow()).with_context(|| "Failed to copy the last output")?;
print_now!("\n"); print_now!("\n");
} }
ReplCmd::ReadFile(file) => {
let mut contents = String::new();
let mut file = fs::File::open(file).expect("Unable to open file");
file.read_to_string(&mut contents).expect("Unable to read file");
self.handle(ReplCmd::Submit(contents))?;
}
} }
Ok(()) Ok(())
} }

@ -17,7 +17,7 @@ use anyhow::{Context, Result};
use reedline::Signal; use reedline::Signal;
use std::rc::Rc; use std::rc::Rc;
pub const REPL_COMMANDS: [(&str, &str); 13] = [ pub const REPL_COMMANDS: [(&str, &str); 14] = [
(".info", "Print system-wide information"), (".info", "Print system-wide information"),
(".set", "Modify the configuration temporarily"), (".set", "Modify the configuration temporarily"),
(".model", "Choose a model"), (".model", "Choose a model"),
@ -31,6 +31,7 @@ pub const REPL_COMMANDS: [(&str, &str); 13] = [
(".clear history", "Clear the history"), (".clear history", "Clear the history"),
(".help", "Print this help message"), (".help", "Print this help message"),
(".exit", "Exit the REPL"), (".exit", "Exit the REPL"),
(".read", "Read the contents of a file into the prompt"),
]; ];
impl Repl { impl Repl {
@ -118,6 +119,10 @@ impl Repl {
Some(name) => handler.handle(ReplCmd::SetRole(name.to_string()))?, Some(name) => handler.handle(ReplCmd::SetRole(name.to_string()))?,
None => print_now!("Usage: .role <name>\n\n"), None => print_now!("Usage: .role <name>\n\n"),
}, },
".read" => match args {
Some(file) => handler.handle(ReplCmd::ReadFile(file.to_string()))?,
None => print_now!("Usage: .read <file name>\n\n"),
},
".info" => { ".info" => {
handler.handle(ReplCmd::ViewInfo)?; handler.handle(ReplCmd::ViewInfo)?;
} }

Loading…
Cancel
Save