Make OS clipboard support optional

This commit is contained in:
Joshua Barretto 2025-11-27 23:11:19 +00:00
parent ed3168b056
commit 33cb24000d
2 changed files with 24 additions and 12 deletions

View file

@ -3,6 +3,10 @@ name = "zte"
version = "0.2.0"
edition = "2024"
[features]
clipboard = ["dep:clipboard"]
default = ["clipboard"]
[dependencies]
clap = { version = "4.4", features = ["derive"] }
slotmap = "1.0"
@ -10,7 +14,7 @@ crossterm = "0.27"
thiserror = "1.0"
chumsky = { version = "0.10.1", features = ["pratt"] }
unicode-display-width = "0.3.0"
clipboard = "0.5.0"
clipboard = { version = "0.5.0", optional = true }
[profile.dev]
opt-level = 2

View file

@ -1,4 +1,5 @@
use crate::{Args, Dir, Error, highlight::Highlights, lang::LangPack, theme};
#[cfg(feature = "clipboard")]
use clipboard::{ClipboardContext, ClipboardProvider};
use slotmap::{HopSlotMap, new_key_type};
use std::{
@ -948,23 +949,26 @@ fn classify(c: char) -> Option<u8> {
}
}
pub struct Clipboard {
// If a global clipboard cannot be established, use a local clipboard instead
ctx: Result<ClipboardContext, String>,
pub enum Clipboard {
#[cfg(feature = "clipboard")]
Global(ClipboardContext),
Local(String),
}
impl Clipboard {
fn get(&mut self) -> Result<String, ()> {
match &mut self.ctx {
Ok(ctx) => ctx.get_contents().map_err(|_| ()),
Err(contents) => Ok(contents.clone()),
match self {
#[cfg(feature = "clipboard")]
Self::Global(ctx) => ctx.get_contents().map_err(|_| ()),
Self::Local(contents) => Ok(contents.clone()),
}
}
fn set(&mut self, text: String) -> Result<(), ()> {
match &mut self.ctx {
Ok(ctx) => ctx.set_contents(text).map_err(|_| ()),
Err(contents) => Ok(*contents = text),
match self {
#[cfg(feature = "clipboard")]
Self::Global(ctx) => ctx.set_contents(text).map_err(|_| ()),
Self::Local(contents) => Ok(*contents = text),
}
}
}
@ -985,8 +989,12 @@ impl TryFrom<Args> for State {
tick: 0,
theme: theme::Theme::default(),
most_recent_counter: 0,
clipboard: Clipboard {
ctx: ClipboardContext::new().map_err(|_| String::new()),
clipboard: 'clipboard: {
#[cfg(feature = "clipboard")]
if let Ok(ctx) = ClipboardContext::new() {
break 'clipboard Clipboard::Global(ctx);
}
Clipboard::Local(String::new())
},
};