feat: use a global clipboard

This commit is contained in:
sxyazi 2023-12-19 22:43:03 +08:00
parent d6ca524c53
commit df42ca799e
No known key found for this signature in database
9 changed files with 35 additions and 28 deletions

3
Cargo.lock generated
View file

@ -2611,7 +2611,9 @@ name = "yazi-core"
version = "0.1.5" version = "0.1.5"
dependencies = [ dependencies = [
"anyhow", "anyhow",
"base64",
"bitflags 2.4.1", "bitflags 2.4.1",
"clipboard-win",
"crossterm", "crossterm",
"futures", "futures",
"indexmap", "indexmap",
@ -2686,7 +2688,6 @@ dependencies = [
"anyhow", "anyhow",
"async-channel", "async-channel",
"base64", "base64",
"clipboard-win",
"crossterm", "crossterm",
"futures", "futures",
"libc", "libc",

View file

@ -16,6 +16,7 @@ yazi-shared = { path = "../yazi-shared", version = "0.1.5" }
# External dependencies # External dependencies
anyhow = "^1" anyhow = "^1"
base64 = "^0"
bitflags = "^2" bitflags = "^2"
crossterm = "^0" crossterm = "^0"
futures = "^0" futures = "^0"
@ -34,3 +35,6 @@ yazi-prebuild = "^0"
# Logging # Logging
tracing = { version = "^0", features = [ "max_level_debug", "release_max_level_warn" ] } tracing = { version = "^0", features = [ "max_level_debug", "release_max_level_warn" ] }
[target."cfg(windows)".dependencies]
clipboard-win = "^4"

View file

@ -1,8 +1,13 @@
use std::ffi::OsString; use std::ffi::OsString;
use parking_lot::Mutex;
use yazi_shared::RoCell;
pub static CLIPBOARD: RoCell<Clipboard> = RoCell::new();
#[derive(Default)] #[derive(Default)]
pub struct Clipboard { pub struct Clipboard {
content: OsString, content: Mutex<OsString>,
} }
impl Clipboard { impl Clipboard {
@ -14,7 +19,7 @@ impl Clipboard {
use yazi_shared::in_ssh_connection; use yazi_shared::in_ssh_connection;
if in_ssh_connection() { if in_ssh_connection() {
return self.content.clone(); return self.content.lock().clone();
} }
let all = [ let all = [
@ -32,7 +37,7 @@ impl Clipboard {
return OsString::from_vec(output.stdout); return OsString::from_vec(output.stdout);
} }
} }
self.content.clone() self.content.lock().clone()
} }
#[cfg(windows)] #[cfg(windows)]
@ -44,20 +49,20 @@ impl Clipboard {
return s.into(); return s.into();
} }
self.content.clone() self.content.lock().clone()
} }
#[cfg(unix)] #[cfg(unix)]
pub async fn set(&mut self, s: impl AsRef<std::ffi::OsStr>) { pub async fn set(&self, s: impl AsRef<std::ffi::OsStr>) {
use std::{io::stdout, process::Stdio}; use std::{io::stdout, process::Stdio};
use crossterm::execute; use crossterm::execute;
use tokio::{io::AsyncWriteExt, process::Command}; use tokio::{io::AsyncWriteExt, process::Command};
use yazi_shared::in_ssh_connection; use yazi_shared::in_ssh_connection;
self.content = s.as_ref().to_owned(); *self.content.lock() = s.as_ref().to_owned();
if in_ssh_connection() { if in_ssh_connection() {
execute!(stdout(), osc52::SetClipboard::new(&self.content)).ok(); execute!(stdout(), osc52::SetClipboard::new(s.as_ref())).ok();
} }
let all = [ let all = [
@ -93,12 +98,12 @@ impl Clipboard {
} }
#[cfg(windows)] #[cfg(windows)]
pub async fn set(&mut self, s: impl AsRef<std::ffi::OsStr>) { pub async fn set(&self, s: impl AsRef<std::ffi::OsStr>) {
use clipboard_win::{formats, set_clipboard}; use clipboard_win::{formats, set_clipboard};
self.content = s.as_ref().to_owned();
let s = s.as_ref().to_owned(); let s = s.as_ref().to_owned();
*self.content.lock() = s.clone();
tokio::task::spawn_blocking(move || set_clipboard(formats::Unicode, s.to_string_lossy())) tokio::task::spawn_blocking(move || set_clipboard(formats::Unicode, s.to_string_lossy()))
.await .await
.ok(); .ok();
@ -110,7 +115,6 @@ mod osc52 {
use std::ffi::OsStr; use std::ffi::OsStr;
use base64::{engine::general_purpose, Engine}; use base64::{engine::general_purpose, Engine};
use crossterm;
#[derive(Debug)] #[derive(Debug)]
pub struct SetClipboard { pub struct SetClipboard {

View file

@ -1,6 +1,6 @@
use yazi_shared::event::Exec; use yazi_shared::event::Exec;
use crate::input::{op::InputOp, Input}; use crate::{input::{op::InputOp, Input}, CLIPBOARD};
pub struct Opt { pub struct Opt {
before: bool, before: bool,
@ -17,7 +17,7 @@ impl Input {
self.handle_op(self.snap().cursor, true); self.handle_op(self.snap().cursor, true);
} }
let s = futures::executor::block_on(self.clipboard.get()); let s = futures::executor::block_on(CLIPBOARD.get());
if s.is_empty() { if s.is_empty() {
return false; return false;
} }

View file

@ -3,10 +3,10 @@ use std::ops::Range;
use tokio::sync::mpsc::UnboundedSender; use tokio::sync::mpsc::UnboundedSender;
use unicode_width::UnicodeWidthStr; use unicode_width::UnicodeWidthStr;
use yazi_config::{popup::Position, INPUT}; use yazi_config::{popup::Position, INPUT};
use yazi_scheduler::external::Clipboard;
use yazi_shared::InputError; use yazi_shared::InputError;
use super::{mode::InputMode, op::InputOp, InputSnap, InputSnaps}; use super::{mode::InputMode, op::InputOp, InputSnap, InputSnaps};
use crate::CLIPBOARD;
#[derive(Default)] #[derive(Default)]
pub struct Input { pub struct Input {
@ -24,8 +24,6 @@ pub struct Input {
// Shell // Shell
pub(super) highlight: bool, pub(super) highlight: bool,
pub(super) clipboard: Clipboard,
} }
impl Input { impl Input {
@ -61,7 +59,7 @@ impl Input {
let drain = snap.value.drain(start.unwrap()..end.unwrap()).collect::<String>(); let drain = snap.value.drain(start.unwrap()..end.unwrap()).collect::<String>();
if cut { if cut {
futures::executor::block_on(self.clipboard.set(&drain)); futures::executor::block_on(CLIPBOARD.set(&drain));
} }
snap.op = InputOp::None; snap.op = InputOp::None;
@ -74,7 +72,7 @@ impl Input {
let yanked = &snap.value[start.unwrap()..end.unwrap()]; let yanked = &snap.value[start.unwrap()..end.unwrap()];
snap.op = InputOp::None; snap.op = InputOp::None;
futures::executor::block_on(self.clipboard.set(yanked)); futures::executor::block_on(CLIPBOARD.set(yanked));
} }
}; };

View file

@ -6,6 +6,7 @@
clippy::unit_arg clippy::unit_arg
)] )]
mod clipboard;
pub mod completion; pub mod completion;
mod context; mod context;
pub mod files; pub mod files;
@ -20,8 +21,13 @@ pub mod tab;
pub mod tasks; pub mod tasks;
pub mod which; pub mod which;
pub use clipboard::*;
pub use context::*; pub use context::*;
pub use highlighter::*; pub use highlighter::*;
pub use step::*; pub use step::*;
pub fn init() { yazi_scheduler::init(); } pub fn init() {
CLIPBOARD.with(Default::default);
yazi_scheduler::init();
}

View file

@ -1,9 +1,8 @@
use std::ffi::{OsStr, OsString}; use std::ffi::{OsStr, OsString};
use yazi_scheduler::external::Clipboard;
use yazi_shared::event::Exec; use yazi_shared::event::Exec;
use crate::tab::Tab; use crate::{tab::Tab, CLIPBOARD};
pub struct Opt<'a> { pub struct Opt<'a> {
type_: &'a str, type_: &'a str,
@ -32,7 +31,7 @@ impl Tab {
} }
} }
futures::executor::block_on(Clipboard::default().set(s)); futures::executor::block_on(CLIPBOARD.set(s));
false false
} }
} }

View file

@ -29,6 +29,3 @@ trash = "^3"
# Logging # Logging
tracing = { version = "^0", features = [ "max_level_debug", "release_max_level_warn" ] } tracing = { version = "^0", features = [ "max_level_debug", "release_max_level_warn" ] }
[target."cfg(windows)".dependencies]
clipboard-win = "^4"

View file

@ -1,4 +1,3 @@
mod clipboard;
mod fd; mod fd;
mod ffmpegthumbnailer; mod ffmpegthumbnailer;
mod file; mod file;
@ -11,7 +10,6 @@ mod shell;
mod unar; mod unar;
mod zoxide; mod zoxide;
pub use clipboard::*;
pub use fd::*; pub use fd::*;
pub use ffmpegthumbnailer::*; pub use ffmpegthumbnailer::*;
pub use file::*; pub use file::*;