Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
518 changes: 334 additions & 184 deletions Cargo.lock

Large diffs are not rendered by default.

15 changes: 8 additions & 7 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,18 +1,19 @@
[package]
name = "mind-corner"
version = "0.6.2"
version = "1.0.0"
edition = "2021"
description = "Simple CLI app for mental health activities."
description = "A CLI app for mental health activities."
authors = ["Mateusz Kuniczuk <mateusz.kuniczuk@gmail.com>"]
license = "MIT"

[dependencies]
chrono = "0.4.38"
chrono = "0.4.40"
inquire = { version = "0.7.5", features = ["editor"] }
csv = "1.3.0"
csv = "1.3.1"
serde = { version = "1.0.205", features = ["derive"] }
log = "0.4.22"
env_logger = "0.11.5"
log = "0.4.27"
env_logger = "0.11.8"
polars = { version = "0.46.0", features = ["lazy", "performant"] }
rand = "0.9.0"
rstest = "0.24.0"
rstest = "0.25.0"
indicatif = "0.17.11"
13 changes: 7 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,13 @@

A CLI app to help you take care of your mental health.

## Modules (WIP)
## Modules

Following modules are supported:
- Meditation Timer
- Gratitiude Journal
- Mood Tracker
- Breathing Guide (not yet)

Additionally, you can take a look at your data through Data Analysis module (WIP).
- Meditation Timer
- Gratitiude Journal
- Mood Tracker
- Breathing Guide

Additionally, you can take a look at your data through Data Analysis module (not finished yet).
102 changes: 101 additions & 1 deletion src/app_modules/breathing_guide.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,103 @@
use crate::utilities::print_in_place;
use indicatif::ProgressBar;
use inquire::Select;
use std::io::{stdout, Stdout};
use std::{fmt, thread, time};

const SECONDS_OF_SLEEP_INTERVAL: u64 = 1;
const PROGRESS_BAR_INTERVAL: u64 = 1;
const TIME_TO_GET_READY: u8 = 3;

#[derive(Clone)]
struct BreathingPattern {
name: &'static str,
breathe_in_duration: u64,
hold_duration: u64,
breathe_out_duration: u64,
}

impl BreathingPattern {
fn run_breathing_guide(&self, breathing_indicator: &ProgressBar) {
breathe_in(breathing_indicator, self.breathe_in_duration);
hold(breathing_indicator, self.hold_duration);
breathe_out(breathing_indicator, self.breathe_out_duration);
}
}

impl fmt::Display for BreathingPattern {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{}", self.name)
}
}

const BREATHING_PATTERNS: &[BreathingPattern] = &[
BreathingPattern {
name: "4-7-8",
breathe_in_duration: 4,
hold_duration: 7,
breathe_out_duration: 8,
},
BreathingPattern {
name: "Box",
breathe_in_duration: 4,
hold_duration: 4,
breathe_out_duration: 4,
},
];

pub(crate) fn run_breathing_guide() {
unimplemented!("We will get to it soon, I promise!");
// TODO store data of type and duration, to csv, after refactor into common csv writing (#14)

let breathing_pattern_options: Vec<BreathingPattern> = BREATHING_PATTERNS.to_vec();

let breathing_pattern: BreathingPattern =
Select::new("Select breathing pattern.", breathing_pattern_options)
.prompt()
.expect("Failed to get breathing pattern");

let breathing_indicator = ProgressBar::new(4);
get_ready();
loop {
breathing_pattern.run_breathing_guide(&breathing_indicator);
}
}

fn get_ready() {
println!("Get comfortable.");
let stdout: Stdout = stdout();
for count_down in (0u8..=TIME_TO_GET_READY).rev() {
let countdown_text = format!("{}", count_down);
print_in_place(&stdout, countdown_text);
sleep_tick();
}
println!();
}

fn breathe_in(breathing_indicator: &ProgressBar, segment_duration: u64) {
breathing_indicator.println("Breathe in.");
for _ in 0..segment_duration {
breathing_indicator.inc(PROGRESS_BAR_INTERVAL);
sleep_tick();
}
}

fn hold(breathing_indicator: &ProgressBar, segment_duration: u64) {
breathing_indicator.println("Hold...");
for _ in 0..segment_duration {
sleep_tick();
}
}

