From 53573c878e182e99bba9d2276ced4cffc6abb42c Mon Sep 17 00:00:00 2001 From: chaos Date: Thu, 28 Nov 2024 00:14:50 +0000 Subject: [PATCH] update --- journal/src/calculate.rs | 33 +++++---- journal/src/types/nutrients.rs | 2 +- journal_cli/src/commands/print_session.rs | 5 +- journal_cli/src/commands/psychonaut/import.rs | 5 +- journal_cli/src/display.rs | 70 +++++++++++-------- 5 files changed, 68 insertions(+), 47 deletions(-) diff --git a/journal/src/calculate.rs b/journal/src/calculate.rs index d7c0d5a..345f462 100644 --- a/journal/src/calculate.rs +++ b/journal/src/calculate.rs @@ -14,21 +14,25 @@ pub struct CalculatedValues { pub hydration: HydrationIngestion, } -pub fn calculate_sustenance_ingestion_amounts(sustenance: &mut SustenanceIngestion) { - let includes = &mut sustenance.sustenance.as_mut().unwrap().includes; +pub fn calculate_ingestion_amounts(ingestion: &mut IngestionKind, recurse: bool) { + if let IngestionKind::Sustenance(sustenance) = ingestion { + let includes = &mut sustenance.sustenance.as_mut().unwrap().includes; - for include in includes.iter_mut() { - match include { - IngestionKind::Substance(ingestion) => { - ingestion.dose = Dose::new_precise(sustenance.amount) * ingestion.dose.clone(); - } - IngestionKind::Sustenance(ingestion) => { - ingestion.amount *= sustenance.amount; + for include in includes.iter_mut() { + match include { + IngestionKind::Substance(ingestion) => { + ingestion.dose = Dose::new_precise(sustenance.amount) * ingestion.dose.clone(); + } + IngestionKind::Sustenance(ingestion) => { + ingestion.amount *= sustenance.amount; - calculate_sustenance_ingestion_amounts(ingestion) + if recurse { + calculate_ingestion_amounts(include, false); + } + } + IngestionKind::Hydration(hydration) => hydration.amount_ml *= sustenance.amount, + _ => {} } - IngestionKind::Hydration(hydration) => hydration.amount_ml *= sustenance.amount, - _ => {} } } } @@ -41,6 +45,8 @@ pub fn calculate_sustenance_contents(root: SustenanceIngestion) -> CalculatedVal while !ingestions.is_empty() { let current_ingestions = ingestions.clone(); + ingestions.clear(); + for ingestion in current_ingestions { match ingestion { IngestionKind::Unknown => {} @@ -68,6 +74,9 @@ pub fn calculate_sustenance_contents(root: SustenanceIngestion) -> CalculatedVal IngestionKind::Sustenance(sustenance) => { let amount = sustenance.amount; let mut sustenance = sustenance.sustenance.clone().unwrap(); + if let Some(nutrients) = sustenance.nutrients { + values.nutrients.kcal += nutrients.kcal * amount; + } for include in sustenance.includes.iter_mut() { match include { diff --git a/journal/src/types/nutrients.rs b/journal/src/types/nutrients.rs index 7a7acee..d38888b 100644 --- a/journal/src/types/nutrients.rs +++ b/journal/src/types/nutrients.rs @@ -1,5 +1,5 @@ #[derive(PartialEq, Default, Debug, Clone)] #[derive(serde::Serialize, serde::Deserialize)] pub struct Nutrients { - pub kcal: u64, + pub kcal: f64, } diff --git a/journal_cli/src/commands/print_session.rs b/journal_cli/src/commands/print_session.rs index 860012f..7194359 100644 --- a/journal_cli/src/commands/print_session.rs +++ b/journal_cli/src/commands/print_session.rs @@ -15,6 +15,9 @@ pub struct PrintSessionArgs { #[arg(long, value_delimiter = ',')] pub consumer_filter: Option>, + + #[arg(long)] + pub tree: bool, } pub fn parse_consumer_filter(consumer_filter: Option>) -> Option> { @@ -48,7 +51,7 @@ pub fn print_session(args: &PrintSessionArgs) -> Result<(), Box 0 { - print!("- "); + print!("{}- ", " ".repeat(depth as usize)); + } + if depth == 0 { + calculate_ingestion_amounts(&mut ingestion.kind, true); } - match &ingestion.kind { + match ingestion.kind.clone() { IngestionKind::Unknown => {} IngestionKind::Substance(substance) => { - print_substance_ingestion(journal, ingestion, substance); + print_substance_ingestion(journal, &ingestion, &substance); } IngestionKind::Sustenance(sustenance) => { - let mut sustenance = sustenance.clone(); - calculate_sustenance_ingestion_amounts(&mut sustenance); + print_sustenance_ingestion(journal, &ingestion, &sustenance); - print_sustenance_ingestion(journal, ingestion, &sustenance); + if depth == 0 { + depth += 2; - let includes = sustenance.sustenance.unwrap().includes; + let values = calculate_sustenance_contents(sustenance.clone()); - for include in includes { - print_ingestion( - journal, - &Ingestion { - kind: include, - ..ingestion.clone() - }, - depth + 1, - ) + print!("{}", " ".repeat(depth as usize)); + println!("Nutrients:"); + print!("{}", " ".repeat((depth + 1) as usize)); + println!("kcal: {}", values.nutrients.kcal); + + print!("{}", " ".repeat(depth as usize)); + println!("Includes:"); + } + + if tree { + let includes = sustenance.sustenance.unwrap().includes; + + for include in includes { + ingestion.kind = include; + + print_ingestion(journal, ingestion.clone(), depth + 1, tree) + } } } IngestionKind::Hydration(hydration) => { println!( "Hydration|{}|{}|{}", hydration, - ingestion.consumer, - format_ingestion_time(ingestion) + ingestion.consumer.clone(), + format_ingestion_time(&ingestion) ) } } @@ -94,22 +107,21 @@ pub fn print_ingestion_log( journal: &JournalType, session: &Session, consumer_filter: Option<&Vec>, + tree: bool, ) { - let mut ingestions = journal + let ingestions = journal .get_session_ingestions(session.id) .expect("could not find ingestions for session"); - for ingestion in ingestions.iter_mut() { + for mut ingestion in ingestions.into_iter() { if let Some(consumer_filter) = consumer_filter { if !consumer_filter.contains(&ingestion.consumer) { continue; } } - ingestion.kind = journal - .resolve_ingestion_kind(ingestion.kind.clone()) - .unwrap(); + ingestion.kind = journal.resolve_ingestion_kind(ingestion.kind).unwrap(); - print_ingestion(journal, ingestion, 0) + print_ingestion(journal, ingestion, 0, tree) } }