feat: add the tee command

This commit is contained in:
Teesh 2025-03-26 16:49:30 +02:00
parent 407050aad5
commit d8bc6b620e
3 changed files with 52 additions and 0 deletions

View file

@ -19,3 +19,4 @@ command!(pwd::Pwd);
command!(sleep::Sleep);
command!(whoami::WhoAmI);
command!(hostname::Hostname);
command!(tee::Tee);

View file

@ -0,0 +1,50 @@
use boxutils::args::ArgParser;
use boxutils::commands::Command;
use std::fs::OpenOptions;
use std::io::Write;
// TODO: Add the -i flag to ignore SIGINT.
// Not done yet because we want this to be
// Windows-compatible
pub struct Tee;
impl Command for Tee {
fn execute(&self) {
let args = ArgParser::builder()
.add_flag("--help")
.add_flag("-a")
.parse_args("tee");
if args.get_flag("--help") {
println!("Usage: tee -a [FILE]...");
}
let append = args.get_flag("-a");
let files = args.get_normal_args();
let mut writes: Vec<Box<dyn Write>> = vec![Box::new(std::io::stdout())];
for file in files {
let this_file = OpenOptions::new()
.create(true)
.write(true)
.append(append)
.open(&file);
if let Ok(this_file) = this_file {
writes.push(Box::new(this_file));
} else {
eprintln!("tee: unable to open file: {}", file);
}
}
let mut buffer = String::new();
while std::io::stdin().read_line(&mut buffer).unwrap_or(0) > 0 {
for output in &mut writes {
let _ = output.write_all(buffer.as_bytes());
let _ = output.flush();
}
buffer.clear();
}
}
}

View file

@ -29,6 +29,7 @@ pub fn get_registry() -> CommandRegistry {
"sleep" => coreutils::commands::Sleep,
"whoami" => coreutils::commands::WhoAmI,
"hostname" => coreutils::commands::Hostname,
"tee" => coreutils::commands::Tee,
"box" => Boxcmd
});