Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

major: refactor codebase without a breaking change #17

Merged
merged 13 commits into from
Dec 11, 2024
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,3 @@ nu-json = "0.100.0"
[features]
default = []
use-wayland = ["arboard/wayland-data-control"]
enforce-daemon = []
3 changes: 1 addition & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,7 @@ nupm install --path nu_plugin_clipboard -f

* supported features:
* **use-wayland**: will prioritize wayland api but will falls back to X11 protocol on error
* **enforce-daemon**: force copy command to spawn a daemon and revert the functionality of `--daemon` flag

* **enforce-daemon**: Deprecation notice: this method is now always enabled in linux environments
* or compile manually

```bash
Expand Down
6 changes: 1 addition & 5 deletions build.nu
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
use std log

let messages = {
"enforce-daemon" : $"Found (ansi blue)($env.XDG_CURRENT_DESKTOP?)(ansi reset) from env\(`(ansi blue)XDG_CURRENT_DESKTOP(ansi reset)`\) activating `(ansi red)enforce-daemon(ansi reset)` mode,
this will cause the copy action to (ansi yellow)use daemon mode(ansi reset) without the (ansi green)`--daemon` \(`-d`\)(ansi reset) flag
and cause `--daemon` flag to have inverted functionality (ansi red)\(now using -d will disable daemon mode\)(ansi reset)",
"use-wayland" : $"Found (ansi blue)wayland(ansi reset) in env\(`(ansi blue)XDG_SESSION_TYPE(ansi reset)`\): activating `(ansi green)use-wayland(ansi reset)` feature"
"use-wayland" : $"Found (ansi blue)wayland(ansi reset) in env\(`(ansi blue)XDG_SESSION_TYPE(ansi reset)`\): activating `(ansi green)use-wayland(ansi reset)` feature"
}

def main [package_file: path = nupm.nuon] {
Expand All @@ -13,7 +10,6 @@ def main [package_file: path = nupm.nuon] {

let name = open ($repo_root | path join "Cargo.toml") | get package.name
let features = []
| if ($nu.os-info.name == "linux") { $in | append enforce-daemon } else { $in }
| if ($nu.os-info.name == "linux" and ($env.XDG_SESSION_TYPE? == "wayland")) {$in | append use-wayland } else { $in }

for feature in $features {
Expand Down
12 changes: 12 additions & 0 deletions src/clipboard/arboard_provider.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
use super::error_mapper::map_arboard_err_to_label;
use nu_protocol::LabeledError;
pub(crate) fn with_clipboard_instance<
U,
F: FnOnce(&mut arboard::Clipboard) -> Result<U, arboard::Error>,
>(
op: F,
) -> Result<U, LabeledError> {
let mut clipboard = arboard::Clipboard::new().map_err(map_arboard_err_to_label)?;

op(&mut clipboard).map_err(map_arboard_err_to_label)
}
34 changes: 34 additions & 0 deletions src/clipboard/clipboard.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
use nu_protocol::LabeledError;

use super::arboard_provider::with_clipboard_instance;

pub enum CheckResult {
Continue,
Exit(String, i32),
}
pub fn create_clipboard() -> impl Clipboard {
#[cfg(target_os = "linux")]
{
crate::clipboard::linux::ClipBoardLinux::new()
}
#[cfg(target_os = "macos")]
{
crate::clipboard::mac_os::ClipboardMacOs::new()
}
#[cfg(target_os = "windows")]
{
crate::clipboard::windows::ClipboardWindows::new()
}
}

pub trait Clipboard {
fn pre_execute_check(&self) -> CheckResult {
CheckResult::Continue
}
fn copy_text(&self, text: &str) -> Result<(), LabeledError> {
with_clipboard_instance(|clip| clip.set_text(text))
}
fn get_text(&self) -> Result<String, LabeledError> {
with_clipboard_instance(|clip| clip.get_text())
}
}
212 changes: 0 additions & 212 deletions src/clipboard/copy.rs

This file was deleted.

3 changes: 3 additions & 0 deletions src/clipboard/error_mapper.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
pub(crate) fn map_arboard_err_to_label(err: arboard::Error) -> nu_protocol::LabeledError {
nu_protocol::LabeledError::new(format!("Clipboard Error: {}", err.to_string()))
}
68 changes: 68 additions & 0 deletions src/clipboard/linux.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
use std::{
env,
io::{stderr, stdout},
process::{Command, Stdio},
};

use super::{
arboard_provider::with_clipboard_instance,
clipboard::{CheckResult, Clipboard},
};

use arboard::SetExtLinux;
use nu_protocol::LabeledError;

const DAEMONIZE_ARG: &str = "daemon-copy";
pub(crate) struct ClipBoardLinux {}

#[cfg(target_os = "linux")]
impl ClipBoardLinux {
pub fn new() -> Self {
Self {}
}
fn is_daemon_request() -> bool {
env::args().nth(1).as_deref() == Some(DAEMONIZE_ARG)
}
fn request_daemon(&self, text: &str) -> Result<(), nu_protocol::LabeledError> {
match env::current_exe().map(|exe| {
Command::new(exe)
.arg(DAEMONIZE_ARG)
.arg(text)
.stdin(Stdio::null())
.stdout(stdout())
.stderr(stderr())
.current_dir(env::temp_dir())
.spawn()
}) {
Ok(Ok(_)) => Ok(()),
Err(err) | Ok(Err(err)) => Err(nu_protocol::LabeledError::new(format!(
"Failed to spawn daemon process: {}",
err.to_string()
))),
}
}
fn copy_with_daemon() -> Result<(), nu_protocol::LabeledError> {
with_clipboard_instance(|clip: &mut arboard::Clipboard| {
clip.clear()?;
let args: Vec<String> = env::args().skip(2).collect();
let data = args.join(" ");
clip.set().wait().text(data)
FMotalleb marked this conversation as resolved.
Show resolved Hide resolved
})
}
}

impl Clipboard for ClipBoardLinux {
fn pre_execute_check(&self) -> CheckResult {
match Self::is_daemon_request() {
true => match Self::copy_with_daemon() {
Err(e) => CheckResult::Exit(e.msg, 1),
_ => CheckResult::Exit("".to_string(), 0),
},
false => CheckResult::Continue,
}
}

fn copy_text(&self, text: &str) -> Result<(), LabeledError> {
self.request_daemon(text)
}
}
13 changes: 13 additions & 0 deletions src/clipboard/mac_os.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
use nu_protocol::LabeledError;

use super::{arboard_provider::with_clipboard_instance, clipboard::Clipboard};

pub(crate) struct ClipBoardMacos;

impl ClipBoardMacos {
pub fn new() -> Self {
Self {}
}
}

impl Clipboard for ClipBoardMacos {}
13 changes: 11 additions & 2 deletions src/clipboard/mod.rs
Original file line number Diff line number Diff line change
@@ -1,2 +1,11 @@
pub mod copy;
pub mod paste;
mod arboard_provider;
pub mod clipboard;
mod error_mapper;

#[cfg(target_os = "linux")]
pub(crate) mod linux;

#[cfg(target_os = "macos")]
pub(crate) mod mac_os;
#[cfg(target_os = "windows")]
pub(crate) mod windows;
Loading