Skip to content

Commit

Permalink
fix(windows): #20 respect TELEVISION_CONFIG env var on windows (#21)
Browse files Browse the repository at this point in the history
* fix: remove org name from default platform config and data dirs

* fix: adjust config and data dirs fallback logic

* patch

* update README
  • Loading branch information
alexpasmantier authored Nov 13, 2024
1 parent 65bb26e commit ff25fb2
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 18 deletions.
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -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"
Expand Down
10 changes: 8 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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).
Expand Down
33 changes: 19 additions & 14 deletions crates/television/config.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#![allow(clippy::module_name_repetitions)]
use std::{collections::HashMap, env, path::PathBuf};

use crate::{
Expand All @@ -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");

Expand Down Expand Up @@ -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<PathBuf> =
// 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<PathBuf> =
// 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<Self, config::ConfigError> {
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();
Expand Down Expand Up @@ -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> {
ProjectDirs::from("com", "alexpasmantier", env!("CARGO_PKG_NAME"))
ProjectDirs::from("com", "", env!("CARGO_PKG_NAME"))
}

#[derive(Clone, Debug, Default, Deref, DerefMut)]
Expand Down

0 comments on commit ff25fb2

Please sign in to comment.