summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--src/repl/handler.rs22
-rw-r--r--src/utils/mod.rs6
2 files changed, 18 insertions, 10 deletions
diff --git a/src/repl/handler.rs b/src/repl/handler.rs
index a061742..1952ebe 100644
--- a/src/repl/handler.rs
+++ b/src/repl/handler.rs
@@ -2,13 +2,13 @@ use crate::client::init_client;
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;
-use anyhow::{Context, Result};
+use anyhow::{bail, Context, Result};
+use arboard::Clipboard;
use crossbeam::channel::Sender;
use crossbeam::sync::WaitGroup;
use std::cell::RefCell;
@@ -32,16 +32,19 @@ pub struct ReplCmdHandler {
config: SharedConfig,
reply: RefCell<String>,
abort: SharedAbortSignal,
+ clipboard: std::result::Result<RefCell<Clipboard>, arboard::Error>,
}
impl ReplCmdHandler {
#[allow(clippy::unnecessary_wraps)]
pub fn init(config: SharedConfig, abort: SharedAbortSignal) -> Result<Self> {
let reply = RefCell::new(String::new());
+ let clipboard = Clipboard::new().map(RefCell::new);
Ok(Self {
config,
reply,
abort,
+ clipboard,
})
}
@@ -67,7 +70,7 @@ impl ReplCmdHandler {
let buffer = ret?;
self.config.read().save_message(&input, &buffer)?;
if self.config.read().auto_copy {
- let _ = copy(&buffer);
+ let _ = self.copy(&buffer);
}
self.config.write().save_conversation(&input, &buffer)?;
*self.reply.borrow_mut() = buffer;
@@ -105,7 +108,8 @@ impl ReplCmdHandler {
print_now!("\n");
}
ReplCmd::Copy => {
- copy(&self.reply.borrow()).with_context(|| "Failed to copy the last output")?;
+ self.copy(&self.reply.borrow())
+ .with_context(|| "Failed to copy the last output")?;
print_now!("\n");
}
ReplCmd::ReadFile(file) => {
@@ -118,6 +122,16 @@ impl ReplCmdHandler {
}
Ok(())
}
+
+ fn copy(&self, text: &str) -> Result<()> {
+ match self.clipboard.as_ref() {
+ Err(err) => bail!("{}", err),
+ Ok(clip) => {
+ clip.borrow_mut().set_text(text)?;
+ Ok(())
+ }
+ }
+ }
}
#[allow(clippy::module_name_repetitions)]
diff --git a/src/utils/mod.rs b/src/utils/mod.rs
index 3ae1004..4ed0d5a 100644
--- a/src/utils/mod.rs
+++ b/src/utils/mod.rs
@@ -3,7 +3,6 @@ mod tiktoken;
use self::tiktoken::cl100k_base;
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};
@@ -38,11 +37,6 @@ pub fn emphasis(text: &str) -> String {
text.stylize().with(Color::White).to_string()
}
-pub fn copy(src: &str) -> Result<(), arboard::Error> {
- let mut clipboard = Clipboard::new()?;
- clipboard.set_text(src)
-}
-
pub fn split_text(text: &str) -> Result<Vec<String>, anyhow::Error> {
let bpe = cl100k_base()?;
let tokens = bpe.encode_with_special_tokens(text);