Insert a time heading when creating a journal entry

This commit is contained in:
Nathan Sobo 2021-12-18 10:38:54 -07:00
parent 9e8ef31452
commit e4f18947de
6 changed files with 70 additions and 39 deletions

2
Cargo.lock generated
View File

@ -2501,9 +2501,9 @@ dependencies = [
name = "journal" name = "journal"
version = "0.1.0" version = "0.1.0"
dependencies = [ dependencies = [
"anyhow",
"chrono", "chrono",
"dirs 4.0.0", "dirs 4.0.0",
"editor",
"gpui", "gpui",
"log", "log",
"util", "util",

View File

@ -1251,7 +1251,7 @@ impl Editor {
} }
} }
fn insert(&mut self, text: &str, cx: &mut ViewContext<Self>) { pub fn insert(&mut self, text: &str, cx: &mut ViewContext<Self>) {
self.start_transaction(cx); self.start_transaction(cx);
let old_selections = self.selections::<usize>(cx).collect::<SmallVec<[_; 32]>>(); let old_selections = self.selections::<usize>(cx).collect::<SmallVec<[_; 32]>>();
let mut new_selections = Vec::new(); let mut new_selections = Vec::new();

View File

@ -7,10 +7,10 @@ edition = "2021"
path = "src/journal.rs" path = "src/journal.rs"
[dependencies] [dependencies]
editor = { path = "../editor" }
gpui = { path = "../gpui" } gpui = { path = "../gpui" }
util = { path = "../util" } util = { path = "../util" }
workspace = { path = "../workspace" } workspace = { path = "../workspace" }
anyhow = "1.0"
chrono = "0.4" chrono = "0.4"
dirs = "4.0" dirs = "4.0"
log = "0.4" log = "0.4"

View File

@ -1,8 +1,7 @@
use std::{fs::OpenOptions, sync::Arc}; use chrono::{Datelike, Local, Timelike};
use editor::{Autoscroll, Editor};
use anyhow::anyhow;
use chrono::{Datelike, Local};
use gpui::{action, keymap::Binding, MutableAppContext}; use gpui::{action, keymap::Binding, MutableAppContext};
use std::{fs::OpenOptions, sync::Arc};
use util::TryFutureExt as _; use util::TryFutureExt as _;
use workspace::AppState; use workspace::AppState;
@ -14,27 +13,37 @@ pub fn init(app_state: Arc<AppState>, cx: &mut MutableAppContext) {
} }
pub fn new_journal_entry(app_state: Arc<AppState>, cx: &mut MutableAppContext) { pub fn new_journal_entry(app_state: Arc<AppState>, cx: &mut MutableAppContext) {
let paths = cx.background().spawn(async move {
let now = Local::now(); let now = Local::now();
let home_dir = dirs::home_dir().ok_or_else(|| anyhow!("can't determine home directory"))?; let home_dir = match dirs::home_dir() {
Some(home_dir) => home_dir,
None => {
log::error!("can't determine home directory");
return;
}
};
let journal_dir = home_dir.join("journal"); let journal_dir = home_dir.join("journal");
let month_dir = journal_dir let month_dir = journal_dir
.join(now.year().to_string()) .join(now.year().to_string())
.join(now.month().to_string()); .join(now.month().to_string());
let entry_path = month_dir.join(format!("{}.md", now.day())); let entry_path = month_dir.join(format!("{}.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!("# {}:{} {}\n\n", hour, now.minute(), am_or_pm);
std::fs::create_dir_all(dbg!(month_dir))?; let create_entry = cx.background().spawn(async move {
std::fs::create_dir_all(month_dir)?;
OpenOptions::new() OpenOptions::new()
.create(true) .create(true)
.write(true) .write(true)
.open(dbg!(&entry_path))?; .open(&entry_path)?;
Ok::<_, std::io::Error>((journal_dir, entry_path))
Ok::<_, anyhow::Error>((journal_dir, entry_path))
}); });
cx.spawn(|mut cx| { cx.spawn(|mut cx| {
async move { async move {
let (journal_dir, entry_path) = paths.await?; let (journal_dir, entry_path) = create_entry.await?;
let workspace = cx let workspace = cx
.update(|cx| workspace::open_paths(&[journal_dir], &app_state, cx)) .update(|cx| workspace::open_paths(&[journal_dir], &app_state, cx))
.await; .await;
@ -46,7 +55,16 @@ pub fn new_journal_entry(app_state: Arc<AppState>, cx: &mut MutableAppContext) {
.await; .await;
if let Some(Some(Ok(item))) = opened.first() { if let Some(Some(Ok(item))) = opened.first() {
log::info!("opened an item!"); if let Some(editor) = item.to_any().downcast::<Editor>() {
editor.update(&mut cx, |editor, cx| {
let len = editor.buffer().read(cx).len();
editor.select_ranges([len..len], Some(Autoscroll::Center), cx);
if len > 0 {
editor.insert("\n\n", cx);
}
editor.insert(&entry_heading, cx);
});
}
} }
Ok(()) Ok(())

View File

@ -107,15 +107,15 @@ impl Pane {
&mut self, &mut self,
project_path: ProjectPath, project_path: ProjectPath,
cx: &mut ViewContext<Self>, cx: &mut ViewContext<Self>,
) -> bool { ) -> Option<Box<dyn ItemViewHandle>> {
if let Some(index) = self.items.iter().position(|item| { if let Some(index) = self.items.iter().position(|item| {
item.project_path(cx.as_ref()) item.project_path(cx.as_ref())
.map_or(false, |item_path| item_path == project_path) .map_or(false, |item_path| item_path == project_path)
}) { }) {
self.activate_item(index, cx); self.activate_item(index, cx);
true self.items.get(index).map(|handle| handle.boxed_clone())
} else { } else {
false None
} }
} }

View File

@ -498,7 +498,7 @@ impl Workspace {
&mut self, &mut self,
abs_paths: &[PathBuf], abs_paths: &[PathBuf],
cx: &mut ViewContext<Self>, cx: &mut ViewContext<Self>,
) -> Task<Vec<Option<Result<Box<dyn ItemHandle>, Arc<anyhow::Error>>>>> { ) -> Task<Vec<Option<Result<Box<dyn ItemViewHandle>, Arc<anyhow::Error>>>>> {
let entries = abs_paths let entries = abs_paths
.iter() .iter()
.cloned() .cloned()
@ -625,10 +625,12 @@ impl Workspace {
&mut self, &mut self,
project_path: ProjectPath, project_path: ProjectPath,
cx: &mut ViewContext<Self>, cx: &mut ViewContext<Self>,
) -> Option<Task<Result<Box<dyn ItemHandle>, Arc<anyhow::Error>>>> { ) -> Option<Task<Result<Box<dyn ItemViewHandle>, Arc<anyhow::Error>>>> {
let pane = self.active_pane().clone(); let pane = self.active_pane().clone();
if self.activate_or_open_existing_entry(project_path.clone(), &pane, cx) { if let Some(existing_item) =
return None; self.activate_or_open_existing_entry(project_path.clone(), &pane, cx)
{
return Some(cx.foreground().spawn(async move { Ok(existing_item) }));
} }
let worktree = match self let worktree = match self
@ -687,10 +689,13 @@ impl Workspace {
// By the time loading finishes, the entry could have been already added // By the time loading finishes, the entry could have been already added
// to the pane. If it was, we activate it, otherwise we'll store the // to the pane. If it was, we activate it, otherwise we'll store the
// item and add a new view for it. // item and add a new view for it.
if !this.activate_or_open_existing_entry(project_path, &pane, cx) { if let Some(existing) =
this.add_item(item.boxed_clone(), cx); this.activate_or_open_existing_entry(project_path, &pane, cx)
{
Ok(existing)
} else {
Ok(this.add_item(item.boxed_clone(), cx))
} }
Ok(item)
}) })
})) }))
} }
@ -700,11 +705,13 @@ impl Workspace {
project_path: ProjectPath, project_path: ProjectPath,
pane: &ViewHandle<Pane>, pane: &ViewHandle<Pane>,
cx: &mut ViewContext<Self>, cx: &mut ViewContext<Self>,
) -> bool { ) -> Option<Box<dyn ItemViewHandle>> {
// If the pane contains a view for this file, then activate // If the pane contains a view for this file, then activate
// that item view. // that item view.
if pane.update(cx, |pane, cx| pane.activate_entry(project_path.clone(), cx)) { if let Some(existing_item_view) =
return true; pane.update(cx, |pane, cx| pane.activate_entry(project_path.clone(), cx))
{
return Some(existing_item_view);
} }
// Otherwise, if this file is already open somewhere in the workspace, // Otherwise, if this file is already open somewhere in the workspace,
@ -727,10 +734,10 @@ impl Workspace {
} }
}); });
if let Some(view) = view_for_existing_item { if let Some(view) = view_for_existing_item {
pane.add_item_view(view, cx.as_mut()); pane.add_item_view(view.boxed_clone(), cx.as_mut());
true Some(view)
} else { } else {
false None
} }
} }
@ -875,13 +882,19 @@ impl Workspace {
pane pane
} }
pub fn add_item<T>(&mut self, item_handle: T, cx: &mut ViewContext<Self>) pub fn add_item<T>(
&mut self,
item_handle: T,
cx: &mut ViewContext<Self>,
) -> Box<dyn ItemViewHandle>
where where
T: ItemHandle, T: ItemHandle,
{ {
let view = item_handle.add_view(cx.window_id(), self.settings.clone(), cx); let view = item_handle.add_view(cx.window_id(), self.settings.clone(), cx);
self.items.push(item_handle.downgrade()); self.items.push(item_handle.downgrade());
self.active_pane().add_item_view(view, cx.as_mut()); self.active_pane()
.add_item_view(view.boxed_clone(), cx.as_mut());
view
} }
fn activate_pane(&mut self, pane: ViewHandle<Pane>, cx: &mut ViewContext<Self>) { fn activate_pane(&mut self, pane: ViewHandle<Pane>, cx: &mut ViewContext<Self>) {