diff --git a/Cargo.toml b/Cargo.toml index 8bc7f62..9d1921a 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,5 +1,5 @@ [package] -name = "see" +name = "seep" version = "0.1.0" edition = "2021" publish = true @@ -7,10 +7,8 @@ authors = ["Christoph J. Scherr "] license = "MIT" description = "print stdin to terminal, then to stdout (for piping)" readme = "README.md" -homepage = "https://git.cscherr.de/PlexSheep/see" -repository = "https://git.cscherr.de/PlexSheep/see" +homepage = "https://git.cscherr.de/PlexSheep/seep" +repository = "https://git.cscherr.de/PlexSheep/seep" keywords = ["cli"] - [dependencies] - diff --git a/README.md b/README.md index 52b1b5f..947e0a1 100644 --- a/README.md +++ b/README.md @@ -1,3 +1,3 @@ -# see +# seep -print stdin to terminal, then pipe into next process \ No newline at end of file +print stdin to terminal, then pipe into next process diff --git a/src/main.rs b/src/main.rs index 1f56fa6..80abace 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,15 +1,30 @@ use std::io::{self, prelude::*}; +const TTY: &str = "/dev/tty"; fn main() -> Result<(), io::Error> { - // TODO: check if stdin is empty and show usage + // /dev/tty points the currently opened terminal. If we write to it directly, we bypass piping + // to the next process let mut tty = std::fs::File::options() .write(true) .read(false) - .open("/dev/tty") + // try to use $TTY set by the terminal, otherwise use the default tty + .open(std::env::var("TTY").unwrap_or(String::from(TTY))) .inspect_err(|err| eprintln!("{err}"))?; - // FIXME: only works with cargo run? - io::copy(&mut io::stdin(), &mut tty).inspect_err(|err| eprintln!("{err}"))?; - tty.flush().inspect_err(|err| eprintln!("{err}"))?; - io::copy(&mut io::stdin(), &mut io::stdout()).inspect_err(|err| eprintln!("{err}"))?; + // we want to write to the stdout too + let mut stdout = io::stdout(); + // sadly, we need to store the content of `stdin` in a buffer and cannot just `io::copy()` it + // multiple times. `tee` from the GNU coreutils does this too. + let mut buf = Vec::new(); + io::stdin() + .read_to_end(&mut buf) + .inspect_err(|err| eprintln!("{err}"))?; + + // now we just write to our targets + tty.write_all(&buf).inspect_err(|err| eprintln!("{err}"))?; + tty.flush().inspect_err(|err| eprintln!("{err}"))?; // make sure it comes there + stdout + .write_all(&buf) + .inspect_err(|err| eprintln!("{err}"))?; + stdout.flush().inspect_err(|err| eprintln!("{err}"))?; // make sure it comes there Ok(()) } diff --git a/tests/cli.rs b/tests/cli.rs deleted file mode 100644 index 6671249..0000000 --- a/tests/cli.rs +++ /dev/null @@ -1,4 +0,0 @@ -#[test] -fn pipe_echo_cat() { - todo!() -}