Initial work to add settings to journal feature

This commit is contained in:
Joseph T Lyons 2022-09-28 16:25:37 -04:00
parent 5d8fe33bd2
commit 773423fcf4
5 changed files with 109 additions and 9 deletions

View file

@ -15,3 +15,4 @@ workspace = { path = "../workspace" }
chrono = "0.4"
dirs = "4.0"
log = { version = "0.4.16", features = ["kv_unstable_serde"] }
settings = { path = "../settings" }

View file

@ -1,7 +1,8 @@
use chrono::{Datelike, Local, Timelike};
use chrono::{Datelike, Local, NaiveTime, Timelike};
use editor::{Autoscroll, Editor};
use gpui::{actions, MutableAppContext};
use std::{fs::OpenOptions, sync::Arc};
use settings::{HourFormat, JournalDirectory, Settings};
use std::{fs::OpenOptions, path::PathBuf, str::FromStr, sync::Arc};
use util::TryFutureExt as _;
use workspace::AppState;
@ -12,24 +13,23 @@ pub fn init(app_state: Arc<AppState>, cx: &mut MutableAppContext) {
}
pub fn new_journal_entry(app_state: Arc<AppState>, cx: &mut MutableAppContext) {
let now = Local::now();
let home_dir = match dirs::home_dir() {
Some(home_dir) => home_dir,
let settings = cx.global::<Settings>();
let journal_dir = match get_journal_dir(&settings.journal_overrides.journal_directory) {
Some(journal_dir) => journal_dir,
None => {
log::error!("can't determine home directory");
return;
}
};
let journal_dir = home_dir.join("journal");
let now = Local::now();
let month_dir = journal_dir
.join(format!("{:02}", now.year()))
.join(format!("{:02}", now.month()));
let entry_path = month_dir.join(format!("{:02}.md", now.day()));
let now = now.time();
let (pm, hour) = now.hour12();
let am_or_pm = if pm { "PM" } else { "AM" };
let entry_heading = format!("# {}:{:02} {}\n\n", hour, now.minute(), am_or_pm);
let hour_format = &settings.journal_overrides.hour_format;
let entry_heading = get_heading_entry(now, &hour_format);
let create_entry = cx.background().spawn(async move {
std::fs::create_dir_all(month_dir)?;
@ -64,6 +64,7 @@ pub fn new_journal_entry(app_state: Arc<AppState>, cx: &mut MutableAppContext) {
editor.insert("\n\n", cx);
}
editor.insert(&entry_heading, cx);
editor.insert("\n\n", cx);
});
}
}
@ -74,3 +75,64 @@ pub fn new_journal_entry(app_state: Arc<AppState>, cx: &mut MutableAppContext) {
})
.detach();
}
fn get_journal_dir(a: &Option<JournalDirectory>) -> Option<PathBuf> {
let journal_default_dir = dirs::home_dir()?.join("journal");
let journal_dir = match a {
Some(JournalDirectory::Always { directory }) => {
PathBuf::from_str(&directory).unwrap_or(journal_default_dir)
}
_ => journal_default_dir,
};
Some(journal_dir)
}
fn get_heading_entry(now: NaiveTime, hour_format: &Option<HourFormat>) -> String {
match hour_format {
Some(HourFormat::Hour24) => {
let hour = now.hour();
format!("# {}:{:02}", hour, now.minute())
}
_ => {
let (pm, hour) = now.hour12();
let am_or_pm = if pm { "PM" } else { "AM" };
format!("# {}:{:02} {}", hour, now.minute(), am_or_pm)
}
}
}
#[cfg(test)]
mod tests {
mod heading_entry_tests {
use super::super::*;
#[test]
fn test_heading_entry_defaults_to_hour_12() {
let naive_time = NaiveTime::from_hms_milli(15, 0, 0, 0);
let actual_heading_entry = get_heading_entry(naive_time, &None);
let expected_heading_entry = "# 3:00 PM";
assert_eq!(actual_heading_entry, expected_heading_entry);
}
#[test]
fn test_heading_entry_is_hour_12() {
let naive_time = NaiveTime::from_hms_milli(15, 0, 0, 0);
let actual_heading_entry = get_heading_entry(naive_time, &Some(HourFormat::Hour12));
let expected_heading_entry = "# 3:00 PM";
assert_eq!(actual_heading_entry, expected_heading_entry);
}
#[test]
fn test_heading_entry_is_hour_24() {
let naive_time = NaiveTime::from_hms_milli(15, 0, 0, 0);
let actual_heading_entry = get_heading_entry(naive_time, &Some(HourFormat::Hour24));
let expected_heading_entry = "# 15:00";
assert_eq!(actual_heading_entry, expected_heading_entry);
}
}
}