summaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/config/mod.rs17
-rw-r--r--src/repl/handler.rs10
-rw-r--r--src/repl/mod.rs6
-rw-r--r--src/utils/mod.rs6
4 files changed, 21 insertions, 18 deletions
diff --git a/src/config/mod.rs b/src/config/mod.rs
index 021d2c1..900f129 100644
--- a/src/config/mod.rs
+++ b/src/config/mod.rs
@@ -10,7 +10,6 @@ use crate::config::message::num_tokens_from_messages;
use crate::utils::{mask_text, now};
use anyhow::{anyhow, bail, Context, Result};
-use copypasta::{ClipboardContext, ClipboardProvider};
use inquire::{Confirm, Text};
use parking_lot::RwLock;
use serde::Deserialize;
@@ -71,7 +70,7 @@ pub struct Config {
pub light_theme: bool,
/// Set a timeout in seconds for connect to gpt
pub connect_timeout: usize,
- /// Automatically copy the last message output to the clipboard
+ /// Automatically copy the last output to the clipboard
pub auto_copy: bool,
/// Predefined roles
#[serde(skip)]
@@ -173,20 +172,6 @@ impl Config {
Ok(path)
}
- pub fn copy_message(&self, output: &str) -> Result<()> {
- if self.auto_copy {
- match ClipboardContext::new() {
- Ok(mut ctx) => {
- ctx.set_contents(output.to_string()).map_err(|err| {
- anyhow!("Error: Unable to the last message output to clipboard, {err}")
- })?;
- }
- Err(_) => bail!("Error: Unable to access clipboard context."),
- }
- }
- Ok(())
- }
-
pub fn save_message(&self, input: &str, output: &str) -> Result<()> {
if !self.save {
return Ok(());
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 => {
diff --git a/src/utils/mod.rs b/src/utils/mod.rs
index 64fc01c..ec57fed 100644
--- a/src/utils/mod.rs
+++ b/src/utils/mod.rs
@@ -2,6 +2,7 @@ mod tiktoken;
pub use self::tiktoken::{cl100k_base_singleton, count_tokens, text_to_tokens, tokens_to_text};
+use arboard::Clipboard;
use chrono::prelude::*;
use crossterm::style::{Color, Stylize};
use std::io::{stdout, Write};
@@ -35,6 +36,11 @@ pub fn mask_text(text: &str, head: usize, tail: usize) -> String {
format!("{}...{}", &text[0..head], &text[text.len() - tail..])
}
+pub fn copy(src: &str) -> Result<(), arboard::Error> {
+ let mut clipboard = Clipboard::new()?;
+ clipboard.set_text(src)
+}
+
#[cfg(test)]
mod tests {
use super::*;