From 0adf14e8d4f34a1a907d46fcc091ba4eb37610bb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nicol=C3=A1s=20Ortega=20Froysa?= Date: Thu, 4 Aug 2022 10:12:46 +0200 Subject: [PATCH] Change weight to priority. --- src/habit.rs | 14 +++++++++----- src/habitmgr.rs | 20 ++++++++++---------- src/main.rs | 20 +++++++++++--------- 3 files changed, 30 insertions(+), 24 deletions(-) diff --git a/src/habit.rs b/src/habit.rs index 18fbf75..6663044 100644 --- a/src/habit.rs +++ b/src/habit.rs @@ -26,21 +26,25 @@ pub struct Habit uid:String, name:String, bad:bool, - weight:u8, + priority:char, // Day 0 is Monday. Use number_days_from_monday() to determine index. days_active:[bool;7], } impl Habit { - pub fn new(name:String, bad:bool, weight:u8, days_active:[bool;7]) -> Self + pub fn new(name:String, bad:bool, priority:char, days_active:[bool;7]) -> Self { + if ![ 'H','L','M' ].contains(&priority) + { + panic!("Unknown priority {}", priority); + } Self { uid: Uuid::new_v4().hyphenated().to_string(), name, bad, - weight, + priority, days_active, } } @@ -80,10 +84,10 @@ impl Habit pub fn get_uid(&self) -> &String { &self.uid } pub fn get_name(&self) -> &String { &self.name } pub fn get_bad(&self) -> bool { self.bad } - pub fn get_weight(&self) -> u8 { self.weight } + pub fn get_priority(&self) -> char { self.priority } pub fn set_name(&mut self, name:String) { self.name = name; } pub fn set_bad(&mut self, bad:bool) { self.bad = bad; } - pub fn set_weight(&mut self, weight:u8) { self.weight = weight; } + pub fn set_priority(&mut self, priority:char) { self.priority = priority; } pub fn set_days(&mut self, days:[bool;7]) { self.days_active = days; } } diff --git a/src/habitmgr.rs b/src/habitmgr.rs index 221e0ae..5b91f7b 100644 --- a/src/habitmgr.rs +++ b/src/habitmgr.rs @@ -126,10 +126,10 @@ impl HabitMgr return days_active; } - pub fn add(&mut self, name:String, bad:bool, weight:u8, days:String) + pub fn add(&mut self, name:String, bad:bool, priority:char, days:String) { let days_active = HabitMgr::days_array_from_string(days); - self.habits.push(Habit::new(name.clone(), bad, weight, days_active)); + self.habits.push(Habit::new(name.clone(), bad, priority, days_active)); self.export_habits(); println!("New habit {} added.", &name); @@ -150,13 +150,13 @@ impl HabitMgr println!("UID: {}", habit.get_uid()); println!("Active Days: {}", habit.active_days_string()); println!("Bad: {}", habit.get_bad()); - println!("Weight: {}", habit.get_weight()); + println!("Weight: {}", habit.get_priority()); } pub fn modify(&mut self, id:usize, name:Option, toggle_bad:bool, - weight:Option, + priority:Option, days:Option) { if name.is_some() @@ -168,9 +168,9 @@ impl HabitMgr let is_bad = self.habits[id].get_bad(); self.habits[id].set_bad(!is_bad); } - if weight.is_some() + if priority.is_some() { - self.habits[id].set_weight(weight.unwrap()); + self.habits[id].set_priority(priority.unwrap()); } if days.is_some() { @@ -192,16 +192,16 @@ impl HabitMgr } else { - println!(" {0: <3} | {1: <5} | {2: <6} | {3}", - "id", "bad", "weight", "name"); + println!(" {0: <3} | {1: <5} | {2: <8} | {3}", + "id", "bad", "priority", "name"); for (i, habit) in self.habits.iter().enumerate() { if all || habit.active_today() { - println!(" {0: <3} | {1: <5} | {2: <6} | {3}", + println!(" {0: <3} | {1: <5} | {2: <8} | {3}", i, habit.get_bad(), - habit.get_weight(), + habit.get_priority(), habit.get_name()); } } diff --git a/src/main.rs b/src/main.rs index a20a5fb..01860c3 100644 --- a/src/main.rs +++ b/src/main.rs @@ -49,9 +49,11 @@ enum Command days:String, #[structopt(long, help = "Assign habit as a bad habit (negative points)")] bad:bool, - #[structopt(short, long, help = "Weight/priority of the new habit", - default_value = "5")] - weight:u8, + #[structopt(short, + long, + help = "Priority of the new habit (L, M, or H)", + default_value = "M")] + priority:char, }, Commit { }, #[structopt(alias = "del")] @@ -76,8 +78,8 @@ enum Command id:usize, #[structopt(short, long, help = "New name for the habit")] name:Option, - #[structopt(short, long, help = "New weight of the habit")] - weight:Option, + #[structopt(short, long, help = "New priority for the habit (L, M, or H)")] + priority:Option, #[structopt(long, short, help = "New days the habit is active")] days:Option, #[structopt(long, help = "Toggle the 'bad' value of the habit")] @@ -116,16 +118,16 @@ fn main() Some(c) => match c { - Command::Add { name, days, bad, weight } => - hmgr.add(name, bad, weight, days), + Command::Add { name, days, bad, priority } => + hmgr.add(name, bad, priority, days), Command::Delete { id } => hmgr.delete(id), Command::Info { id } => hmgr.habit_info(id), Command::List { all } => hmgr.list(all), - Command::Modify { id, name, weight, days, toggle_bad } => - hmgr.modify(id, name, toggle_bad, weight, days), + Command::Modify { id, name, priority, days, toggle_bad } => + hmgr.modify(id, name, toggle_bad, priority, days), _ => todo!(), }, }