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

add support to non cli-clipboard target(like android) #272

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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
8 changes: 6 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ documentation = "https://amp.rs/docs"
readme = "README.md"
license-file = "LICENSE"
keywords = ["text", "editor", "terminal", "modal"]
edition="2021"
edition = "2021"

[build-dependencies]
regex = "1.10"
Expand All @@ -27,7 +27,7 @@ syntect = "5.1"
termion = "2.0"
error-chain = "0.12"
unicode-segmentation = "1.10"
cli-clipboard = "0.4"
cli-clipboard = { version = "0.4.0", optional = true }
yaml-rust = "0.4"
smallvec = "1.11"
lazy_static = "1.4"
Expand All @@ -50,3 +50,7 @@ criterion = "0.5"
name = "draw_buffer"
path = "benches/view/draw_buffer.rs"
harness = false

[features]
default = ["clipboard"]
clipboard = ["dep:cli-clipboard"]
182 changes: 112 additions & 70 deletions src/models/application/clipboard.rs
Original file line number Diff line number Diff line change
@@ -1,107 +1,149 @@
use crate::errors::*;
use cli_clipboard::{ClipboardContext, ClipboardProvider};

/// In-app content can be captured in both regular and full-line selection
/// modes. This type describes the structure of said content, based on the
/// context in which it was captured. When OS-level clipboard contents are
/// used, they are always represented as inline, as we cannot infer block
/// style without the copy context.

#[derive(Debug, PartialEq)]
pub enum ClipboardContent {
Inline(String),
Block(String),
None,
}

/// Qualifies in-app copy/paste content with structural information, and
/// synchronizes said content with the OS-level clipboard (preferring it
/// in scenarios where it differs from the in-app equivalent).
pub struct Clipboard {
content: ClipboardContent,
system_clipboard: Option<ClipboardContext>,
}
#[cfg(feature = "clipboard")]
mod clipboard_impl {
use super::*;
use cli_clipboard::{ClipboardContext, ClipboardProvider};

impl Default for Clipboard {
fn default() -> Self {
Self::new()
/// Qualifies in-app copy/paste content with structural information, and
/// synchronizes said content with the OS-level clipboard (preferring it
/// in scenarios where it differs from the in-app equivalent).
pub struct Clipboard {
content: ClipboardContent,
system_clipboard: Option<ClipboardContext>,
}
}

impl Clipboard {
pub fn new() -> Clipboard {
// Initialize and keep a reference to the system clipboard.
let system_clipboard = match ClipboardProvider::new() {
Ok(clipboard) => Some(clipboard),
Err(_) => None,
};

Clipboard {
content: ClipboardContent::None,
system_clipboard,
impl Default for Clipboard {
fn default() -> Self {
Self::new()
}
}

/// Returns the in-app clipboard content. However, if in-app content
/// differs from the system clipboard, the system clipboard content will
/// be saved to the in-app clipboard as inline data and returned instead.
pub fn get_content(&mut self) -> &ClipboardContent {
// Check the system clipboard for newer content.
let new_content = match self.system_clipboard {
Some(ref mut clipboard) => {
match clipboard.get_contents() {
Ok(content) => {
if content.is_empty() {
None
} else {
// There is system clipboard content we can use.
match self.content {
ClipboardContent::Inline(ref app_content) |
ClipboardContent::Block(ref app_content) => {
// We have in-app clipboard content, too. Prefer
// the system clipboard content if they differ.
if content != *app_content {
Some(ClipboardContent::Inline(content))
} else {
None
impl Clipboard {
pub fn new() -> Clipboard {
// Initialize and keep a reference to the system clipboard.
let system_clipboard = match ClipboardProvider::new() {
Ok(clipboard) => Some(clipboard),
Err(_) => None,
};

Clipboard {
content: ClipboardContent::None,
system_clipboard,
}
}

/// Returns the in-app clipboard content. However, if in-app content
/// differs from the system clipboard, the system clipboard content will
/// be saved to the in-app clipboard as inline data and returned instead.
pub fn get_content(&mut self) -> &ClipboardContent {
// Check the system clipboard for newer content.
let new_content = match self.system_clipboard {
Some(ref mut clipboard) => {
match clipboard.get_contents() {
Ok(content) => {
if content.is_empty() {
None
} else {
// There is system clipboard content we can use.
match self.content {
ClipboardContent::Inline(ref app_content)
| ClipboardContent::Block(ref app_content) => {
// We have in-app clipboard content, too. Prefer
// the system clipboard content if they differ.
if content != *app_content {
Some(ClipboardContent::Inline(content))
} else {
None
}
}
// We have no in-app clipboard content. Use the system's.
_ => Some(ClipboardContent::Inline(content)),
}
// We have no in-app clipboard content. Use the system's.
_ => Some(ClipboardContent::Inline(content)),
}
}
_ => None,
}
_ => None,
}
None => None,
};

// Update the in-app clipboard if we've found newer content.
if new_content.is_some() {
self.content = new_content.unwrap();
}
None => None,
};

// Update the in-app clipboard if we've found newer content.
if new_content.is_some() {
self.content = new_content.unwrap();
&self.content
}

&self.content
}
// Updates the in-app and system clipboards with the specified content.
pub fn set_content(&mut self, content: ClipboardContent) -> Result<()> {
// Update the in-app clipboard.
self.content = content;

// Updates the in-app and system clipboards with the specified content.
pub fn set_content(&mut self, content: ClipboardContent) -> Result<()> {
// Update the in-app clipboard.
self.content = content;

// Update the system clipboard.
match self.content {
ClipboardContent::Inline(ref app_content) |
ClipboardContent::Block(ref app_content) => {
if let Some(ref mut clipboard) = self.system_clipboard {
return clipboard
.set_contents(app_content.clone())
.map_err(|_| Error::from("Failed to update system clipboard"));
// Update the system clipboard.
match self.content {
ClipboardContent::Inline(ref app_content)
| ClipboardContent::Block(ref app_content) => {
if let Some(ref mut clipboard) = self.system_clipboard {
return clipboard
.set_contents(app_content.clone())
.map_err(|_| Error::from("Failed to update system clipboard"));
}
}
_ => (),
}
_ => (),

Ok(())
}
}
}
#[cfg(feature = "clipboard")]
pub use clipboard_impl::*;

#[cfg(not(feature = "clipboard"))]
mod non_clipboard_impl {
use super::*;
/// a fake clipboard implementation for non-clipboard feature
/// it just save the content in memory, do not interact with system clipboard
pub struct Clipboard {
content: ClipboardContent,
}
impl Default for Clipboard {
fn default() -> Self {
Self::new()
}
}

Ok(())
impl Clipboard {
pub fn new() -> Clipboard {
Clipboard {
content: ClipboardContent::None,
}
}

pub fn get_content(&mut self) -> &ClipboardContent {
&self.content
}

pub fn set_content(&mut self, content: ClipboardContent) -> Result<()> {
self.content = content;
Ok(())
}
}
}
#[cfg(not(feature = "clipboard"))]
pub use non_clipboard_impl::*;