From 3c62de34f73f9b7d3e769b2fa61f9da173f42b4d Mon Sep 17 00:00:00 2001 From: Joseph T Lyons Date: Thu, 29 Sep 2022 16:18:16 -0400 Subject: [PATCH] Change journal location setting name to "path" and default to ~ --- 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(-) diff --git a/Cargo.lock b/Cargo.lock index 28fc8fcb61..8859966acb 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2765,6 +2765,7 @@ dependencies = [ "gpui", "log", "settings", + "shellexpand", "util", "workspace", ] diff --git a/assets/settings/default.json b/assets/settings/default.json index d46dc11df5..d88d229dfb 100644 --- a/assets/settings/default.json +++ b/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" diff --git a/crates/journal/Cargo.toml b/crates/journal/Cargo.toml index 091aadd12e..8c900d9f4a 100644 --- a/crates/journal/Cargo.toml +++ b/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" \ No newline at end of file diff --git a/crates/journal/src/journal.rs b/crates/journal/src/journal.rs index c6e33d1134..4269556251 100644 --- a/crates/journal/src/journal.rs +++ b/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, cx: &mut MutableAppContext) { pub fn new_journal_entry(app_state: Arc, cx: &mut MutableAppContext) { let settings = cx.global::(); - 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, cx: &mut MutableAppContext) { .detach(); } -fn journal_dir(a: &Option) -> Option { - let journal_default_dir = dirs::home_dir()?.join("journal"); +fn journal_dir(settings: &Settings) -> Option { + 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) -> String { diff --git a/crates/settings/src/settings.rs b/crates/settings/src/settings.rs index 5d4195d3b2..69a1ea9441 100644 --- a/crates/settings/src/settings.rs +++ b/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, + pub path: Option, pub hour_format: Option, } -#[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,