Change journal location setting name to "path" and default to ~

Joseph T Lyons created

Change summary

Cargo.lock                      |  1 +
assets/settings/default.json    |  8 ++++----
crates/journal/Cargo.toml       |  1 +
crates/journal/src/journal.rs   | 31 ++++++++++++++++++-------------
crates/settings/src/settings.rs | 22 +++++++++++++++-------
5 files changed, 39 insertions(+), 24 deletions(-)

Detailed changes

Cargo.lock 🔗

@@ -2765,6 +2765,7 @@ dependencies = [
  "gpui",
  "log",
  "settings",
+ "shellexpand",
  "util",
  "workspace",
 ]

assets/settings/default.json 🔗

@@ -76,10 +76,10 @@
     "tab_size": 4,
     // Settings specific to journaling
     "journal": {
-        // The directory in which the journal entries are created
-        "journal_directory": "always_home",
-        // What format to present the hours in
-        // May take 2 values: 
+        // The path of the directory where journal entries are stored
+        "path": "~",
+        // What format to display the hours in
+        // May take 2 values:
         // 1. hour12
         // 2. hour24
         "hour_format": "hour12"

crates/journal/Cargo.toml 🔗

@@ -16,3 +16,4 @@ chrono = "0.4"
 dirs = "4.0"
 log = { version = "0.4.16", features = ["kv_unstable_serde"] }
 settings = { path = "../settings" }
+shellexpand = "2.1.0"

crates/journal/src/journal.rs 🔗

@@ -1,8 +1,12 @@
 use chrono::{Datelike, Local, NaiveTime, Timelike};
 use editor::{Autoscroll, Editor};
 use gpui::{actions, MutableAppContext};
-use settings::{HourFormat, JournalDirectory, Settings};
-use std::{fs::OpenOptions, path::PathBuf, str::FromStr, sync::Arc};
+use settings::{HourFormat, Settings};
+use std::{
+    fs::OpenOptions,
+    path::{Path, PathBuf},
+    sync::Arc,
+};
 use util::TryFutureExt as _;
 use workspace::AppState;
 
@@ -14,10 +18,10 @@ pub fn init(app_state: Arc<AppState>, cx: &mut MutableAppContext) {
 
 pub fn new_journal_entry(app_state: Arc<AppState>, cx: &mut MutableAppContext) {
     let settings = cx.global::<Settings>();
-    let journal_dir = match journal_dir(&settings.journal_overrides.journal_directory) {
+    let journal_dir = match journal_dir(&settings) {
         Some(journal_dir) => journal_dir,
         None => {
-            log::error!("can't determine home directory");
+            log::error!("Can't determine journal directory");
             return;
         }
     };
@@ -76,17 +80,18 @@ pub fn new_journal_entry(app_state: Arc<AppState>, cx: &mut MutableAppContext) {
     .detach();
 }
 
-fn journal_dir(a: &Option<JournalDirectory>) -> Option<PathBuf> {
-    let journal_default_dir = dirs::home_dir()?.join("journal");
+fn journal_dir(settings: &Settings) -> Option<PathBuf> {
+    let journal_dir = settings
+        .journal_overrides
+        .path
+        .as_ref()
+        .unwrap_or(settings.journal_defaults.path.as_ref()?);
 
-    let journal_dir = match a {
-        Some(JournalDirectory::Always { directory }) => {
-            PathBuf::from_str(&directory).unwrap_or(journal_default_dir)
-        }
-        _ => journal_default_dir,
-    };
+    let expanded_journal_dir = shellexpand::full(&journal_dir) //TODO handle this better
+        .ok()
+        .map(|dir| Path::new(&dir.to_string()).to_path_buf().join("journal"));
 
-    Some(journal_dir)
+    return expanded_journal_dir;
 }
 
 fn heading_entry(now: NaiveTime, hour_format: &Option<HourFormat>) -> String {

crates/settings/src/settings.rs 🔗

@@ -103,17 +103,19 @@ pub enum Autosave {
     OnWindowChange,
 }
 
-#[derive(Clone, Debug, Default, Deserialize, JsonSchema)]
+#[derive(Clone, Debug, Deserialize, JsonSchema)]
 pub struct JournalSettings {
-    pub journal_directory: Option<JournalDirectory>,
+    pub path: Option<String>,
     pub hour_format: Option<HourFormat>,
 }
 
-#[derive(Clone, Debug, Deserialize, JsonSchema)]
-#[serde(rename_all = "snake_case")]
-pub enum JournalDirectory {
-    AlwaysHome,
-    Always { directory: String },
+impl Default for JournalSettings {
+    fn default() -> Self {
+        Self {
+            path: Some("~".into()),
+            hour_format: Some(Default::default()),
+        }
+    }
 }
 
 #[derive(Clone, Debug, Deserialize, JsonSchema)]
@@ -123,6 +125,12 @@ pub enum HourFormat {
     Hour24,
 }
 
+impl Default for HourFormat {
+    fn default() -> Self {
+        Self::Hour12
+    }
+}
+
 #[derive(Clone, Debug, Default, Deserialize, JsonSchema)]
 pub struct TerminalSettings {
     pub shell: Option<Shell>,