it works now
cargo devel CI / cargo CI (push) Successful in 27s Details

This commit is contained in:
Christoph J. Scherr 2024-02-21 14:09:46 +01:00
parent bec7203240
commit f6f91e5201
Signed by: cscherrNT
GPG Key ID: 8E2B45BC51A27EA7
4 changed files with 26 additions and 17 deletions

View File

@ -1,5 +1,5 @@
[package] [package]
name = "see" name = "seep"
version = "0.1.0" version = "0.1.0"
edition = "2021" edition = "2021"
publish = true publish = true
@ -7,10 +7,8 @@ authors = ["Christoph J. Scherr <software@cscherr.de>"]
license = "MIT" license = "MIT"
description = "print stdin to terminal, then to stdout (for piping)" description = "print stdin to terminal, then to stdout (for piping)"
readme = "README.md" readme = "README.md"
homepage = "https://git.cscherr.de/PlexSheep/see" homepage = "https://git.cscherr.de/PlexSheep/seep"
repository = "https://git.cscherr.de/PlexSheep/see" repository = "https://git.cscherr.de/PlexSheep/seep"
keywords = ["cli"] keywords = ["cli"]
[dependencies] [dependencies]

View File

@ -1,3 +1,3 @@
# see # seep
print stdin to terminal, then pipe into next process print stdin to terminal, then pipe into next process

View File

@ -1,15 +1,30 @@
use std::io::{self, prelude::*}; use std::io::{self, prelude::*};
const TTY: &str = "/dev/tty";
fn main() -> Result<(), io::Error> { 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() let mut tty = std::fs::File::options()
.write(true) .write(true)
.read(false) .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}"))?; .inspect_err(|err| eprintln!("{err}"))?;
// FIXME: only works with cargo run? // we want to write to the stdout too
io::copy(&mut io::stdin(), &mut tty).inspect_err(|err| eprintln!("{err}"))?; let mut stdout = io::stdout();
tty.flush().inspect_err(|err| eprintln!("{err}"))?; // sadly, we need to store the content of `stdin` in a buffer and cannot just `io::copy()` it
io::copy(&mut io::stdin(), &mut io::stdout()).inspect_err(|err| eprintln!("{err}"))?; // 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(()) Ok(())
} }

View File

@ -1,4 +0,0 @@
#[test]
fn pipe_echo_cat() {
todo!()
}