fn breathe_out(breathing_indicator: &ProgressBar, segment_duration: u64) {
breathing_indicator.set_length(segment_duration);
breathing_indicator.set_position(segment_duration);
breathing_indicator.println("Breathe out.");
for _ in 0..segment_duration {
breathing_indicator.dec(PROGRESS_BAR_INTERVAL);
sleep_tick();
}
}

fn sleep_tick() {
thread::sleep(time::Duration::from_secs(SECONDS_OF_SLEEP_INTERVAL));
}
8 changes: 4 additions & 4 deletions src/app_modules/meditation_timer.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
use crate::data::meditation_timer::add_meditation_record;
use crate::utilities::print_in_place;
use inquire::validator::Validation;
use inquire::Text;
use log::{error, info};
use std::io::{stdout, Write};
use std::io::{stdout, Stdout};
use std::thread::sleep;
use std::time::Duration;

Expand Down Expand Up @@ -43,14 +44,13 @@ fn get_parsed_duration(duration: String) -> u32 {

fn start_timer(duration: u32) {
println!("Starting meditation timer.");
let mut standard_output = stdout();
let stdout: Stdout = stdout();

for seconds in 0..=duration {
let minutes = seconds / SECONDS_IN_MINUTE;
let seconds_in_minute = seconds % SECONDS_IN_MINUTE;

print!("\r{:02}:{:02}", minutes, seconds_in_minute);
standard_output.flush().expect("Failed to flush stdout.");
print_in_place(&stdout, format!("{:02}:{:02}", minutes, seconds_in_minute));
sleep(Duration::from_secs(1));
}

Expand Down
8 changes: 4 additions & 4 deletions src/app_modules/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ pub enum AppAction {

#[derive(Debug)]
enum AppModule {
Timer,
MeditationTimer,
MoodTracker,
GratitudeJournal,
BreathingGuide,
Expand All @@ -27,7 +27,7 @@ enum AppModule {
impl fmt::Display for AppModule {
fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
AppModule::Timer => write!(formatter, "Timer"),
AppModule::MeditationTimer => write!(formatter, "Meditation Timer"),
AppModule::MoodTracker => write!(formatter, "Mood Tracker"),
AppModule::GratitudeJournal => write!(formatter, "Gratitude Journal"),
AppModule::BreathingGuide => write!(formatter, "Breathing Guide"),
Expand All @@ -39,7 +39,7 @@ impl fmt::Display for AppModule {

pub fn select_module() -> AppAction {
let module_choices = vec![
AppModule::Timer,
AppModule::MeditationTimer,
AppModule::MoodTracker,
AppModule::GratitudeJournal,
AppModule::BreathingGuide,
Expand All @@ -51,7 +51,7 @@ pub fn select_module() -> AppAction {

match module_answer {
Ok(choice) => match choice {
AppModule::Timer => meditation_timer::run_meditation_timer(),
AppModule::MeditationTimer => meditation_timer::run_meditation_timer(),
AppModule::MoodTracker => mood_tracker::run_mood_tracker(),
AppModule::GratitudeJournal => gratitude_journal::run_gratitude_journal(),
AppModule::BreathingGuide => breathing_guide::run_breathing_guide(),
Expand Down
1 change: 0 additions & 1 deletion src/app_modules/mood_tracker.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@ pub(crate) fn run_mood_tracker() {
];

let user_mood: Mood = Select::new("How would you rate your mood?\n", mood_options)
// TODO report the newline issue with without_filtering() ? (#14)
.with_help_message("↑↓ to move, enter to select mood")
.with_starting_cursor(2)
.without_filtering()
Expand Down
6 changes: 6 additions & 0 deletions src/utilities.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use chrono::{DateTime, Local, TimeZone};
use std::fmt::Display;
use std::io::{Stdout, Write};

const DATE_TIME_FORMAT: &str = "%Y-%m-%d %H:%M:%S";
const DATE_FORMAT: &str = "%Y-%m-%d";
Expand All @@ -20,6 +21,11 @@ where
time.format(format).to_string()
}

pub(crate) fn print_in_place(mut standard_output: &Stdout, text: String) {
print!("\r{}", text);
standard_output.flush().expect("Failed to flush stdout.");
}

#[cfg(test)]
mod tests {
use crate::utilities::{get_formatted_datetime, DATE_FORMAT, DATE_TIME_FORMAT};
Expand Down