Skip to content

Commit

Permalink
tasks: add done flag
Browse files Browse the repository at this point in the history
  • Loading branch information
bouzuya committed May 6, 2021
1 parent 66ec119 commit 4d3799a
Showing 1 changed file with 16 additions and 3 deletions.
19 changes: 16 additions & 3 deletions tasks/src/main.rs
Expand Up @@ -3,9 +3,15 @@ use std::{
path::{Path, PathBuf},
};

#[derive(Debug, serde::Deserialize, serde::Serialize)]
struct Task {
done: bool,
text: String,
}

#[derive(Debug, serde::Deserialize, serde::Serialize)]
struct TasksJson {
tasks: Vec<String>,
tasks: Vec<Task>,
}

fn tasks_json_path() -> PathBuf {
Expand Down Expand Up @@ -34,12 +40,19 @@ fn main() {
"add" => {
let text = env::args().nth(2).unwrap();
let mut json = read_tasks_json(path.as_path());
json.tasks.push(text);
json.tasks.push(Task { done: false, text });
write_tasks_json(path.as_path(), &json);
}
"list" => {
let json = read_tasks_json(path.as_path());
println!("{}", json.tasks.join("\n"));
println!(
"{}",
json.tasks
.iter()
.map(|task| format!("{} {}", if task.done { "☑" } else { "☐" }, task.text))
.collect::<Vec<String>>()
.join("\n")
);
}
_ => panic!("invalid subcommand"),
}
Expand Down

0 comments on commit 4d3799a

Please sign in to comment.