Implement active day functionality.
This commit is contained in:
39
src/habit.rs
39
src/habit.rs
@ -18,6 +18,7 @@
|
||||
|
||||
use uuid::Uuid;
|
||||
use serde::{Serialize, Deserialize};
|
||||
use time::OffsetDateTime;
|
||||
|
||||
#[derive(Serialize, Deserialize, Debug)]
|
||||
pub struct Habit
|
||||
@ -26,11 +27,13 @@ pub struct Habit
|
||||
name:String,
|
||||
bad:bool,
|
||||
weight:u8,
|
||||
// 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) -> Self
|
||||
pub fn new(name:String, bad:bool, weight:u8, days_active:[bool;7]) -> Self
|
||||
{
|
||||
Self
|
||||
{
|
||||
@ -38,9 +41,42 @@ impl Habit
|
||||
name,
|
||||
bad,
|
||||
weight,
|
||||
days_active,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn active_days_string(&self) -> String
|
||||
{
|
||||
let mut res:String = String::new();
|
||||
|
||||
for (i,v) in self.days_active.iter().enumerate()
|
||||
{
|
||||
if *v
|
||||
{
|
||||
res.push_str(match i {
|
||||
0 => "mon",
|
||||
1 => "tue",
|
||||
2 => "wed",
|
||||
3 => "thu",
|
||||
4 => "fri",
|
||||
5 => "sat",
|
||||
6 => "sun",
|
||||
_ => "unk", // this one will never occur, but the compiler complains
|
||||
});
|
||||
res.push(',');
|
||||
}
|
||||
}
|
||||
res.pop();
|
||||
|
||||
return res;
|
||||
}
|
||||
|
||||
pub fn active_today(&self) -> bool
|
||||
{
|
||||
return self.days_active[OffsetDateTime::now_local().unwrap()
|
||||
.weekday().number_days_from_monday() as usize]
|
||||
}
|
||||
|
||||
pub fn get_uid(&self) -> &String { &self.uid }
|
||||
pub fn get_name(&self) -> &String { &self.name }
|
||||
pub fn get_bad(&self) -> bool { self.bad }
|
||||
@ -49,4 +85,5 @@ impl Habit
|
||||
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_days(&mut self, days:[bool;7]) { self.days_active = days; }
|
||||
}
|
||||
|
@ -19,6 +19,7 @@
|
||||
use std::path::PathBuf;
|
||||
use std::fs::OpenOptions;
|
||||
use std::io::{BufReader, BufWriter};
|
||||
use time::Weekday;
|
||||
|
||||
use crate::habit::Habit;
|
||||
|
||||
@ -95,9 +96,40 @@ impl HabitMgr
|
||||
});
|
||||
}
|
||||
|
||||
pub fn add(&mut self, name:String, bad:bool, weight:u8, _days:String)
|
||||
fn days_array_from_string(days:String) -> [bool;7]
|
||||
{
|
||||
self.habits.push(Habit::new(name.clone(), bad, weight));
|
||||
let days_list = days.split(",");
|
||||
let mut days_active:[bool;7] = [ false; 7 ];
|
||||
for i in days_list
|
||||
{
|
||||
match i
|
||||
{
|
||||
"mon" =>
|
||||
days_active[Weekday::Monday.number_days_from_monday() as usize] = true,
|
||||
"tue" =>
|
||||
days_active[Weekday::Tuesday.number_days_from_monday() as usize] = true,
|
||||
"wed" =>
|
||||
days_active[Weekday::Wednesday.number_days_from_monday() as usize] = true,
|
||||
"thu" =>
|
||||
days_active[Weekday::Thursday.number_days_from_monday() as usize] = true,
|
||||
"fri" =>
|
||||
days_active[Weekday::Friday.number_days_from_monday() as usize] = true,
|
||||
"sat" =>
|
||||
days_active[Weekday::Saturday.number_days_from_monday() as usize] = true,
|
||||
"sun" =>
|
||||
days_active[Weekday::Sunday.number_days_from_monday() as usize] = true,
|
||||
_ =>
|
||||
panic!("Day {} not recognized!", i),
|
||||
}
|
||||
}
|
||||
|
||||
return days_active;
|
||||
}
|
||||
|
||||
pub fn add(&mut self, name:String, bad:bool, weight:u8, days:String)
|
||||
{
|
||||
let days_active = HabitMgr::days_array_from_string(days);
|
||||
self.habits.push(Habit::new(name.clone(), bad, weight, days_active));
|
||||
self.export_habits();
|
||||
|
||||
println!("New habit {} added.", &name);
|
||||
@ -116,6 +148,7 @@ impl HabitMgr
|
||||
println!("Name: {}", habit.get_name());
|
||||
println!("ID: {}", id);
|
||||
println!("UID: {}", habit.get_uid());
|
||||
println!("Active Days: {}", habit.active_days_string());
|
||||
println!("Bad: {}", habit.get_bad());
|
||||
println!("Weight: {}", habit.get_weight());
|
||||
}
|
||||
@ -124,7 +157,7 @@ impl HabitMgr
|
||||
name:Option<String>,
|
||||
toggle_bad:bool,
|
||||
weight:Option<u8>,
|
||||
_days:Option<String>)
|
||||
days:Option<String>)
|
||||
{
|
||||
if name.is_some()
|
||||
{
|
||||
@ -139,26 +172,38 @@ impl HabitMgr
|
||||
{
|
||||
self.habits[id].set_weight(weight.unwrap());
|
||||
}
|
||||
if days.is_some()
|
||||
{
|
||||
let days_active = HabitMgr::days_array_from_string(days.unwrap());
|
||||
self.habits[id].set_days(days_active);
|
||||
}
|
||||
self.export_habits();
|
||||
}
|
||||
|
||||
pub fn list(&self, _all:bool)
|
||||
pub fn list(&self, all:bool)
|
||||
{
|
||||
if self.habits.is_empty()
|
||||
{
|
||||
println!("There are no habits. Add one!");
|
||||
}
|
||||
else if !all && self.habits.iter().all(|i| !i.active_today())
|
||||
{
|
||||
println!("No active habits available today!");
|
||||
}
|
||||
else
|
||||
{
|
||||
println!(" {0: <3} | {1: <5} | {2: <6} | {3}",
|
||||
"id", "bad", "weight", "name");
|
||||
for (i, habit) in self.habits.iter().enumerate()
|
||||
{
|
||||
println!(" {0: <3} | {1: <5} | {2: <6} | {3}",
|
||||
i,
|
||||
habit.get_bad(),
|
||||
habit.get_weight(),
|
||||
habit.get_name());
|
||||
if all || habit.active_today()
|
||||
{
|
||||
println!(" {0: <3} | {1: <5} | {2: <6} | {3}",
|
||||
i,
|
||||
habit.get_bad(),
|
||||
habit.get_weight(),
|
||||
habit.get_name());
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user