diff --git a/Cargo.lock b/Cargo.lock index 363ee93c14..28fc8fcb61 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2764,6 +2764,7 @@ dependencies = [ "editor", "gpui", "log", + "settings", "util", "workspace", ] diff --git a/assets/settings/default.json b/assets/settings/default.json index a12cf44d94..25117521bf 100644 --- a/assets/settings/default.json +++ b/assets/settings/default.json @@ -74,6 +74,13 @@ "hard_tabs": false, // How many columns a tab should occupy. "tab_size": 4, + // TODO + "journal": { + // TODO + "journal_directory": "always_home", + // TODO + "hour_format": "hour12" + }, // Settings specific to the terminal "terminal": { // What shell to use when opening a terminal. May take 3 values: diff --git a/crates/journal/Cargo.toml b/crates/journal/Cargo.toml index 94dcf8e407..091aadd12e 100644 --- a/crates/journal/Cargo.toml +++ b/crates/journal/Cargo.toml @@ -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" } diff --git a/crates/journal/src/journal.rs b/crates/journal/src/journal.rs index 8135950e50..3a44345b70 100644 --- a/crates/journal/src/journal.rs +++ b/crates/journal/src/journal.rs @@ -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, cx: &mut MutableAppContext) { } pub fn new_journal_entry(app_state: Arc, cx: &mut MutableAppContext) { - let now = Local::now(); - let home_dir = match dirs::home_dir() { - Some(home_dir) => home_dir, + let settings = cx.global::(); + 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, 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, cx: &mut MutableAppContext) { }) .detach(); } + +fn get_journal_dir(a: &Option) -> Option { + 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) -> 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); + } + } +} diff --git a/crates/settings/src/settings.rs b/crates/settings/src/settings.rs index e346ff60e6..5d4195d3b2 100644 --- a/crates/settings/src/settings.rs +++ b/crates/settings/src/settings.rs @@ -32,6 +32,8 @@ pub struct Settings { pub default_dock_anchor: DockAnchor, pub editor_defaults: EditorSettings, pub editor_overrides: EditorSettings, + pub journal_defaults: JournalSettings, + pub journal_overrides: JournalSettings, pub terminal_defaults: TerminalSettings, pub terminal_overrides: TerminalSettings, pub language_defaults: HashMap, EditorSettings>, @@ -101,6 +103,26 @@ pub enum Autosave { OnWindowChange, } +#[derive(Clone, Debug, Default, Deserialize, JsonSchema)] +pub struct JournalSettings { + pub journal_directory: Option, + pub hour_format: Option, +} + +#[derive(Clone, Debug, Deserialize, JsonSchema)] +#[serde(rename_all = "snake_case")] +pub enum JournalDirectory { + AlwaysHome, + Always { directory: String }, +} + +#[derive(Clone, Debug, Deserialize, JsonSchema)] +#[serde(rename_all = "snake_case")] +pub enum HourFormat { + Hour12, + Hour24, +} + #[derive(Clone, Debug, Default, Deserialize, JsonSchema)] pub struct TerminalSettings { pub shell: Option, @@ -194,6 +216,8 @@ pub struct SettingsFileContent { #[serde(flatten)] pub editor: EditorSettings, #[serde(default)] + pub journal: JournalSettings, + #[serde(default)] pub terminal: TerminalSettings, #[serde(default)] #[serde(alias = "language_overrides")] @@ -252,6 +276,8 @@ impl Settings { enable_language_server: required(defaults.editor.enable_language_server), }, editor_overrides: Default::default(), + journal_defaults: Default::default(), + journal_overrides: Default::default(), terminal_defaults: Default::default(), terminal_overrides: Default::default(), language_defaults: defaults.languages, @@ -303,6 +329,7 @@ impl Settings { } self.editor_overrides = data.editor; + self.journal_overrides = data.journal; self.terminal_defaults.font_size = data.terminal.font_size; self.terminal_overrides = data.terminal; self.language_overrides = data.languages; @@ -380,6 +407,8 @@ impl Settings { enable_language_server: Some(true), }, editor_overrides: Default::default(), + journal_defaults: Default::default(), + journal_overrides: Default::default(), terminal_defaults: Default::default(), terminal_overrides: Default::default(), language_defaults: Default::default(),