From 212ce86f499939a305945a74839c7c21048ba81e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jon=20Grythe=20St=C3=B8dle?= Date: Mon, 15 Jun 2020 00:09:02 +0200 Subject: [PATCH] Move config directory This changes the config directory from the `dirs::cache_dir` to `dirs::config_dir`. It does however keep the cache directory as the default logging directory, as it seems a better fit. It also adds a function, `migrate_config`, which is called at startup to move directory entries inside the "old" config directory to the "new" one (but it skips moving log files). The intention is that this function can be removed after a few releases when the likelihood of someone upgrading from 0.6.0 or earlier is fairly small. Fixes #98 --- THEMES.md | 8 ++++++-- src/main.rs | 35 +++++++++++++++++++++++++++++++++-- 2 files changed, 39 insertions(+), 4 deletions(-) diff --git a/THEMES.md b/THEMES.md index b0852646..a9a7fa47 100644 --- a/THEMES.md +++ b/THEMES.md @@ -4,7 +4,11 @@ default on light terminal: ![](assets/light-theme.png) to change the colors of the program you have to modify `theme.ron` file -[Ron format](https://github.com/ron-rs/ron) located at config path (same as [log path](README.md#diagnostics)). +[Ron format](https://github.com/ron-rs/ron) located at config path. The path differs depending on the operating system: + +* `$HOME/Library/Preferences/gitui/theme.ron` (mac) +* `$XDG_CONFIG_HOME/gitui/theme.ron` (linux using XDG) +* `$HOME/.config/gitui/theme.ron` (linux) Valid colors can be found in [ColorDef](./src/ui/style.rs#ColorDef) struct. note that rgb colors might not be supported -in every terminal. \ No newline at end of file +in every terminal. diff --git a/src/main.rs b/src/main.rs index d066ce16..cdb19be7 100644 --- a/src/main.rs +++ b/src/main.rs @@ -66,6 +66,9 @@ fn main() -> Result<()> { return Ok(()); } + // TODO: To be removed in a future version, when upgrading from 0.6.x or earlier is unlikely + migrate_config()?; + setup_terminal()?; defer! { shutdown_terminal().expect("shutdown failed"); @@ -202,7 +205,26 @@ fn start_terminal( Ok(terminal) } -fn get_app_config_path() -> Result { +fn migrate_config() -> Result<()> { + let cache_path: PathBuf = get_app_cache_path()?; + + let entries = cache_path + .read_dir()? + .flat_map(|dir_entry| dir_entry) + .filter(|entry| { + !entry.file_name().to_string_lossy().ends_with(".log") + }); + + for entry in entries { + let mut config_path: PathBuf = get_app_config_path()?; + config_path.push(entry.file_name()); + fs::rename(entry.path(), config_path)?; + } + + Ok(()) +} + +fn get_app_cache_path() -> Result { let mut path = dirs::cache_dir() .ok_or_else(|| anyhow!("failed to find os cache dir."))?; @@ -211,8 +233,17 @@ fn get_app_config_path() -> Result { Ok(path) } +fn get_app_config_path() -> Result { + let mut path = dirs::config_dir() + .ok_or_else(|| anyhow!("failed to find os config dir."))?; + + path.push("gitui"); + fs::create_dir_all(&path)?; + Ok(path) +} + fn setup_logging() -> Result<()> { - let mut path = get_app_config_path()?; + let mut path = get_app_cache_path()?; path.push("gitui.log"); let _ = WriteLogger::init(