diff options
| -rw-r--r-- | Cargo.lock | 304 | ||||
| -rw-r--r-- | Cargo.toml | 1 | ||||
| -rw-r--r-- | src/config.rs | 24 | ||||
| -rw-r--r-- | src/main.rs | 2 | ||||
| -rw-r--r-- | src/repl/handler.rs | 43 | ||||
| -rw-r--r-- | src/repl/mod.rs | 30 | ||||
| -rw-r--r-- | src/utils.rs | 8 |
7 files changed, 42 insertions, 370 deletions
@@ -18,7 +18,6 @@ dependencies = [ "bytes", "chrono", "clap", - "copypasta", "crossbeam", "crossterm 0.26.1", "dirs", @@ -96,12 +95,6 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "bef38d45163c2f1dde094a7dfd33ccf595c92905c8f8f4fdc18d06fb1037718a" [[package]] -name = "block" -version = "0.1.6" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "0d8c1fef690941d3e7788d328517591fecc684c084084702d6ff1641e993699a" - -[[package]] name = "bumpalo" version = "3.12.0" source = "registry+https://github.com/rust-lang/crates.io-index" @@ -178,16 +171,6 @@ dependencies = [ ] [[package]] -name = "clipboard-win" -version = "3.1.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "9fdf5e01086b6be750428ba4a40619f847eb2e95756eee84b18e06e5f0b50342" -dependencies = [ - "lazy-bytes-cast", - "winapi", -] - -[[package]] name = "codespan-reporting" version = "0.11.1" source = "registry+https://github.com/rust-lang/crates.io-index" @@ -198,20 +181,6 @@ dependencies = [ ] [[package]] -name = "copypasta" -version = "0.8.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "133fc8675ee3a4ec9aa513584deda9aa0faeda3586b87f7f0f2ba082c66fb172" -dependencies = [ - "clipboard-win", - "objc", - "objc-foundation", - "objc_id", - "smithay-clipboard", - "x11-clipboard", -] - -[[package]] name = "core-foundation-sys" version = "0.8.3" source = "registry+https://github.com/rust-lang/crates.io-index" @@ -270,7 +239,7 @@ dependencies = [ "autocfg", "cfg-if", "crossbeam-utils", - "memoffset 0.8.0", + "memoffset", "scopeguard", ] @@ -416,21 +385,6 @@ dependencies = [ ] [[package]] -name = "dlib" -version = "0.5.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ac1b7517328c04c2aa68422fc60a41b92208182142ed04a25879c26c8f878794" -dependencies = [ - "libloading", -] - -[[package]] -name = "downcast-rs" -version = "1.2.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "9ea835d29036a4087793836fa931b08837ad5e957da9e23886b29586fb9b6650" - -[[package]] name = "dyn-clone" version = "1.0.11" source = "registry+https://github.com/rust-lang/crates.io-index" @@ -581,16 +535,6 @@ dependencies = [ ] [[package]] -name = "gethostname" -version = "0.2.3" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "c1ebd34e35c46e00bb73e81363248d627782724609fe1b6396f553f68fe3862e" -dependencies = [ - "libc", - "winapi", -] - -[[package]] name = "getrandom" version = "0.2.8" source = "registry+https://github.com/rust-lang/crates.io-index" @@ -840,12 +784,6 @@ dependencies = [ ] [[package]] -name = "lazy-bytes-cast" -version = "5.0.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "10257499f089cd156ad82d0a9cd57d9501fa2c989068992a97eb3c27836f206b" - -[[package]] name = "lazy_static" version = "1.4.0" source = "registry+https://github.com/rust-lang/crates.io-index" @@ -858,16 +796,6 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "201de327520df007757c1f0adce6e827fe8562fbc28bfd9c15571c66ca1f5f79" [[package]] -name = "libloading" -version = "0.7.4" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "b67380fd3b2fbe7527a606e18729d21c6f3951633d0500574c4dc22d2d638b9f" -dependencies = [ - "cfg-if", - "winapi", -] - -[[package]] name = "link-cplusplus" version = "1.0.8" source = "registry+https://github.com/rust-lang/crates.io-index" @@ -902,39 +830,12 @@ dependencies = [ ] [[package]] -name = "malloc_buf" -version = "0.0.6" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "62bb907fe88d54d8d9ce32a3cceab4218ed2f6b7d35617cafe9adf84e43919cb" -dependencies = [ - "libc", -] - -[[package]] name = "memchr" version = "2.5.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "2dffe52ecf27772e601905b7522cb4ef790d2cc203488bbd0e2fe85fcb74566d" [[package]] -name = "memmap2" -version = "0.5.10" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "83faa42c0a078c393f6b29d5db232d8be22776a891f8f56e5284faee4a20b327" -dependencies = [ - "libc", -] - -[[package]] -name = "memoffset" -version = "0.6.5" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "5aa361d4faea93603064a027415f07bd8e1d5c88c9fbf68bf56a285428fd79ce" -dependencies = [ - "autocfg", -] - -[[package]] name = "memoffset" version = "0.8.0" source = "registry+https://github.com/rust-lang/crates.io-index" @@ -986,18 +887,6 @@ dependencies = [ ] [[package]] -name = "nix" -version = "0.24.3" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "fa52e972a9a719cecb6864fb88568781eb706bac2cd1d4f04a648542dbf78069" -dependencies = [ - "bitflags", - "cfg-if", - "libc", - "memoffset 0.6.5", -] - -[[package]] name = "nom" version = "7.1.3" source = "registry+https://github.com/rust-lang/crates.io-index" @@ -1047,35 +936,6 @@ dependencies = [ ] [[package]] -name = "objc" -version = "0.2.7" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "915b1b472bc21c53464d6c8461c9d3af805ba1ef837e1cac254428f4a77177b1" -dependencies = [ - "malloc_buf", -] - -[[package]] -name = "objc-foundation" -version = "0.1.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "1add1b659e36c9607c7aab864a76c7a4c2760cd0cd2e120f3fb8b952c7e22bf9" -dependencies = [ - "block", - "objc", - "objc_id", -] - -[[package]] -name = "objc_id" -version = "0.1.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "c92d4ddb4bd7b50d730c215ff871754d0da6b2178849f8a2a2ab69712d0c073b" -dependencies = [ - "objc", -] - -[[package]] name = "once_cell" version = "1.17.1" source = "registry+https://github.com/rust-lang/crates.io-index" @@ -1364,12 +1224,6 @@ dependencies = [ ] [[package]] -name = "scoped-tls" -version = "1.0.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "e1cf6437eb19a8f4a6cc0f7dca544973b0b78843adbfeb3683d1a94a0024a294" - -[[package]] name = "scopeguard" version = "1.1.0" source = "registry+https://github.com/rust-lang/crates.io-index" @@ -1493,34 +1347,6 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "a507befe795404456341dfab10cef66ead4c041f62b8b11bbb92bffe5d0953e0" [[package]] -name = "smithay-client-toolkit" -version = "0.16.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f307c47d32d2715eb2e0ece5589057820e0e5e70d07c247d1063e844e107f454" -dependencies = [ - "bitflags", - "dlib", - "lazy_static", - "log", - "memmap2", - "nix", - "pkg-config", - "wayland-client", - "wayland-cursor", - "wayland-protocols", -] - -[[package]] -name = "smithay-clipboard" -version = "0.6.6" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "0a345c870a1fae0b1b779085e81b51e614767c239e93503588e54c5b17f4b0e8" -dependencies = [ - "smithay-client-toolkit", - "wayland-client", -] - -[[package]] name = "socket2" version = "0.4.9" source = "registry+https://github.com/rust-lang/crates.io-index" @@ -1959,79 +1785,6 @@ dependencies = [ ] [[package]] -name = "wayland-client" -version = "0.29.5" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "3f3b068c05a039c9f755f881dc50f01732214f5685e379829759088967c46715" -dependencies = [ - "bitflags", - "downcast-rs", - "libc", - "nix", - "scoped-tls", - "wayland-commons", - "wayland-scanner", - "wayland-sys", -] - -[[package]] -name = "wayland-commons" -version = "0.29.5" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "8691f134d584a33a6606d9d717b95c4fa20065605f798a3f350d78dced02a902" -dependencies = [ - "nix", - "once_cell", - "smallvec", - "wayland-sys", -] - -[[package]] -name = "wayland-cursor" -version = "0.29.5" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "6865c6b66f13d6257bef1cd40cbfe8ef2f150fb8ebbdb1e8e873455931377661" -dependencies = [ - "nix", - "wayland-client", - "xcursor", -] - -[[package]] -name = "wayland-protocols" -version = "0.29.5" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "b950621f9354b322ee817a23474e479b34be96c2e909c14f7bc0100e9a970bc6" -dependencies = [ - "bitflags", - "wayland-client", - "wayland-commons", - "wayland-scanner", -] - -[[package]] -name = "wayland-scanner" -version = "0.29.5" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "8f4303d8fa22ab852f789e75a967f0a2cdc430a607751c0499bada3e451cbd53" -dependencies = [ - "proc-macro2", - "quote", - "xml-rs", -] - -[[package]] -name = "wayland-sys" -version = "0.29.5" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "be12ce1a3c39ec7dba25594b97b42cb3195d54953ddb9d3d95a7c3902bc6e9d4" -dependencies = [ - "dlib", - "lazy_static", - "pkg-config", -] - -[[package]] name = "web-sys" version = "0.3.61" source = "registry+https://github.com/rust-lang/crates.io-index" @@ -2086,15 +1839,6 @@ dependencies = [ ] [[package]] -name = "winapi-wsapoll" -version = "0.1.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "44c17110f57155602a80dca10be03852116403c9ff3cd25b079d666f2aa3df6e" -dependencies = [ - "winapi", -] - -[[package]] name = "winapi-x86_64-pc-windows-gnu" version = "0.4.0" source = "registry+https://github.com/rust-lang/crates.io-index" @@ -2174,49 +1918,3 @@ checksum = "80d0f4e272c85def139476380b12f9ac60926689dd2e01d4923222f40580869d" dependencies = [ "winapi", ] - -[[package]] -name = "x11-clipboard" -version = "0.7.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "0827f86aa910c4e73329a4f619deabe88ebb4b042370bf023c2d5d8b4eb54695" -dependencies = [ - "x11rb", -] - -[[package]] -name = "x11rb" -version = "0.10.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "592b4883219f345e712b3209c62654ebda0bb50887f330cbd018d0f654bfd507" -dependencies = [ - "gethostname", - "nix", - "winapi", - "winapi-wsapoll", - "x11rb-protocol", -] - -[[package]] -name = "x11rb-protocol" -version = "0.10.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "56b245751c0ac9db0e006dc812031482784e434630205a93c73cfefcaabeac67" -dependencies = [ - "nix", -] - -[[package]] -name = "xcursor" -version = "0.3.4" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "463705a63313cd4301184381c5e8042f0a7e9b4bb63653f216311d4ae74690b7" -dependencies = [ - "nom", -] - -[[package]] -name = "xml-rs" -version = "0.8.4" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d2d7d3948613f75c98fd9328cfdcc45acc4d360655289d0a7d4ec931392200a3" @@ -27,7 +27,6 @@ serde_yaml = "0.9.17" tokio = { version = "1.26.0", features = ["full"] } crossbeam = "0.8.2" crossterm = "0.26.1" -copypasta = "0.8.2" chrono = "0.4.23" atty = "0.2.14" unicode-width = "0.1.10" 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) |
