Change weight to priority.

This commit is contained in:
Nicolás A. Ortega Froysa 2022-08-04 10:12:46 +02:00
parent 3995a47078
commit 0adf14e8d4
3 changed files with 30 additions and 24 deletions

View File

@ -26,21 +26,25 @@ pub struct Habit
uid:String, uid:String,
name:String, name:String,
bad:bool, bad:bool,
weight:u8, priority:char,
// Day 0 is Monday. Use number_days_from_monday() to determine index. // Day 0 is Monday. Use number_days_from_monday() to determine index.
days_active:[bool;7], days_active:[bool;7],
} }
impl Habit 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 Self
{ {
uid: Uuid::new_v4().hyphenated().to_string(), uid: Uuid::new_v4().hyphenated().to_string(),
name, name,
bad, bad,
weight, priority,
days_active, days_active,
} }
} }
@ -80,10 +84,10 @@ impl Habit
pub fn get_uid(&self) -> &String { &self.uid } pub fn get_uid(&self) -> &String { &self.uid }
pub fn get_name(&self) -> &String { &self.name } pub fn get_name(&self) -> &String { &self.name }
pub fn get_bad(&self) -> bool { self.bad } 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_name(&mut self, name:String) { self.name = name; }
pub fn set_bad(&mut self, bad:bool) { self.bad = bad; } 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; } pub fn set_days(&mut self, days:[bool;7]) { self.days_active = days; }
} }

View File

@ -126,10 +126,10 @@ impl HabitMgr
return days_active; 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); 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(); self.export_habits();
println!("New habit {} added.", &name); println!("New habit {} added.", &name);
@ -150,13 +150,13 @@ impl HabitMgr
println!("UID: {}", habit.get_uid()); println!("UID: {}", habit.get_uid());
println!("Active Days: {}", habit.active_days_string()); println!("Active Days: {}", habit.active_days_string());
println!("Bad: {}", habit.get_bad()); println!("Bad: {}", habit.get_bad());
println!("Weight: {}", habit.get_weight()); println!("Weight: {}", habit.get_priority());
} }
pub fn modify(&mut self, id:usize, pub fn modify(&mut self, id:usize,
name:Option<String>, name:Option<String>,
toggle_bad:bool, toggle_bad:bool,
weight:Option<u8>, priority:Option<char>,
days:Option<String>) days:Option<String>)
{ {
if name.is_some() if name.is_some()
@ -168,9 +168,9 @@ impl HabitMgr
let is_bad = self.habits[id].get_bad(); let is_bad = self.habits[id].get_bad();
self.habits[id].set_bad(!is_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() if days.is_some()
{ {
@ -192,16 +192,16 @@ impl HabitMgr
} }
else else
{ {
println!(" {0: <3} | {1: <5} | {2: <6} | {3}", println!(" {0: <3} | {1: <5} | {2: <8} | {3}",
"id", "bad", "weight", "name"); "id", "bad", "priority", "name");
for (i, habit) in self.habits.iter().enumerate() for (i, habit) in self.habits.iter().enumerate()
{ {
if all || habit.active_today() if all || habit.active_today()
{ {
println!(" {0: <3} | {1: <5} | {2: <6} | {3}", println!(" {0: <3} | {1: <5} | {2: <8} | {3}",
i, i,
habit.get_bad(), habit.get_bad(),
habit.get_weight(), habit.get_priority(),
habit.get_name()); habit.get_name());
} }
} }

View File

@ -49,9 +49,11 @@ enum Command
days:String, days:String,
#[structopt(long, help = "Assign habit as a bad habit (negative points)")] #[structopt(long, help = "Assign habit as a bad habit (negative points)")]
bad:bool, bad:bool,
#[structopt(short, long, help = "Weight/priority of the new habit", #[structopt(short,
default_value = "5")] long,
weight:u8, help = "Priority of the new habit (L, M, or H)",
default_value = "M")]
priority:char,
}, },
Commit { }, Commit { },
#[structopt(alias = "del")] #[structopt(alias = "del")]
@ -76,8 +78,8 @@ enum Command
id:usize, id:usize,
#[structopt(short, long, help = "New name for the habit")] #[structopt(short, long, help = "New name for the habit")]
name:Option<String>, name:Option<String>,
#[structopt(short, long, help = "New weight of the habit")] #[structopt(short, long, help = "New priority for the habit (L, M, or H)")]
weight:Option<u8>, priority:Option<char>,
#[structopt(long, short, help = "New days the habit is active")] #[structopt(long, short, help = "New days the habit is active")]
days:Option<String>, days:Option<String>,
#[structopt(long, help = "Toggle the 'bad' value of the habit")] #[structopt(long, help = "Toggle the 'bad' value of the habit")]
@ -116,16 +118,16 @@ fn main()
Some(c) => Some(c) =>
match c match c
{ {
Command::Add { name, days, bad, weight } => Command::Add { name, days, bad, priority } =>
hmgr.add(name, bad, weight, days), hmgr.add(name, bad, priority, days),
Command::Delete { id } => Command::Delete { id } =>
hmgr.delete(id), hmgr.delete(id),
Command::Info { id } => Command::Info { id } =>
hmgr.habit_info(id), hmgr.habit_info(id),
Command::List { all } => Command::List { all } =>
hmgr.list(all), hmgr.list(all),
Command::Modify { id, name, weight, days, toggle_bad } => Command::Modify { id, name, priority, days, toggle_bad } =>
hmgr.modify(id, name, toggle_bad, weight, days), hmgr.modify(id, name, toggle_bad, priority, days),
_ => todo!(), _ => todo!(),
}, },
} }