feat: add initial copy command

This commit is contained in:
2025-01-10 08:52:01 +01:00
commit 27796dd359
11 changed files with 805 additions and 0 deletions

1
crates/voidpin/.gitignore vendored Normal file
View File

@@ -0,0 +1 @@
/target

16
crates/voidpin/Cargo.toml Normal file
View File

@@ -0,0 +1,16 @@
[package]
name = "voidpin"
edition = "2021"
version.workspace = true
[dependencies]
anyhow.workspace = true
tokio.workspace = true
tracing.workspace = true
tracing-subscriber.workspace = true
clap.workspace = true
dotenv.workspace = true
serde = { version = "1.0.197", features = ["derive"] }
uuid = { version = "1.7.0", features = ["v4"] }

View File

@@ -0,0 +1,66 @@
use std::{io::Read, process::Stdio};
use anyhow::Context;
use clap::{Parser, Subcommand};
use tokio::io::AsyncWriteExt;
#[derive(Parser)]
#[command(author, version, about, long_about = None, subcommand_required = true)]
struct Command {
#[command(subcommand)]
command: Option<Commands>,
}
#[derive(Subcommand)]
enum Commands {
Listen {},
Copy {},
}
#[tokio::main]
async fn main() -> anyhow::Result<()> {
dotenv::dotenv().ok();
tracing_subscriber::fmt::init();
let cli = Command::parse();
tracing::debug!("Starting cli");
match cli.command.unwrap() {
Commands::Listen {} => {}
Commands::Copy {} => {
let mut input = String::new();
std::io::stdin()
.read_to_string(&mut input)
.context("failed to read from stdin")?;
if input.is_empty() {
tracing::info!("no content to put in clipboard");
return Ok(());
}
tracing::debug!(content = &input, "found content");
// FIXME: hardcode for macos
let mut copy_process = tokio::process::Command::new("pbcopy")
.stdin(Stdio::piped())
.spawn()?;
if let Some(mut stdin_handle) = copy_process.stdin.take() {
stdin_handle
.write_all(input.as_bytes())
.await
.context("failed to write input to copy process")?;
stdin_handle
.flush()
.await
.context("failed to flush to program")?;
}
let status = copy_process.wait().await?;
tracing::info!("copy process ended with status: {:?}", status);
}
_ => (),
}
Ok(())
}