Skip to content

Commit db15b59

Browse files
authored
feat: respect XDG Base Directory environment variables (#142)
1 parent 8dcfb03 commit db15b59

File tree

3 files changed

+64
-13
lines changed

3 files changed

+64
-13
lines changed

cli/src/config.rs

Lines changed: 27 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,18 @@
11
use crate::theme::ThemeColors;
22
use jolt_theme::NamedTheme;
33
use serde::{Deserialize, Serialize};
4+
use std::env;
45
use std::fs;
56
use std::path::PathBuf;
67
use std::process::Command;
78
use tracing::Level;
89

10+
// XDG Base Directory environment variables
11+
pub const XDG_CONFIG_HOME: &str = "XDG_CONFIG_HOME";
12+
pub const XDG_CACHE_HOME: &str = "XDG_CACHE_HOME";
13+
pub const XDG_DATA_HOME: &str = "XDG_DATA_HOME";
14+
pub const XDG_RUNTIME_DIR: &str = "XDG_RUNTIME_DIR";
15+
916
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize, Default)]
1017
#[serde(rename_all = "lowercase")]
1118
pub enum LogLevel {
@@ -266,25 +273,41 @@ impl Default for UnitsConfig {
266273
}
267274

268275
pub fn config_dir() -> PathBuf {
269-
dirs::config_dir()
276+
env::var(XDG_CONFIG_HOME)
277+
.ok()
278+
.filter(|s| !s.is_empty())
279+
.map(PathBuf::from)
280+
.or_else(dirs::config_dir)
270281
.unwrap_or_else(|| PathBuf::from("~/.config"))
271282
.join("jolt")
272283
}
273284

274285
pub fn cache_dir() -> PathBuf {
275-
dirs::cache_dir()
286+
env::var(XDG_CACHE_HOME)
287+
.ok()
288+
.filter(|s| !s.is_empty())
289+
.map(PathBuf::from)
290+
.or_else(dirs::cache_dir)
276291
.unwrap_or_else(|| PathBuf::from("~/.cache"))
277292
.join("jolt")
278293
}
279294

280295
pub fn data_dir() -> PathBuf {
281-
dirs::data_dir()
296+
env::var(XDG_DATA_HOME)
297+
.ok()
298+
.filter(|s| !s.is_empty())
299+
.map(PathBuf::from)
300+
.or_else(dirs::data_dir)
282301
.unwrap_or_else(|| PathBuf::from("~/.local/share"))
283302
.join("jolt")
284303
}
285304

286305
pub fn runtime_dir() -> PathBuf {
287-
dirs::runtime_dir()
306+
env::var(XDG_RUNTIME_DIR)
307+
.ok()
308+
.filter(|s| !s.is_empty())
309+
.map(PathBuf::from)
310+
.or_else(dirs::runtime_dir)
288311
.or_else(dirs::cache_dir)
289312
.unwrap_or_else(|| PathBuf::from("/tmp"))
290313
.join("jolt")

cli/src/daemon/service.rs

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -362,10 +362,15 @@ const SYSTEMD_SERVICE_NAME: &str = "jolt-daemon.service";
362362

363363
#[cfg(target_os = "linux")]
364364
fn linux_service_path() -> PathBuf {
365-
let config_dir = dirs::config_dir().unwrap_or_else(|| {
366-
let home = std::env::var("HOME").unwrap_or_default();
367-
PathBuf::from(home).join(".config")
368-
});
365+
let config_dir = std::env::var(crate::config::XDG_CONFIG_HOME)
366+
.ok()
367+
.filter(|s| !s.is_empty())
368+
.map(PathBuf::from)
369+
.or_else(dirs::config_dir)
370+
.unwrap_or_else(|| {
371+
let home = std::env::var("HOME").unwrap_or_default();
372+
PathBuf::from(home).join(".config")
373+
});
369374

370375
config_dir.join("systemd/user").join(SYSTEMD_SERVICE_NAME)
371376
}

website/src/content/docs/docs/configuration.md

Lines changed: 28 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,25 @@ The config file is located at:
2121
~/.config/jolt/config.toml
2222
```
2323

24+
### XDG Base Directory Support
25+
26+
jolt respects the [XDG Base Directory Specification](https://specifications.freedesktop.org/basedir-spec/latest/). If you set XDG environment variables, jolt will use them instead of the default paths:
27+
28+
| Variable | Default (macOS) | Default (Linux) | Used for |
29+
| ----------------- | ------------------------------- | ---------------- | -------------------------- |
30+
| `XDG_CONFIG_HOME` | `~/Library/Application Support` | `~/.config` | Config file, custom themes |
31+
| `XDG_DATA_HOME` | `~/Library/Application Support` | `~/.local/share` | Database, history |
32+
| `XDG_CACHE_HOME` | `~/Library/Caches` | `~/.cache` | Temporary cache |
33+
| `XDG_RUNTIME_DIR` | (falls back to cache) | `/run/user/$UID` | Runtime files, logs |
34+
35+
For example, to store jolt's config in `~/.config` on macOS:
36+
37+
```bash
38+
export XDG_CONFIG_HOME="$HOME/.config"
39+
```
40+
41+
With this set, jolt will use `~/.config/jolt/config.toml` instead of `~/Library/Application Support/jolt/config.toml`.
42+
2443
jolt creates this file automatically with default values on first run.
2544

2645
## Managing Config
@@ -155,11 +174,15 @@ Changes made in the config editor are saved immediately to the config file.
155174

156175
Some settings can be overridden with environment variables:
157176

158-
| Variable | Description |
159-
| ----------------- | ------------------------ |
160-
| `JOLT_CONFIG` | Custom config file path |
161-
| `JOLT_THEME` | Override theme |
162-
| `JOLT_APPEARANCE` | Override appearance mode |
177+
| Variable | Description |
178+
| ----------------- | -------------------------------------------------------------------------- |
179+
| `JOLT_CONFIG` | Custom config file path |
180+
| `JOLT_THEME` | Override theme |
181+
| `JOLT_APPEARANCE` | Override appearance mode |
182+
| `XDG_CONFIG_HOME` | Override config directory (see [XDG support](#xdg-base-directory-support)) |
183+
| `XDG_DATA_HOME` | Override data directory |
184+
| `XDG_CACHE_HOME` | Override cache directory |
185+
| `XDG_RUNTIME_DIR` | Override runtime directory |
163186

164187
Example:
165188

0 commit comments

Comments
 (0)