1use std::{fs::OpenOptions, sync::Arc};
2
3use anyhow::anyhow;
4use chrono::{Datelike, Local};
5use gpui::{action, keymap::Binding, MutableAppContext};
6use util::TryFutureExt as _;
7use workspace::AppState;
8
9action!(NewJournalEntry);
10
11pub fn init(app_state: Arc<AppState>, cx: &mut MutableAppContext) {
12 log::info!("JOURNAL INIT");
13 cx.add_bindings(vec![Binding::new("ctrl-alt-cmd-j", NewJournalEntry, None)]);
14
15 let mut counter = 0;
16 cx.add_global_action(move |_: &NewJournalEntry, cx| {
17 log::info!("NEW JOURNAL ENTRY ACTION");
18 counter += 1;
19 if counter == 2 {
20 log::info!("called twice?");
21 }
22 new_journal_entry(app_state.clone(), cx)
23 });
24}
25
26pub fn new_journal_entry(app_state: Arc<AppState>, cx: &mut MutableAppContext) {
27 log::info!("NEW JOURNAL ENTRY");
28 let paths = cx.background().spawn(async move {
29 let now = Local::now();
30 let home_dir = dirs::home_dir().ok_or_else(|| anyhow!("can't determine home directory"))?;
31 let journal_dir = home_dir.join("journal");
32 let month_dir = journal_dir
33 .join(now.year().to_string())
34 .join(now.month().to_string());
35 let entry_path = month_dir.join(format!("{}.md", now.day()));
36
37 std::fs::create_dir_all(dbg!(month_dir))?;
38 OpenOptions::new()
39 .create(true)
40 .write(true)
41 .open(dbg!(&entry_path))?;
42
43 Ok::<_, anyhow::Error>((journal_dir, entry_path))
44 });
45
46 cx.spawn(|mut cx| {
47 async move {
48 let (journal_dir, entry_path) = paths.await?;
49 let workspace = cx
50 .update(|cx| workspace::open_paths(&[journal_dir], &app_state, cx))
51 .await;
52
53 workspace
54 .update(&mut cx, |workspace, cx| {
55 workspace.open_paths(&[entry_path], cx)
56 })
57 .await;
58
59 dbg!(workspace);
60 Ok(())
61 }
62 .log_err()
63 })
64 .detach();
65}