Skip to content

Commit

Permalink
Merge pull request #17 from FMotalleb/refactor
Browse files Browse the repository at this point in the history
major: refactor codebase without a breaking change
  • Loading branch information
FMotalleb authored Dec 11, 2024
2 parents 00cc3d3 + 5fa5dcd commit 5c95fe0
Show file tree
Hide file tree
Showing 19 changed files with 428 additions and 392 deletions.
183 changes: 93 additions & 90 deletions Cargo.lock

Large diffs are not rendered by default.

3 changes: 1 addition & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ keywords = ["nushell", "clipboard", "plugin"]
homepage = "https://github.com/FMotalleb/nu_plugin_clipboard"
repository = "https://github.com/FMotalleb/nu_plugin_clipboard"
description = "A nushell plugin to copy text into clipboard or get text from it."
version = "0.100.0"
version = "0.100.1"
edition = "2021"
readme = "README.md"

Expand All @@ -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, to disable this behavior set `$env.config.plugins.clipboard.NO_DAEMON` to `true`, to make it permanent add `$env.config.plugins.clipboard.NO_DAEMON = true` to `config env`
* 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
2 changes: 1 addition & 1 deletion nupm.nuon
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "nu_plugin_clipboard",
"version": "0.100.0",
"version": "0.100.1",
"description": "A nushell plugin to copy text into clipboard or get text from it.",
"license": "LICENSE",
"type": "custom"
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)
}
53 changes: 53 additions & 0 deletions src/clipboard/clipboard.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
use std::io::Error;

use nu_protocol::LabeledError;

use super::arboard_provider::with_clipboard_instance;

pub enum CheckResult {
Continue,
Exit(String, i32),
}

fn no_daemon(config: Option<nu_protocol::Value>) -> Result<bool, Error> {
match config {
None => Ok(false),
Some(nu_protocol::Value::Record { val, .. }) => {
return no_daemon(val.get("NO_DAEMON").cloned());
}
Some(nu_protocol::Value::Bool { val, .. }) => Ok(val),
Some(nu_protocol::Value::String { val, .. }) => match val.as_str() {
"true" | "True" | "1" => Ok(true),
_ => Ok(false),
},
Some(nu_protocol::Value::Int { val, .. }) => Ok(val == 1),
_ => Ok(true),
}
}

pub fn create_clipboard(config: Option<nu_protocol::Value>) -> impl Clipboard {
#[cfg(target_os = "linux")]
{
crate::clipboard::linux::ClipBoardLinux::new(!no_daemon(config).unwrap_or(false))
}
#[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()))
}
Loading

0 comments on commit 5c95fe0

Please sign in to comment.