From 00ce1575715329a21726a506b13c0c2aef7f3f82 Mon Sep 17 00:00:00 2001 From: sigoden Date: Thu, 4 May 2023 22:42:50 +0800 Subject: feat: add `.copy` command (#119) --- src/repl/handler.rs | 10 +++++++++- src/repl/mod.rs | 6 +++++- 2 files changed, 14 insertions(+), 2 deletions(-) (limited to 'src/repl') diff --git a/src/repl/handler.rs b/src/repl/handler.rs index c38f607..495413c 100644 --- a/src/repl/handler.rs +++ b/src/repl/handler.rs @@ -2,6 +2,7 @@ use crate::client::ChatGptClient; use crate::config::SharedConfig; use crate::print_now; use crate::render::render_stream; +use crate::utils::copy; use super::abort::SharedAbortSignal; @@ -20,6 +21,7 @@ pub enum ReplCmd { ViewInfo, StartConversation, EndConversatoin, + Copy, } pub struct ReplCmdHandler { @@ -64,7 +66,9 @@ impl ReplCmdHandler { wg.wait(); let buffer = ret?; self.config.read().save_message(&input, &buffer)?; - let _ = self.config.read().copy_message(&buffer); + if self.config.read().auto_copy { + let _ = copy(&buffer); + } self.config.write().save_conversation(&input, &buffer)?; *self.reply.borrow_mut() = buffer; } @@ -100,6 +104,10 @@ impl ReplCmdHandler { self.config.write().end_conversation(); print_now!("\n"); } + ReplCmd::Copy => { + copy(&self.reply.borrow()).with_context(|| "Failed to copy the last output")?; + print_now!("\n"); + } } Ok(()) } diff --git a/src/repl/mod.rs b/src/repl/mod.rs index 6bedfa3..8f3d184 100644 --- a/src/repl/mod.rs +++ b/src/repl/mod.rs @@ -19,7 +19,7 @@ use reedline::Signal; use std::borrow::Cow; use std::sync::Arc; -pub const REPL_COMMANDS: [(&str, &str); 12] = [ +pub const REPL_COMMANDS: [(&str, &str); 13] = [ (".info", "Print system-wide information"), (".set", "Modify the configuration temporarily"), (".model", "Choose a model"), @@ -28,6 +28,7 @@ pub const REPL_COMMANDS: [(&str, &str); 12] = [ (".clear role", "Clear the currently selected role"), (".conversation", "Start a conversation."), (".clear conversation", "End current conversation."), + (".copy", "Copy the last output to the clipboard"), (".history", "Print the history"), (".clear history", "Clear the history"), (".help", "Print this help message"), @@ -138,6 +139,9 @@ impl Repl { ".conversation" => { handler.handle(ReplCmd::StartConversation)?; } + ".copy" => { + handler.handle(ReplCmd::Copy)?; + } _ => dump_unknown_command(), }, None => { -- cgit v1.2.3