journal.rs

  1use chrono::{Datelike, Local, NaiveTime, Timelike};
  2use editor::{scroll::autoscroll::Autoscroll, Editor};
  3use gpui::{actions, AppContext};
  4use settings::{HourFormat, Settings};
  5use std::{
  6    fs::OpenOptions,
  7    path::{Path, PathBuf},
  8    sync::Arc,
  9};
 10use workspace::AppState;
 11
 12actions!(journal, [NewJournalEntry]);
 13
 14pub fn init(app_state: Arc<AppState>, cx: &mut AppContext) {
 15    cx.add_global_action(move |_: &NewJournalEntry, cx| new_journal_entry(app_state.clone(), cx));
 16}
 17
 18pub fn new_journal_entry(app_state: Arc<AppState>, cx: &mut AppContext) {
 19    let settings = cx.global::<Settings>();
 20    let journal_dir = match journal_dir(&settings) {
 21        Some(journal_dir) => journal_dir,
 22        None => {
 23            log::error!("Can't determine journal directory");
 24            return;
 25        }
 26    };
 27
 28    let now = Local::now();
 29    let month_dir = journal_dir
 30        .join(format!("{:02}", now.year()))
 31        .join(format!("{:02}", now.month()));
 32    let entry_path = month_dir.join(format!("{:02}.md", now.day()));
 33    let now = now.time();
 34    let hour_format = &settings.journal_overrides.hour_format;
 35    let entry_heading = heading_entry(now, &hour_format);
 36
 37    let create_entry = cx.background().spawn(async move {
 38        std::fs::create_dir_all(month_dir)?;
 39        OpenOptions::new()
 40            .create(true)
 41            .write(true)
 42            .open(&entry_path)?;
 43        Ok::<_, std::io::Error>((journal_dir, entry_path))
 44    });
 45
 46    cx.spawn(|mut cx| async move {
 47        let (journal_dir, entry_path) = create_entry.await?;
 48        let (workspace, _) = cx
 49            .update(|cx| workspace::open_paths(&[journal_dir], &app_state, None, cx))
 50            .await?;
 51
 52        let opened = workspace
 53            .update(&mut cx, |workspace, cx| {
 54                workspace.open_paths(vec![entry_path], true, cx)
 55            })?
 56            .await;
 57
 58        if let Some(Some(Ok(item))) = opened.first() {
 59            if let Some(editor) = item.downcast::<Editor>().map(|editor| editor.downgrade()) {
 60                editor.update(&mut cx, |editor, cx| {
 61                    let len = editor.buffer().read(cx).len(cx);
 62                    editor.change_selections(Some(Autoscroll::center()), cx, |s| {
 63                        s.select_ranges([len..len])
 64                    });
 65                    if len > 0 {
 66                        editor.insert("\n\n", cx);
 67                    }
 68                    editor.insert(&entry_heading, cx);
 69                    editor.insert("\n\n", cx);
 70                })?;
 71            }
 72        }
 73
 74        anyhow::Ok(())
 75    })
 76    .detach_and_log_err(cx);
 77}
 78
 79fn journal_dir(settings: &Settings) -> Option<PathBuf> {
 80    let journal_dir = settings
 81        .journal_overrides
 82        .path
 83        .as_ref()
 84        .unwrap_or(settings.journal_defaults.path.as_ref()?);
 85
 86    let expanded_journal_dir = shellexpand::full(&journal_dir) //TODO handle this better
 87        .ok()
 88        .map(|dir| Path::new(&dir.to_string()).to_path_buf().join("journal"));
 89
 90    return expanded_journal_dir;
 91}
 92
 93fn heading_entry(now: NaiveTime, hour_format: &Option<HourFormat>) -> String {
 94    match hour_format {
 95        Some(HourFormat::Hour24) => {
 96            let hour = now.hour();
 97            format!("# {}:{:02}", hour, now.minute())
 98        }
 99        _ => {
100            let (pm, hour) = now.hour12();
101            let am_or_pm = if pm { "PM" } else { "AM" };
102            format!("# {}:{:02} {}", hour, now.minute(), am_or_pm)
103        }
104    }
105}
106
107#[cfg(test)]
108mod tests {
109    mod heading_entry_tests {
110        use super::super::*;
111
112        #[test]
113        fn test_heading_entry_defaults_to_hour_12() {
114            let naive_time = NaiveTime::from_hms_milli_opt(15, 0, 0, 0).unwrap();
115            let actual_heading_entry = heading_entry(naive_time, &None);
116            let expected_heading_entry = "# 3:00 PM";
117
118            assert_eq!(actual_heading_entry, expected_heading_entry);
119        }
120
121        #[test]
122        fn test_heading_entry_is_hour_12() {
123            let naive_time = NaiveTime::from_hms_milli_opt(15, 0, 0, 0).unwrap();
124            let actual_heading_entry = heading_entry(naive_time, &Some(HourFormat::Hour12));
125            let expected_heading_entry = "# 3:00 PM";
126
127            assert_eq!(actual_heading_entry, expected_heading_entry);
128        }
129
130        #[test]
131        fn test_heading_entry_is_hour_24() {
132            let naive_time = NaiveTime::from_hms_milli_opt(15, 0, 0, 0).unwrap();
133            let actual_heading_entry = heading_entry(naive_time, &Some(HourFormat::Hour24));
134            let expected_heading_entry = "# 15:00";
135
136            assert_eq!(actual_heading_entry, expected_heading_entry);
137        }
138    }
139}