Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added arrow print output flag #21

Merged
merged 1 commit into from
Nov 24, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "gift_circle"
version = "0.5.1"
version = "0.6.0"
edition = "2021"
authors = ["Chuck King"]

Expand Down
10 changes: 10 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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
```
24 changes: 18 additions & 6 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Person> = vec![];

for result in rdr.deserialize() {
Expand All @@ -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::<Vec<_>>();
// 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() {
Expand Down
4 changes: 3 additions & 1 deletion src/myargs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down