From 327fd3098ad51a824d1ee5db38ae6c5d38c3272f Mon Sep 17 00:00:00 2001 From: kjuulh Date: Fri, 10 Jan 2025 09:07:44 +0100 Subject: [PATCH] chore: extract local copier to struct --- crates/voidpin/src/copy.rs | 48 +++++++++++++++++++++++++++++++++++++ crates/voidpin/src/main.rs | 30 +++++++---------------- crates/voidpin/src/state.rs | 8 +++++++ 3 files changed, 65 insertions(+), 21 deletions(-) create mode 100644 crates/voidpin/src/copy.rs create mode 100644 crates/voidpin/src/state.rs diff --git a/crates/voidpin/src/copy.rs b/crates/voidpin/src/copy.rs new file mode 100644 index 0000000..0f28a75 --- /dev/null +++ b/crates/voidpin/src/copy.rs @@ -0,0 +1,48 @@ +use std::process::Stdio; + +use anyhow::Context; +use tokio::io::AsyncWriteExt; + +use crate::state::State; + +#[derive(Default)] +pub struct LocalCopier {} + +impl LocalCopier { + pub fn new() -> Self { + Self {} + } + + pub async fn copy(&self, input: &[u8]) -> anyhow::Result<()> { + // 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) + .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(()) + } +} + +pub trait LocalCopierState { + fn local_copier(&self) -> LocalCopier; +} + +impl LocalCopierState for State { + fn local_copier(&self) -> LocalCopier { + LocalCopier::new() + } +} diff --git a/crates/voidpin/src/main.rs b/crates/voidpin/src/main.rs index 12fcc6e..b5b8b8c 100644 --- a/crates/voidpin/src/main.rs +++ b/crates/voidpin/src/main.rs @@ -1,8 +1,12 @@ -use std::{io::Read, process::Stdio}; +use std::io::Read; use anyhow::Context; use clap::{Parser, Subcommand}; -use tokio::io::AsyncWriteExt; +use copy::LocalCopierState; +use state::State; + +mod copy; +mod state; #[derive(Parser)] #[command(author, version, about, long_about = None, subcommand_required = true)] @@ -25,6 +29,8 @@ async fn main() -> anyhow::Result<()> { let cli = Command::parse(); tracing::debug!("Starting cli"); + let state = State::new(); + match cli.command.unwrap() { Commands::Listen {} => {} Commands::Copy {} => { @@ -39,25 +45,7 @@ async fn main() -> anyhow::Result<()> { } 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); + state.local_copier().copy(input.as_bytes()).await?; } _ => (), } diff --git a/crates/voidpin/src/state.rs b/crates/voidpin/src/state.rs new file mode 100644 index 0000000..d214087 --- /dev/null +++ b/crates/voidpin/src/state.rs @@ -0,0 +1,8 @@ +#[derive(Clone)] +pub struct State {} + +impl State { + pub fn new() -> Self { + Self {} + } +}