From ff25fb2ddeb9c6f70294e5099a617219e30248d8 Mon Sep 17 00:00:00 2001 From: Alexandre Pasmantier <47638216+alexpasmantier@users.noreply.github.com> Date: Thu, 14 Nov 2024 00:08:19 +0100 Subject: [PATCH] fix(windows): #20 respect `TELEVISION_CONFIG` env var on windows (#21) * fix: remove org name from default platform config and data dirs * fix: adjust config and data dirs fallback logic * patch * update README --- Cargo.lock | 2 +- Cargo.toml | 2 +- README.md | 10 ++++++++-- crates/television/config.rs | 33 +++++++++++++++++++-------------- 4 files changed, 29 insertions(+), 18 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 7ab4794..307c989 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2963,7 +2963,7 @@ dependencies = [ [[package]] name = "television" -version = "0.4.20" +version = "0.4.21" dependencies = [ "anyhow", "better-panic", diff --git a/Cargo.toml b/Cargo.toml index 1a61241..dd23526 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "television" -version = "0.4.20" +version = "0.4.21" edition = "2021" description = "The revolution will be televised." license = "MIT" diff --git a/README.md b/README.md index 91c016f..5261de4 100644 --- a/README.md +++ b/README.md @@ -160,10 +160,16 @@ is a simple TOML file that allows you to customize the behavior of `television` |Platform|Value| |--------|:-----:| |Linux|`$XDG_CONFIG_HOME/television/config.toml` or `$HOME/.config/television/config.toml`| -|macOS|`$HOME/Library/Application Support/television/config.toml`| +|macOS|`$HOME/Library/Application Support/com.television/config.toml`| |Windows|`{FOLDERID_LocalAppData}\television\config`| -Any of these paths may be overriden by setting the `TELEVISION_CONFIG` environment variable to the path of your desired configuration folder. +You may also override these default paths by setting the `TELEVISION_CONFIG` environment variable to the path of your desired configuration **folder**. + +Example: +```bash +export TELEVISION_CONFIG=$HOME/.config/television +touch $TELEVISION_CONFIG/config.toml +``` #### Default Configuration The default configuration file can be found in [./.config/config.toml](./.config/config.toml). diff --git a/crates/television/config.rs b/crates/television/config.rs index 3e844da..7ab603d 100644 --- a/crates/television/config.rs +++ b/crates/television/config.rs @@ -1,3 +1,4 @@ +#![allow(clippy::module_name_repetitions)] use std::{collections::HashMap, env, path::PathBuf}; use crate::{ @@ -13,7 +14,7 @@ use lazy_static::lazy_static; use ratatui::style::{Color, Modifier, Style}; use serde::{de::Deserializer, Deserialize}; use television_previewers::previewers::{self, PreviewerConfig}; -use tracing::{info, warn}; +use tracing::{debug, warn}; const CONFIG: &str = include_str!("../../.config/config.toml"); @@ -96,24 +97,25 @@ lazy_static! { pub static ref PROJECT_NAME_UPPER: String = PROJECT_NAME.to_uppercase().to_string(); pub static ref DATA_FOLDER: Option = // if `TELEVISION_DATA` is set, use that as the data directory - env::var_os(format!("{}_DATA", PROJECT_NAME_UPPER.clone())).or_else(|| { + env::var_os(format!("{}_DATA", PROJECT_NAME_UPPER.clone())).map(PathBuf::from).or_else(|| { // otherwise, use the XDG data directory - env::var_os("XDG_DATA_HOME") - }).map(PathBuf::from).map(|p| p.join(PROJECT_NAME.as_str())).filter(|p| p.is_absolute()); + env::var_os("XDG_DATA_HOME").map(PathBuf::from).map(|p| p.join(PROJECT_NAME.as_str())).filter(|p| p.is_absolute()) + }); pub static ref CONFIG_FOLDER: Option = - // if `TELEVISION_CONFIG` is set, use that as the config directory - env::var_os(format!("{}_CONFIG", PROJECT_NAME_UPPER.clone())).or_else(|| { - // otherwise, use the XDG config directory - env::var_os("XDG_CONFIG_HOME") - }).map(PathBuf::from).map(|p| p.join(PROJECT_NAME.as_str())).filter(|p| p.is_absolute()); + // if `TELEVISION_CONFIG` is set, use that as the television config directory + env::var_os(format!("{}_CONFIG", PROJECT_NAME_UPPER.clone())).map(PathBuf::from).or_else(|| { + // otherwise, use the XDG config directory + 'television' + env::var_os("XDG_CONFIG_HOME").map(PathBuf::from).map(|p| p.join(PROJECT_NAME.as_str())).filter(|p| p.is_absolute()) + }); } const CONFIG_FILE_NAME: &str = "config.toml"; impl Config { + #[allow(clippy::missing_panics_doc, clippy::missing_errors_doc)] pub fn new() -> Result { let default_config: Config = - toml::from_str(CONFIG).expect("default config"); + toml::from_str(CONFIG).expect("default config should be valid"); let data_dir = get_data_dir(); let config_dir = get_config_dir(); @@ -157,29 +159,32 @@ impl Config { pub fn get_data_dir() -> PathBuf { let directory = if let Some(s) = DATA_FOLDER.clone() { + debug!("Using data directory: {:?}", s); s } else if let Some(proj_dirs) = project_directory() { + debug!("Falling back to default data dir"); proj_dirs.data_local_dir().to_path_buf() } else { - PathBuf::from("../../../../..").join(".data") + PathBuf::from(".").join(".data") }; directory } pub fn get_config_dir() -> PathBuf { let directory = if let Some(s) = CONFIG_FOLDER.clone() { + debug!("Using config directory: {:?}", s); s } else if let Some(proj_dirs) = project_directory() { + debug!("Falling back to default config dir"); proj_dirs.config_local_dir().to_path_buf() } else { - PathBuf::from("../../../../..").join("../../../../../.config") + PathBuf::from(".").join(".config") }; - info!("Using config directory: {:?}", directory); directory } fn project_directory() -> Option { - ProjectDirs::from("com", "alexpasmantier", env!("CARGO_PKG_NAME")) + ProjectDirs::from("com", "", env!("CARGO_PKG_NAME")) } #[derive(Clone, Debug, Default, Deref, DerefMut)]