Skip to content

Commit

Permalink
add reverse ability
Browse files Browse the repository at this point in the history
  • Loading branch information
vanilla-extracts committed Dec 1, 2024
1 parent ff3986c commit b4db411
Showing 1 changed file with 9 additions and 3 deletions.
12 changes: 9 additions & 3 deletions crates/cat/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,18 @@ use std::process::exit;

fn main() {
let mut args: Args = env::args();
let mut reversed = false;
if args.len() < 2 {
println!("Usage: cat <file>");
exit(1);
}
if args.len() > 2 {
reversed = !reversed;
}
let file = args.nth(1);
match file {
Some(name) => {
let s = cat(name);
let s = cat(name,reversed);
if s.is_err() {
println!("Error while reading file");
}
Expand All @@ -22,8 +26,10 @@ fn main() {
};
}

fn cat(file: String) -> Result<(), Box<dyn Error>> {
fn cat(file: String,reversed: bool) -> Result<(), Box<dyn Error>> {
let content = read_to_string(file)?;
println!("{content}");
println!("{}",
if reversed {content.lines().rev().collect::<Vec<&str>>().join("\n")} else {content}
);
Ok(())
}

0 comments on commit b4db411

Please sign in to comment.