From a5f2f1866b7ba430d2d205b7ebb97792a2ac22bc Mon Sep 17 00:00:00 2001 From: teesh3rt Date: Thu, 27 Mar 2025 13:07:37 +0200 Subject: [PATCH] feat: add a convenience function for repls, will be useful later! --- coreutils/src/commands/tee.rs | 2 +- utils/src/input.rs | 4 ++++ utils/src/lib.rs | 3 ++- 3 files changed, 7 insertions(+), 2 deletions(-) create mode 100644 utils/src/input.rs diff --git a/coreutils/src/commands/tee.rs b/coreutils/src/commands/tee.rs index e149097..ed8bf38 100644 --- a/coreutils/src/commands/tee.rs +++ b/coreutils/src/commands/tee.rs @@ -39,7 +39,7 @@ impl Command for Tee { } let mut buffer = String::new(); - while std::io::stdin().read_line(&mut buffer).unwrap_or(0) > 0 { + while boxutils::input::get_like_repl(&mut buffer) { for output in &mut writes { let _ = output.write_all(buffer.as_bytes()); let _ = output.flush(); diff --git a/utils/src/input.rs b/utils/src/input.rs new file mode 100644 index 0000000..dfeb1da --- /dev/null +++ b/utils/src/input.rs @@ -0,0 +1,4 @@ +// this function makes it so that an EOF (commonly used in pipes) stops the repl +pub fn get_like_repl(buffer: &mut String) -> bool { + std::io::stdin().read_line(buffer).unwrap_or(0) > 0 +} diff --git a/utils/src/lib.rs b/utils/src/lib.rs index 55df7ff..2cfa076 100644 --- a/utils/src/lib.rs +++ b/utils/src/lib.rs @@ -1,4 +1,5 @@ pub mod args; pub mod commands; +pub mod encoding; +pub mod input; pub mod registry; -pub mod encoding; \ No newline at end of file