From 14ba428ec829fbf26c387aa981fa47713e4b8182 Mon Sep 17 00:00:00 2001 From: Patrick Tulskie Date: Thu, 26 Jan 2023 21:38:16 -0500 Subject: [PATCH] Added support for receiving from a pipe --- Cargo.lock | 54 +++++++++++++++++++++++++++++++++++++++++++++-- dfang/Cargo.toml | 1 + dfang/src/main.rs | 14 +++++++++++- rfang/Cargo.toml | 1 + rfang/src/main.rs | 14 +++++++++++- 5 files changed, 80 insertions(+), 4 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 75a471d..ad27170 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -11,20 +11,47 @@ dependencies = [ "memchr", ] +[[package]] +name = "atty" +version = "0.2.14" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d9b39be18770d11421cdb1b9947a45dd3f37e93092cbf377614828a319d5fee8" +dependencies = [ + "hermit-abi", + "libc", + "winapi", +] + [[package]] name = "dfang" -version = "0.1.0" +version = "0.1.1" dependencies = [ + "atty", "lazy_static", "regex", ] +[[package]] +name = "hermit-abi" +version = "0.1.19" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "62b467343b94ba476dcb2500d242dadbb39557df889310ac77c5d99100aaac33" +dependencies = [ + "libc", +] + [[package]] name = "lazy_static" version = "1.4.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "e2abad23fbc42b3700f2f279844dc832adb2b2eb069b2df918f455c4e18cc646" +[[package]] +name = "libc" +version = "0.2.139" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "201de327520df007757c1f0adce6e827fe8562fbc28bfd9c15571c66ca1f5f79" + [[package]] name = "memchr" version = "2.5.0" @@ -50,8 +77,31 @@ checksum = "456c603be3e8d448b072f410900c09faf164fbce2d480456f50eea6e25f9c848" [[package]] name = "rfang" -version = "0.1.0" +version = "0.1.1" dependencies = [ + "atty", "lazy_static", "regex", ] + +[[package]] +name = "winapi" +version = "0.3.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5c839a674fcd7a98952e593242ea400abe93992746761e38641405d28b00f419" +dependencies = [ + "winapi-i686-pc-windows-gnu", + "winapi-x86_64-pc-windows-gnu", +] + +[[package]] +name = "winapi-i686-pc-windows-gnu" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ac3b87c63620426dd9b991e5ce0329eff545bccbbb34f3be09ff6fb6ab51b7b6" + +[[package]] +name = "winapi-x86_64-pc-windows-gnu" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "712e227841d057c1ee1cd2fb22fa7e5a5461ae8e48fa2ca79ec42cfc1931183f" diff --git a/dfang/Cargo.toml b/dfang/Cargo.toml index ab016e8..53f31ae 100644 --- a/dfang/Cargo.toml +++ b/dfang/Cargo.toml @@ -14,3 +14,4 @@ edition = "2021" [dependencies] regex = "1" lazy_static = "1" +atty = "0.2.*" diff --git a/dfang/src/main.rs b/dfang/src/main.rs index d2ace6e..c11300e 100644 --- a/dfang/src/main.rs +++ b/dfang/src/main.rs @@ -1,6 +1,9 @@ use lazy_static::lazy_static; use regex::Regex; use std::env; +use std::io; +use std::io::Read; +extern crate atty; lazy_static! { // Replacers @@ -20,7 +23,16 @@ fn main() { let args: Vec = env::args().collect(); if args.len() < 2 { - help(); + let mut input = String::new(); + if !atty::is(atty::Stream::Stdin) { + // read input from pipe + io::stdin().read_to_string(&mut input).unwrap(); + for line in input.lines() { + println!("{}", defang(line)); + } + } else { + help(); + } } else { for i in 1..args.len() { println!("{}", defang(&args[i])); diff --git a/rfang/Cargo.toml b/rfang/Cargo.toml index 489888b..3f59ccb 100644 --- a/rfang/Cargo.toml +++ b/rfang/Cargo.toml @@ -14,3 +14,4 @@ edition = "2021" [dependencies] regex = "1" lazy_static = "1" +atty = "0.2.*" diff --git a/rfang/src/main.rs b/rfang/src/main.rs index 9de33b1..d8fd3eb 100644 --- a/rfang/src/main.rs +++ b/rfang/src/main.rs @@ -1,6 +1,9 @@ use lazy_static::lazy_static; use regex::Regex; use std::env; +use std::io; +use std::io::Read; +extern crate atty; lazy_static! { // Replacers @@ -15,7 +18,16 @@ fn main() { let args: Vec = env::args().collect(); if args.len() < 2 { - help(); + let mut input = String::new(); + if !atty::is(atty::Stream::Stdin) { + // read input from pipe + io::stdin().read_to_string(&mut input).unwrap(); + for line in input.lines() { + println!("{}", refang(line)); + } + } else { + help(); + } } else { for i in 1..args.len() { println!("{}", refang(&args[i]));