diff --git a/Cargo.lock b/Cargo.lock index ea0cddf..785a49d 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -130,7 +130,7 @@ dependencies = [ [[package]] name = "gift_circle" -version = "0.5.1" +version = "0.6.0" dependencies = [ "anyhow", "clap", diff --git a/Cargo.toml b/Cargo.toml index 002f2ee..b42fa13 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "gift_circle" -version = "0.5.1" +version = "0.6.0" edition = "2021" authors = ["Chuck King"] diff --git a/README.md b/README.md index 928885a..3726fe1 100644 --- a/README.md +++ b/README.md @@ -50,6 +50,15 @@ Kenya Hill,kenya.hill@example.com,1,Daisy Jones Daisy Jones,daisy.jones@example.com,3,Jack Brown ``` +### Arrow Print + +If you just want a simple, single-line output that shows the name of folks and who they are assigned to give a gift to, you may use the -a/--arrow-print flag. The -a flag output will look like this: + +```shell +cargo run -- -a -i=./src/example-participants.csv +Beverly Jones -> Kenya Hill -> Billy Jones -> Jane Hill -> Daisy Jones -> Jack Brown -> Bill Jones -> Jessica Brown -> Joe Hill -> Beverly Jones +``` + ## Code This code was written and compiled on an Intel-based MacBook Pro, so the release binary should work on any Intel-based Mac. If you have an Intel MacBook, you should be able to download the gift_cirle binary that is attached to the GitHub release, modify the permissions to make it executable (chmod +x gift_circle), and invoke it against your participants file as shown above. @@ -60,6 +69,7 @@ If you have a different machine, you can install Rust for your machine, download cargo test cargo run -- --help cargo run -- --i=./src/example-participants.csv +cargo run -- -a -i=./src/example-participants.csv cargo build ./target/debug/gift_circle --i=./src/example-participants.csv ``` diff --git a/src/main.rs b/src/main.rs index ebee057..f3d9d6f 100644 --- a/src/main.rs +++ b/src/main.rs @@ -18,8 +18,6 @@ fn run() -> Result<()> { let mut rdr = csv::Reader::from_path(args.input.clone()) .with_context(|| format!("Failed to read input from {}", &args.input))?; - let mut wtr = csv::Writer::from_writer(io::stdout()); - let mut participants: Vec = vec![]; for result in rdr.deserialize() { @@ -28,11 +26,25 @@ fn run() -> Result<()> { } let gift_circle = get_gift_circle(participants)?; - for person in gift_circle { - wtr.serialize(person)?; - } - Ok(wtr.flush()?) + if args.arrow_print { + let mut names = gift_circle + .iter() + .map(|p| p.name.clone()) + .collect::>(); + // Add the first person to the end to wrap the circle + let first_person = &names.first().unwrap().clone(); + names.push(first_person.to_string()); + + println!("{}", &names.join(" -> ")); + Ok(()) + } else { + let mut wtr = csv::Writer::from_writer(io::stdout()); + for person in gift_circle { + wtr.serialize(person)?; + } + Ok(wtr.flush()?) + } } fn main() { diff --git a/src/myargs.rs b/src/myargs.rs index 3e58e69..622bef9 100644 --- a/src/myargs.rs +++ b/src/myargs.rs @@ -5,8 +5,10 @@ use clap::Parser; #[command(author, version, about, long_about = None)] pub struct Args { /// Input file - #[arg(short, long)] + #[arg(long, short)] pub input: String, + #[clap(long, short, action)] + pub arrow_print: bool, } pub fn get_args() -> Args {