From d030f97d4512675318d7271d984ad50490816a72 Mon Sep 17 00:00:00 2001 From: Fred Sundvik Date: Fri, 31 Dec 2021 15:07:12 +0200 Subject: [PATCH] Fix for non-existing data directory Also respect XDG_DATA_HOME on unix systems instead of using hardcoded path. --- Cargo.lock | 19 ++++++++++++++++--- Cargo.toml | 1 + src/settings/window_geometry.rs | 27 ++++++++++++++++++++------- 3 files changed, 37 insertions(+), 10 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 37ebb2d..6a2a56e 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -570,6 +570,15 @@ dependencies = [ "dirs-sys", ] +[[package]] +name = "dirs" +version = "3.0.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "30baa043103c9d0c2a57cf537cc2f35623889dc0d405e6c3cccfadbc81c71309" +dependencies = [ + "dirs-sys", +] + [[package]] name = "dirs-sys" version = "0.3.6" @@ -1325,7 +1334,7 @@ dependencies = [ "cfg-if 0.1.10", "clap", "derive-new", - "dirs", + "dirs 2.0.2", "euclid", "flexi_logger", "futures 0.3.17", @@ -1353,6 +1362,7 @@ dependencies = [ "winapi", "winit 0.24.0 (git+https://github.com/neovide/winit?branch=new-keyboard-all)", "winres", + "xdg", ] [[package]] @@ -2673,9 +2683,12 @@ dependencies = [ [[package]] name = "xdg" -version = "2.2.0" +version = "2.4.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d089681aa106a86fade1b0128fb5daf07d5867a509ab036d99988dec80429a57" +checksum = "3a23fe958c70412687039c86f578938b4a0bb50ec788e96bce4d6ab00ddd5803" +dependencies = [ + "dirs 3.0.2", +] [[package]] name = "xkbcommon-dl" diff --git a/Cargo.toml b/Cargo.toml index 7b7af35..85b6f66 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -44,6 +44,7 @@ winit = { git = "https://github.com/neovide/winit", branch = "new-keyboard-all" gl = "0.14.0" swash = "0.1.4" clap="2.33.3" +xdg="2.4.0" [dev-dependencies] mockall = "0.7.0" diff --git a/src/settings/window_geometry.rs b/src/settings/window_geometry.rs index 6cddf67..3859c9e 100644 --- a/src/settings/window_geometry.rs +++ b/src/settings/window_geometry.rs @@ -2,20 +2,32 @@ use crate::settings::SETTINGS; use crate::utils::Dimensions; use crate::window::WindowSettings; use std::path::PathBuf; - #[cfg(unix)] -const SETTINGS_PATH: &str = ".local/share/nvim/neovide-settings.json"; -#[cfg(windows)] -const SETTINGS_PATH: &str = "AppData/Local/nvim-data/neovide-settings.json"; +use xdg; + +const SETTINGS_FILE: &str = "neovide-settings.json"; pub const DEFAULT_WINDOW_GEOMETRY: Dimensions = Dimensions { width: 100, height: 50, }; +#[cfg(windows)] fn neovim_std_datapath() -> PathBuf { - let mut settings_path = dirs::home_dir().unwrap(); - settings_path.push(SETTINGS_PATH); + let mut data_path = dirs::home_dir().unwrap(); + data_path.push("AppData/local/nvim-data"); + data_path +} + +#[cfg(unix)] +fn neovim_std_datapath() -> PathBuf { + let xdg_dirs = xdg::BaseDirectories::with_prefix("nvim").unwrap(); + xdg_dirs.get_data_home() +} + +fn settings_path() -> PathBuf { + let mut settings_path = neovim_std_datapath(); + settings_path.push(SETTINGS_FILE); settings_path } @@ -42,7 +54,8 @@ pub fn maybe_save_window_size(grid_size: Option) { DEFAULT_WINDOW_GEOMETRY }; - let settings_path = neovim_std_datapath(); + let settings_path = settings_path(); + std::fs::create_dir_all(neovim_std_datapath()).unwrap(); let json = serde_json::to_string(&saved_window_size).unwrap(); log::debug!("Saved Window Size: {}", json); std::fs::write(settings_path, json).unwrap();