diff options
Diffstat (limited to 'src/journal.rs')
-rw-r--r-- | src/journal.rs | 67 |
1 files changed, 47 insertions, 20 deletions
diff --git a/src/journal.rs b/src/journal.rs index 911f9a3..c3bb1eb 100644 --- a/src/journal.rs +++ b/src/journal.rs @@ -1,6 +1,5 @@ use crate::error::JournalError; use chrono::Local; -use log::debug; use std::path::{Path, PathBuf}; use std::process::Command; @@ -8,31 +7,32 @@ const MAX_DRAFT_COUNT: usize = 1000; pub struct Journal { dirname: PathBuf, + entries: PathBuf, } impl Journal { - pub fn is_journal(path: &Path) -> bool { - if let Ok(meta) = std::fs::symlink_metadata(path) { - meta.is_dir() - } else { - false - } + pub fn is_journal(path: &Path, entries: &Path) -> bool { + is_dir(path) && is_dir(entries) } - pub fn init(path: &Path) -> Result<Self, JournalError> { + pub fn init(path: &Path, entries: &Path) -> Result<Self, JournalError> { std::fs::create_dir(path) .map_err(|err| JournalError::CreateDirectory(path.to_path_buf(), err))?; + std::fs::create_dir(entries) + .map_err(|err| JournalError::CreateDirectory(entries.to_path_buf(), err))?; Ok(Self { dirname: path.to_path_buf(), + entries: entries.to_path_buf(), }) } - pub fn new(path: &Path) -> Result<Self, JournalError> { - let dirname = path.to_path_buf(); - if dirname.exists() { - Ok(Self { dirname }) + pub fn new(path: &Path, entries: &Path) -> Result<Self, JournalError> { + if Self::is_journal(path, entries) { + let dirname = path.to_path_buf(); + let entries = entries.to_path_buf(); + Ok(Self { dirname, entries }) } else { - Err(JournalError::NotAJournal(dirname.display().to_string())) + Err(JournalError::NotAJournal(path.display().to_string())) } } @@ -45,10 +45,10 @@ impl Journal { } fn entries(&self) -> PathBuf { - self.dirname().join("entries") + self.entries.clone() } - pub fn new_draft(&self, title: &str, _editor: &str) -> anyhow::Result<()> { + pub fn new_draft(&self, title: &str, editor: &str) -> anyhow::Result<()> { let drafts = self.drafts(); if !drafts.exists() { std::fs::create_dir(&drafts)?; @@ -56,7 +56,8 @@ impl Journal { let pathname = self.pick_file_id(&drafts)?; let text = format!(r#"[[!meta title="{}"]]"#, title); - std::fs::write(pathname, format!("{}\n\n", text))?; + std::fs::write(&pathname, format!("{}\n\n", text))?; + self.edit(editor, &pathname)?; Ok(()) } @@ -84,11 +85,28 @@ impl Journal { } } + fn edit(&self, editor: &str, filename: &Path) -> Result<(), JournalError> { + if editor == "none" { + return Ok(()); + } + match Command::new(editor).arg(filename).output() { + Err(err) => Err(JournalError::SpawnEditor(filename.to_path_buf(), err)), + Ok(output) => { + if output.status.success() { + Ok(()) + } else { + let stderr = String::from_utf8_lossy(&output.stderr); + Err(JournalError::EditorFailed( + filename.to_path_buf(), + stderr.into_owned(), + )) + } + } + } + } + pub fn edit_draft(&self, editor: &str, filename: &Path) -> anyhow::Result<()> { - debug!("edit_draft: editor={:?}", editor); - debug!("edit_draft: filename={:?}", filename); - Command::new(editor).arg(filename).status()?; - debug!("edit_draft: editor finished"); + self.edit(editor, filename)?; Ok(()) } @@ -101,8 +119,17 @@ impl Journal { let subdir = entries.join(Local::today().format("%Y/%m/%d").to_string()); std::fs::create_dir_all(&subdir)?; + let basename = PathBuf::from(format!("{}.mdwn", basename)); let entry = subdir.join(basename); std::fs::rename(filename, entry)?; Ok(()) } } + +fn is_dir(path: &Path) -> bool { + if let Ok(meta) = std::fs::symlink_metadata(path) { + meta.is_dir() + } else { + false + } +} |