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

Fix clipboard on linux #46

Open
wants to merge 1 commit 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
20 changes: 20 additions & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,14 @@ struct Cli {
#[clap(long)]
no_clipboard: bool,

// Used internally for maintaining clipboard on linux.
#[clap(long, hide = true)]
daemon: bool,

// Used internally for maintaining clipboard on linux.
#[clap(long, hide = true)]
clipboard_content: Option<String>,

/// Optional Path to a custom Handlebars template
#[clap(short, long)]
template: Option<PathBuf>,
Expand All @@ -100,6 +108,18 @@ fn main() -> Result<()> {
env_logger::init();
let args = Cli::parse();

if args.daemon {
if let Some(clipboard_content) = args.clipboard_content.as_ref() {
return match copy_to_clipboard(&clipboard_content) {
Ok(_) => Ok(()),
Err(e) => {
eprintln!("Error: {}", e);
std::process::exit(1)
}
}
}
}

// Handlebars Template Setup
let (template_content, template_name) = get_template(&args)?;
let handlebars = handlebars_setup(&template_content, template_name)?;
Expand Down
36 changes: 29 additions & 7 deletions src/template.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,16 @@

use anyhow::{Context, Result};
use arboard::Clipboard;

#[cfg(target_os = "linux")]
use arboard::SetExtLinux;

use colored::*;
use handlebars::{no_escape, Handlebars};
use inquire::Text;
use regex::Regex;
use std::io::Write;
use std::{env, process};

/// Set up the Handlebars template engine with a template string and a template name.
///
Expand Down Expand Up @@ -116,14 +121,31 @@ pub fn handle_undefined_variables(
///
/// * `Result<()>` - An empty result indicating success or an error.
pub fn copy_to_clipboard(rendered: &str) -> Result<()> {
match Clipboard::new() {
Ok(mut clipboard) => {
clipboard
.set_text(rendered.to_string())
.context("Failed to copy to clipboard")?;
Ok(())
#[cfg(target_os = "linux")]
if env::args().any(|arg| arg == "--daemon") {
let mut clipboard = Clipboard::new().context("Failed to initialize clipboard in daemon")?;
clipboard.set().wait().text(rendered).context("Failed to copy to clipboard in daemon")?;
return Ok(());
}

if cfg!(target_os = "linux") {
process::Command::new(env::current_exe()?)
.arg(".") // Path is required arg. Can be set as anything here.
.arg("--daemon")
.arg("--clipboard-content")
.arg(rendered)
.spawn().context("Failed to spawn daemon process")?;
return Ok(())
} else {
match Clipboard::new() {
Ok(mut clipboard) => {
clipboard
.set_text(rendered.to_string())
.context("Failed to copy to clipboard")?;
Ok(())
}
Err(e) => Err(anyhow::anyhow!("Failed to initialize clipboard: {}", e)),
}
Err(e) => Err(anyhow::anyhow!("Failed to initialize clipboard: {}", e)),
}
}

Expand Down