diff options
| author | Leo Bergnéhr <leo@bergnehr.se> | 2023-10-26 13:25:53 +0200 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2023-10-26 19:25:53 +0800 |
| commit | c3f9ef6c396ba692e82cdfaa929429c85eebd289 (patch) | |
| tree | c2fb59abefe4618f3f0cbf0094f82d8e8c91e97c /src | |
| parent | 66fd547c0fac30d98565a92d93824237a4813669 (diff) | |
| download | aichat-c3f9ef6c396ba692e82cdfaa929429c85eebd289.tar.gz | |
feat: add .read command (#145)
Diffstat (limited to 'src')
| -rw-r--r-- | src/repl/handler.rs | 9 | ||||
| -rw-r--r-- | src/repl/mod.rs | 7 |
2 files changed, 15 insertions, 1 deletions
diff --git a/src/repl/handler.rs b/src/repl/handler.rs index e512cb5..45836ba 100644 --- a/src/repl/handler.rs +++ b/src/repl/handler.rs @@ -3,6 +3,8 @@ use crate::config::SharedConfig; use crate::print_now; use crate::render::render_stream; use crate::utils::copy; +use std::fs; +use std::io::Read; use super::abort::SharedAbortSignal; @@ -22,6 +24,7 @@ pub enum ReplCmd { StartConversation, EndConversatoin, Copy, + ReadFile(String), } #[allow(clippy::module_name_repetitions)] @@ -105,6 +108,12 @@ impl ReplCmdHandler { copy(&self.reply.borrow()).with_context(|| "Failed to copy the last output")?; 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(()) } diff --git a/src/repl/mod.rs b/src/repl/mod.rs index 311ca72..7c99757 100644 --- a/src/repl/mod.rs +++ b/src/repl/mod.rs @@ -17,7 +17,7 @@ use anyhow::{Context, Result}; use reedline::Signal; use std::rc::Rc; -pub const REPL_COMMANDS: [(&str, &str); 13] = [ +pub const REPL_COMMANDS: [(&str, &str); 14] = [ (".info", "Print system-wide information"), (".set", "Modify the configuration temporarily"), (".model", "Choose a model"), @@ -31,6 +31,7 @@ pub const REPL_COMMANDS: [(&str, &str); 13] = [ (".clear history", "Clear the history"), (".help", "Print this help message"), (".exit", "Exit the REPL"), + (".read", "Read the contents of a file into the prompt"), ]; impl Repl { @@ -118,6 +119,10 @@ impl Repl { Some(name) => handler.handle(ReplCmd::SetRole(name.to_string()))?, 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" => { handler.handle(ReplCmd::ViewInfo)?; } |
