feat: add initial copy command
This commit is contained in:
1
crates/voidpin/.gitignore
vendored
Normal file
1
crates/voidpin/.gitignore
vendored
Normal file
@@ -0,0 +1 @@
|
||||
/target
|
16
crates/voidpin/Cargo.toml
Normal file
16
crates/voidpin/Cargo.toml
Normal 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"] }
|
66
crates/voidpin/src/main.rs
Normal file
66
crates/voidpin/src/main.rs
Normal 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(())
|
||||
}
|
Reference in New Issue
Block a user