Skip to content

Commit

Permalink
src: use in-house XDG base implementation for config
Browse files Browse the repository at this point in the history
removes the need for directories dependency and its dependencies for
other OS's etc.
  • Loading branch information
marcelohdez committed Nov 8, 2024
1 parent 0c7f084 commit 89ef7b3
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 156 deletions.
152 changes: 10 additions & 142 deletions Cargo.lock

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

1 change: 0 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ path = "src/main.rs"
anyhow = "1.0.0"
clap = { version = "4.4.0", features = ["derive"] }
clap_complete = "4.4.0"
directories = "5.0.0"
env_logger = "0.11.0"
log = "0.4.20"
serde = { version = "1.0.0", features = ["derive"] }
Expand Down
43 changes: 30 additions & 13 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,16 @@
use std::{borrow::Cow, fs::File, io::read_to_string, path::Path, process, thread, time::Duration};
use std::{
borrow::Cow,
env,
fs::File,
io::read_to_string,
path::{Path, PathBuf},
process, thread,
time::Duration,
};

use anyhow::{anyhow, bail, Context};
use anyhow::{anyhow, Context};
use clap::Parser;
use dim_screen::{dim::DimData, opts::DimOpts, CONFIG_FILENAME, DEFAULT_ALPHA, DEFAULT_DURATION};
use directories::ProjectDirs;
use log::{debug, info};
use smithay_client_toolkit::{
compositor::CompositorState,
Expand Down Expand Up @@ -49,22 +56,32 @@ fn main() -> anyhow::Result<()> {
}

fn get_config(dir: Option<&Path>) -> anyhow::Result<Option<DimOpts>> {
let project_dirs = ProjectDirs::from("com", "marcelohdez", "dim");

let Some(dir): Option<Cow<Path>> = dir
.map(Cow::Borrowed)
.or(project_dirs.map(|dirs| Cow::Owned(dirs.config_dir().join(CONFIG_FILENAME))))
else {
bail!("Could not generate project directories on this OS?");
let config = match dir {
Some(user_config) => Cow::Borrowed(user_config),
None => {
// follow XDG base directory spec, checking $XDG_CONFIG_HOME first then defaulting to $HOME/.config
let config_home = env::var("XDG_CONFIG_HOME")
.map(PathBuf::from)
.or(env::var("HOME")
.map(PathBuf::from)
.map(|p| p.join(".config")));

if let Ok(path) = config_home {
Cow::Owned(path.join("dim").join(CONFIG_FILENAME))
} else {
info!("No config path, neither XDG_CONFIG_HOME nor HOME are set.");
return Ok(None);
}
}
};

if !dir.exists() {
if !config.exists() {
info!("No config found!");
return Ok(None);
}

debug!("Config file found at {dir:?}");
let file = File::open(dir).context("Failed to open config file")?;
debug!("Config file found at {config:?}");
let file = File::open(config).context("Failed to open config file")?;
let config: DimOpts = toml::from_str(&read_to_string(file)?)?;

debug!("Config: {config:?}");
Expand Down

0 comments on commit 89ef7b3

Please sign in to comment.