Skip to content

Commit

Permalink
Merge pull request #78 from mufeedvh/cross-compilation
Browse files Browse the repository at this point in the history
Code2prompt should compile on all platforms
  • Loading branch information
ODAncona authored Feb 21, 2025
2 parents c752483 + 8dedb9a commit 2f3bcbe
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 39 deletions.
67 changes: 34 additions & 33 deletions src/clipboard.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
use anyhow::{Context, Result};
use arboard::{Clipboard, LinuxClipboardKind};
use colored::*;
use log::info;
use std::process::{Command, Stdio};
use arboard::Clipboard;

/// Copies the provided text to the system clipboard.
///
Expand All @@ -29,8 +26,36 @@ pub fn copy_text_to_clipboard(rendered: &str) -> Result<()> {
}

#[cfg(target_os = "linux")]
use arboard::SetExtLinux;
/// Entry point for the clipboard daemon process on Linux.
///
/// This function reads clipboard content from its standard input, sets it as the system clipboard,
/// and then waits to serve clipboard requests. This ensures that the clipboard content remains available
/// even after the main application exits. The daemon will exit automatically once the clipboard is overwritten.
///
/// # Returns
///
/// * `Result<()>` - Returns Ok on success or an error if clipboard operations fail.
pub fn serve_clipboard_daemon() -> anyhow::Result<()> {
use arboard::{Clipboard, LinuxClipboardKind, SetExtLinux};
use std::io::Read;
// Read content from stdin
let mut content_from_stdin = String::new();
std::io::stdin()
.read_to_string(&mut content_from_stdin)
.context("Failed to read from stdin")?;
// Initialize the clipboard
let mut clipboard = Clipboard::new().context("Failed to initialize clipboard")?;
// Explicitly set the clipboard selection to Clipboard (not Primary)
clipboard
.set()
.clipboard(LinuxClipboardKind::Clipboard)
.wait()
.text(content_from_stdin)
.context("Failed to set clipboard content")?;
Ok(())
}

#[cfg(target_os = "linux")]
/// Spawns a daemon process to maintain clipboard content on Linux.
///
/// On Linux (Wayland/X11), the clipboard content is owned by the process that defined it.
Expand All @@ -47,6 +72,10 @@ use arboard::SetExtLinux;
/// * `Result<()>` - Returns Ok if the daemon process was spawned and the content was sent successfully,
/// or an error if the process could not be launched or written to.
pub fn spawn_clipboard_daemon(content: &str) -> anyhow::Result<()> {
use colored::*;
use log::info;
use std::process::{Command, Stdio};

// ~~~ Setting up the command to run the daemon ~~~
let current_exe: std::path::PathBuf =
std::env::current_exe().context("Failed to get current executable path")?;
Expand Down Expand Up @@ -79,31 +108,3 @@ pub fn spawn_clipboard_daemon(content: &str) -> anyhow::Result<()> {
);
Ok(())
}

/// Entry point for the clipboard daemon process on Linux.
///
/// This function reads clipboard content from its standard input, sets it as the system clipboard,
/// and then waits to serve clipboard requests. This ensures that the clipboard content remains available
/// even after the main application exits. The daemon will exit automatically once the clipboard is overwritten.
///
/// # Returns
///
/// * `Result<()>` - Returns Ok on success or an error if clipboard operations fail.
pub fn serve_clipboard_daemon() -> anyhow::Result<()> {
use std::io::Read;
// Read content from stdin
let mut content_from_stdin = String::new();
std::io::stdin()
.read_to_string(&mut content_from_stdin)
.context("Failed to read from stdin")?;
// Initialize the clipboard
let mut clipboard = arboard::Clipboard::new().context("Failed to initialize clipboard")?;
// Explicitly set the clipboard selection to Clipboard (not Primary)
clipboard
.set()
.clipboard(LinuxClipboardKind::Clipboard)
.wait()
.text(content_from_stdin)
.context("Failed to set clipboard content")?;
Ok(())
}
5 changes: 4 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,10 @@ pub mod template;
pub mod token;
pub mod util;

pub use clipboard::{copy_text_to_clipboard, serve_clipboard_daemon, spawn_clipboard_daemon};
#[cfg(target_os = "linux")]
pub use clipboard::{serve_clipboard_daemon, spawn_clipboard_daemon};

pub use clipboard::copy_text_to_clipboard;
pub use filter::should_include_file;
pub use git::{get_git_diff, get_git_diff_between_branches, get_git_log};
pub use path::{label, traverse_directory};
Expand Down
12 changes: 7 additions & 5 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ use anyhow::{Context, Result};
use clap::Parser;
use code2prompt::{
get_git_diff, get_git_diff_between_branches, get_git_log, get_model_info, get_tokenizer,
handle_undefined_variables, handlebars_setup, label, render_template, serve_clipboard_daemon,
spawn_clipboard_daemon, traverse_directory, write_to_file, FileSortMethod,
handle_undefined_variables, handlebars_setup, label, render_template, traverse_directory,
write_to_file, FileSortMethod,
};
use colored::*;
use indicatif::{ProgressBar, ProgressStyle};
Expand All @@ -23,8 +23,8 @@ const CUSTOM_TEMPLATE_NAME: &str = "custom";
// CLI Arguments
#[derive(Parser)]
#[clap(
name = env!("CARGO_PKG_NAME"),
version = env!("CARGO_PKG_VERSION"),
name = env!("CARGO_PKG_NAME"),
version = env!("CARGO_PKG_VERSION"),
author = env!("CARGO_PKG_AUTHORS")
)]
#[command(arg_required_else_help = true)]
Expand Down Expand Up @@ -127,6 +127,7 @@ fn main() -> Result<()> {
// ~~~ Clipboard Daemon ~~~
#[cfg(target_os = "linux")]
{
use code2prompt::serve_clipboard_daemon;
if args.clipboard_daemon {
info! {"Serving clipboard daemon..."};
serve_clipboard_daemon()?;
Expand Down Expand Up @@ -290,12 +291,13 @@ fn main() -> Result<()> {
if !args.no_clipboard {
#[cfg(target_os = "linux")]
{
use code2prompt::spawn_clipboard_daemon;
spawn_clipboard_daemon(&rendered)?;
}
#[cfg(not(target_os = "linux"))]
{
use code2prompt::copy_text_to_clipboard;
match copy_to_clipboard(&rendered) {
match copy_text_to_clipboard(&rendered) {
Ok(_) => {
println!(
"{}{}{} {}",
Expand Down

0 comments on commit 2f3bcbe

Please sign in to comment.