feat: add basic cut-after

This commit is contained in:
2025-01-14 10:13:22 +01:00
commit 3366d2d934
11 changed files with 736 additions and 0 deletions

1
crates/cut-after/.gitignore vendored Normal file
View File

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

View File

@@ -0,0 +1,13 @@
[package]
name = "cut-after"
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

View File

@@ -0,0 +1,40 @@
use std::io::{Read, Write};
use anyhow::Context;
use clap::Parser;
#[derive(Parser)]
#[command(author, version, about, long_about = None)]
struct Command {
#[arg()]
substr: String,
}
#[tokio::main]
async fn main() -> anyhow::Result<()> {
dotenv::dotenv().ok();
tracing_subscriber::fmt::init();
let cli = Command::parse();
tracing::debug!("Starting cli");
let mut input = String::new();
std::io::stdin()
.read_to_string(&mut input)
.context("failed to read from stdin")?;
let mut stdout = std::io::stdout();
if let Some((before, _)) = input.split_once(&cli.substr) {
stdout.write_all(before.as_bytes())?;
} else {
stdout.write_all(input.as_bytes())?;
}
stdout.flush()?;
if input.is_empty() {
tracing::info!("no content to put in clipboard");
return Ok(());
}
Ok(())
}