Skip to content

Commit

Permalink
feat: use a global clipboard
Browse files Browse the repository at this point in the history
  • Loading branch information
sxyazi committed Dec 19, 2023
1 parent d6ca524 commit df42ca7
Show file tree
Hide file tree
Showing 9 changed files with 35 additions and 28 deletions.
3 changes: 2 additions & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions yazi-core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ yazi-shared = { path = "../yazi-shared", version = "0.1.5" }

# External dependencies
anyhow = "^1"
base64 = "^0"
bitflags = "^2"
crossterm = "^0"
futures = "^0"
Expand All @@ -34,3 +35,6 @@ yazi-prebuild = "^0"

# Logging
tracing = { version = "^0", features = [ "max_level_debug", "release_max_level_warn" ] }

[target."cfg(windows)".dependencies]
clipboard-win = "^4"
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
use std::ffi::OsString;

use parking_lot::Mutex;
use yazi_shared::RoCell;

pub static CLIPBOARD: RoCell<Clipboard> = RoCell::new();

#[derive(Default)]
pub struct Clipboard {
content: OsString,
content: Mutex<OsString>,
}

impl Clipboard {
Expand All @@ -14,7 +19,7 @@ impl Clipboard {
use yazi_shared::in_ssh_connection;

if in_ssh_connection() {
return self.content.clone();
return self.content.lock().clone();
}

let all = [
Expand All @@ -32,7 +37,7 @@ impl Clipboard {
return OsString::from_vec(output.stdout);
}
}
self.content.clone()
self.content.lock().clone()
}

#[cfg(windows)]
Expand All @@ -44,20 +49,20 @@ impl Clipboard {
return s.into();
}

self.content.clone()
self.content.lock().clone()
}

#[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 crossterm::execute;
use tokio::{io::AsyncWriteExt, process::Command};
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() {
execute!(stdout(), osc52::SetClipboard::new(&self.content)).ok();
execute!(stdout(), osc52::SetClipboard::new(s.as_ref())).ok();
}

let all = [
Expand Down Expand Up @@ -93,12 +98,12 @@ impl Clipboard {
}

#[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};

self.content = 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()))
.await
.ok();
Expand All @@ -110,7 +115,6 @@ mod osc52 {
use std::ffi::OsStr;

use base64::{engine::general_purpose, Engine};
use crossterm;

#[derive(Debug)]
pub struct SetClipboard {
Expand Down
4 changes: 2 additions & 2 deletions yazi-core/src/input/commands/paste.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use yazi_shared::event::Exec;

use crate::input::{op::InputOp, Input};
use crate::{input::{op::InputOp, Input}, CLIPBOARD};

pub struct Opt {
before: bool,
Expand All @@ -17,7 +17,7 @@ impl Input {
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() {
return false;
}
Expand Down
8 changes: 3 additions & 5 deletions yazi-core/src/input/input.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@ use std::ops::Range;
use tokio::sync::mpsc::UnboundedSender;
use unicode_width::UnicodeWidthStr;
use yazi_config::{popup::Position, INPUT};
use yazi_scheduler::external::Clipboard;
use yazi_shared::InputError;

use super::{mode::InputMode, op::InputOp, InputSnap, InputSnaps};
use crate::CLIPBOARD;

#[derive(Default)]
pub struct Input {
Expand All @@ -24,8 +24,6 @@ pub struct Input {

// Shell
pub(super) highlight: bool,

pub(super) clipboard: Clipboard,
}

impl Input {
Expand Down Expand Up @@ -61,7 +59,7 @@ impl Input {

let drain = snap.value.drain(start.unwrap()..end.unwrap()).collect::<String>();
if cut {
futures::executor::block_on(self.clipboard.set(&drain));
futures::executor::block_on(CLIPBOARD.set(&drain));
}

snap.op = InputOp::None;
Expand All @@ -74,7 +72,7 @@ impl Input {
let yanked = &snap.value[start.unwrap()..end.unwrap()];

snap.op = InputOp::None;
futures::executor::block_on(self.clipboard.set(yanked));
futures::executor::block_on(CLIPBOARD.set(yanked));
}
};

Expand Down
8 changes: 7 additions & 1 deletion yazi-core/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
clippy::unit_arg
)]

mod clipboard;
pub mod completion;
mod context;
pub mod files;
Expand All @@ -20,8 +21,13 @@ pub mod tab;
pub mod tasks;
pub mod which;

pub use clipboard::*;
pub use context::*;
pub use highlighter::*;
pub use step::*;

pub fn init() { yazi_scheduler::init(); }
pub fn init() {
CLIPBOARD.with(Default::default);

yazi_scheduler::init();
}
5 changes: 2 additions & 3 deletions yazi-core/src/tab/commands/copy.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
use std::ffi::{OsStr, OsString};

use yazi_scheduler::external::Clipboard;
use yazi_shared::event::Exec;

use crate::tab::Tab;
use crate::{tab::Tab, CLIPBOARD};

pub struct Opt<'a> {
type_: &'a str,
Expand Down Expand Up @@ -32,7 +31,7 @@ impl Tab {
}
}

futures::executor::block_on(Clipboard::default().set(s));
futures::executor::block_on(CLIPBOARD.set(s));
false
}
}
3 changes: 0 additions & 3 deletions yazi-scheduler/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,3 @@ trash = "^3"

# Logging
tracing = { version = "^0", features = [ "max_level_debug", "release_max_level_warn" ] }

[target."cfg(windows)".dependencies]
clipboard-win = "^4"
2 changes: 0 additions & 2 deletions yazi-scheduler/src/external/mod.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
mod clipboard;
mod fd;
mod ffmpegthumbnailer;
mod file;
Expand All @@ -11,7 +10,6 @@ mod shell;
mod unar;
mod zoxide;

pub use clipboard::*;
pub use fd::*;
pub use ffmpegthumbnailer::*;
pub use file::*;
Expand Down

0 comments on commit df42ca7

Please sign in to comment.