use std::env;
use std::fs;
use std::path::PathBuf;
use anyhow::{Context, Result};
use serde::{Deserialize, Serialize};
pub fn config_dir() -> PathBuf {
dirs_config_dir()
.unwrap_or_else(|| PathBuf::from("."))
.join("mfe-player")
}
fn legacy_config_dir() -> PathBuf {
dirs_config_dir()
.unwrap_or_else(|| PathBuf::from("."))
.join("nuclear-tui")
}
fn legacy_cache_dir() -> PathBuf {
if let Ok(xdg) = env::var("XDG_CACHE_HOME") {
if !xdg.trim().is_empty() {
return PathBuf::from(xdg).join("nuclear-tui");
}
}
env::var_os("HOME")
.map(|home| PathBuf::from(home).join(".cache").join("nuclear-tui"))
.unwrap_or_else(|| PathBuf::from(".cache").join("nuclear-tui"))
}
pub fn migrate_legacy() {
let new = config_dir();
let old = legacy_config_dir();
if !new.exists() && old.is_dir() && fs::rename(&old, &new).is_err() {
let _ = copy_dir(&old, &new);
}
let new_cache = cache_dir();
let old_cache = legacy_cache_dir();
if !new_cache.exists() && old_cache.is_dir() && fs::rename(&old_cache, &new_cache).is_err() {
let _ = copy_dir(&old_cache, &new_cache);
}
}
fn copy_dir(from: &PathBuf, to: &PathBuf) -> std::io::Result<()> {
fs::create_dir_all(to)?;
for entry in fs::read_dir(from)? {
let entry = entry?;
let target = to.join(entry.file_name());
if entry.file_type()?.is_dir() {
copy_dir(&entry.path(), &target)?;
} else {
fs::copy(entry.path(), target)?;
}
}
Ok(())
}
fn dirs_config_dir() -> Option<PathBuf> {
#[cfg(windows)]
if let Ok(appdata) = env::var("APPDATA") {
if !appdata.trim().is_empty() {
return Some(PathBuf::from(appdata));
}
}
if let Ok(xdg) = env::var("XDG_CONFIG_HOME") {
if !xdg.trim().is_empty() {
return Some(PathBuf::from(xdg));
}
}
env::var_os("HOME").map(|home| PathBuf::from(home).join(".config"))
}
pub fn plugins_dir() -> PathBuf {
config_dir().join("plugins")
}
pub fn state_path() -> PathBuf {
config_dir().join("state.json")
}
pub fn taste_path() -> PathBuf {
config_dir().join("taste.json")
}
pub fn config_path() -> PathBuf {
config_dir().join("config.toml")
}
pub fn mpv_log_path() -> PathBuf {
config_dir().join("mpv.log")
}
pub fn cache_dir() -> PathBuf {
#[cfg(windows)]
if let Ok(local) = env::var("LOCALAPPDATA") {
if !local.trim().is_empty() {
return PathBuf::from(local).join("mfe-player");
}
}
#[cfg(target_os = "macos")]
if let Some(home) = env::var_os("HOME") {
return PathBuf::from(home)
.join("Library")
.join("Caches")
.join("mfe-player");
}
if let Ok(xdg) = env::var("XDG_CACHE_HOME") {
if !xdg.trim().is_empty() {
return PathBuf::from(xdg).join("mfe-player");
}
}
env::var_os("HOME")
.map(|home| PathBuf::from(home).join(".cache").join("mfe-player"))
.unwrap_or_else(|| PathBuf::from(".cache").join("mfe-player"))
}
pub fn downloads_dir() -> PathBuf {
cache_dir().join("downloads")
}
pub fn artwork_dir() -> PathBuf {
cache_dir().join("artwork")
}
pub fn library_cache_path() -> PathBuf {
config_dir().join("library.json")
}
pub fn runtime_dir() -> PathBuf {
if let Ok(xdg) = env::var("XDG_RUNTIME_DIR") {
if !xdg.trim().is_empty() {
return PathBuf::from(xdg).join("mfe-player");
}
}
let user = env::var("USER")
.or_else(|_| env::var("USERNAME"))
.unwrap_or_else(|_| "user".to_string());
env::temp_dir().join(format!("mfe-player-{user}"))
}
#[cfg(windows)]
fn user_name() -> String {
env::var("USER")
.or_else(|_| env::var("USERNAME"))
.unwrap_or_else(|_| "user".to_string())
}
pub fn mpv_socket_path() -> PathBuf {
#[cfg(windows)]
{
PathBuf::from(format!(r"\\.\pipe\mfe-player-{}", user_name()))
}
#[cfg(not(windows))]
{
runtime_dir().join("mpv.sock")
}
}
pub fn session_lock_path() -> PathBuf {
runtime_dir().join("session.pid")
}
pub fn handoff_path() -> PathBuf {
runtime_dir().join("handoff.pid")
}
pub fn daemon_pid_path() -> PathBuf {
runtime_dir().join("daemon.pid")
}
pub fn daemon_heartbeat_path() -> PathBuf {
runtime_dir().join("daemon.heartbeat")
}
pub fn daemon_stop_path() -> PathBuf {
runtime_dir().join("daemon.stop")
}
pub fn daemon_control_path() -> PathBuf {
runtime_dir().join("daemon.control")
}
pub fn daemon_log_path() -> PathBuf {
config_dir().join("daemon.log")
}
pub fn private_mpv_socket_path(pid: u32) -> PathBuf {
#[cfg(windows)]
{
PathBuf::from(format!(r"\\.\pipe\mfe-player-{}-{pid}", user_name()))
}
#[cfg(not(windows))]
{
runtime_dir().join(format!("mpv-{pid}.sock"))
}
}
fn default_true() -> bool {
true
}
fn default_media() -> String {
"auto".to_string()
}
fn default_seek() -> f64 {
5.0
}
fn default_long_seek() -> f64 {
30.0
}
fn default_volume_step() -> f64 {
5.0
}
fn default_max_volume() -> f64 {
130.0
}
fn default_speed_step() -> f64 {
0.1
}
fn default_theme() -> String {
"mfe".to_string()
}
fn default_view() -> String {
"search".to_string()
}
fn default_cava_position() -> String {
"right".to_string()
}
fn default_cava_bars() -> u16 {
28
}
fn default_eq() -> String {
"flat".to_string()
}
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(default)]
pub struct AppConfig {
pub theme: String,
pub seek_step: f64,
pub long_seek_step: f64,
pub volume_step: f64,
pub max_volume: f64,
pub speed_step: f64,
pub default_view: String,
pub prefetch: bool,
pub radio: bool,
pub mouse: bool,
pub cava: bool,
pub cava_position: String,
pub cava_bars: u16,
pub normalize: bool,
pub eq: String,
pub eq_gains: Vec<f64>,
pub audio_device: String,
pub media: String,
pub mpris: bool,
pub library_paths: Vec<String>,
pub listenbrainz_token: String,
pub fade_seconds: f64,
pub replaygain: String,
pub transparent: bool,
pub wallpaper: String,
pub ambient_art: bool,
pub motion: bool,
pub icons: String,
pub notifications: bool,
pub autoplay: String,
pub autoplay_explore: f64,
pub autoplay_probe_every: u32,
pub autoplay_probe: bool,
pub taste_enabled: bool,
pub discord: bool,
pub discord_client_id: String,
pub discord_asset: String,
pub discord_show_paused: bool,
pub discord_show_buttons: bool,
pub discord_when_private: bool,
}
impl Default for AppConfig {
fn default() -> Self {
Self {
theme: default_theme(),
seek_step: default_seek(),
long_seek_step: default_long_seek(),
volume_step: default_volume_step(),
max_volume: default_max_volume(),
speed_step: default_speed_step(),
default_view: default_view(),
prefetch: default_true(),
radio: false,
mouse: default_true(),
cava: default_true(),
cava_position: default_cava_position(),
cava_bars: default_cava_bars(),
normalize: false,
eq: default_eq(),
eq_gains: Vec::new(),
audio_device: String::new(),
media: default_media(),
mpris: default_true(),
library_paths: Vec::new(),
listenbrainz_token: String::new(),
fade_seconds: 0.0,
replaygain: "off".to_string(),
transparent: true,
wallpaper: String::new(),
ambient_art: false,
motion: true,
icons: "nerd".to_string(),
notifications: false,
autoplay: "taste".to_string(),
autoplay_explore: 0.12,
autoplay_probe_every: 10,
autoplay_probe: true,
taste_enabled: true,
discord: false,
discord_client_id: String::new(),
discord_asset: "mfe".to_string(),
discord_show_paused: false,
discord_show_buttons: true,
discord_when_private: false,
}
}
}
const DEFAULT_CONFIG: &str = r#"# mfe-player configuration
# Edit and restart the app. Every key is optional.
# Colour theme: mfe | gruvbox | nord | mono | matrix | rose |
# catppuccin | dracula | tokyo-night | solarized | amber | ocean |
# auto (your terminal's own 16 colours) | wallpaper
theme = "mfe"
# Playback steps (seconds / percent).
seek_step = 5
long_seek_step = 30
volume_step = 5
# Ceiling for the volume meter and +/- keys (mpv amplifies above 100%).
max_volume = 130
# Playback-speed step for [ and ] (0.05–0.5).
speed_step = 0.1
# View shown at startup: search | queue | favorites | playlists | downloads |
# plugins | library | history | browse
default_view = "search"
# Resolve the next track while the current one plays (no silence between songs).
prefetch = true
# When the queue ends, keep playing related tracks (Spotify-style radio).
radio = false
# Autoplay when the queue ends and repeat is off: off | similar | taste.
# "taste" learns from what you finish, repeat and skip and keeps the vibe.
autoplay = "taste"
# Learn a local taste profile from plays and skips (stored in taste.json).
taste_enabled = true
# How far the batch may wander outside the profile, and how often to try one
# deliberate out-of-profile track (0 disables the mismatch probe).
autoplay_explore = 0.12
autoplay_probe = true
autoplay_probe_every = 10
# Mouse: wheel scrolls, click selects, double-click plays, click the progress bar to seek.
mouse = true
# Spectrum visualiser (needs `cava` installed).
cava = true
cava_position = "right" # right | left
cava_bars = 28
# Audio polish.
normalize = false # loudness normalisation via mpv dynaudnorm
eq = "flat" # flat | bass | vocal | night | loud
audio_device = "" # empty = system default (e.g. "alsa/hdmi:CARD=HDMI")
# Desktop media keys / playerctl integration.
# media = "auto" uses the native backend: MPRIS on Linux, SMTC on Windows,
# Now Playing on macOS. "off" hides the media card entirely.
media = "auto"
mpris = true # legacy switch; false disables every backend
# Local music folders (FLAC, MP3, Opus, Ogg, M4A...). Scanned on launch.
# Example: library_paths = ["~/Music", "/mnt/archive/albums"]
library_paths = []
# Optional: scrobble to ListenBrainz (https://listenbrainz.org/profile/).
# Paste your user token here and every play is submitted from a background
# thread. Leave empty to keep everything local.
listenbrainz_token = ""
# Smart fade: seconds of fade at the start and the end of every track.
# 0 = off, 2-4 = gentle, 6 = long. Cycle with Alt+F.
fade_seconds = 0
# ReplayGain for your own files: off | track | album. Cycle with Alt+R.
replaygain = "off"
# Custom ten-band EQ curve in dB. Tune it with Ctrl+E in the app.
eq_gains = [0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0]
# Let the terminal's transparency show your wallpaper through the app.
# On by default; toggle at runtime with Alt+W.
transparent = true
# Optional explicit wallpaper for theme = "wallpaper". Empty means "ask the
# compositor" (swww, hyprpaper, GNOME, Plasma, macOS, Windows).
wallpaper = ""
# Ambient tint: pull a vivid accent from the current album cover and let the
# theme follow the music. Grey covers are ignored. Toggle with Alt+A.
ambient_art = false
# Motion: gradients, marquees and borders react to cava's energy. Turn off for
# a completely still interface (screen readers, remote shells, calm setups).
# Toggle with Alt+M.
motion = true
# Glyphs: nerd (default), ascii (plain TTYs/screen readers), or auto.
icons = "nerd"
# Desktop notification when a new track starts (Linux, macOS, Windows).
notifications = false
# Discord Rich Presence. Create an application at
# https://discord.com/developers/applications, copy its client id, upload an
# image asset (default key "mfe"), then enable this. Nothing leaves your
# machine: presence is sent over Discord's local IPC socket.
discord = false
discord_client_id = ""
discord_asset = "mfe"
# Keep the card visible when paused, and show project buttons (max two).
discord_show_paused = false
discord_show_buttons = true
# Hide presence for private sessions (headless daemon keeps showing it).
discord_when_private = false
"#;
fn write_atomic(path: &std::path::Path, raw: &str) {
let tmp = path.with_extension("toml.tmp");
if fs::write(&tmp, raw).is_ok() {
let _ = crate::platform::fs_util::replace_file(&tmp, path);
}
}
pub fn load_config() -> (AppConfig, Option<String>) {
let path = config_path();
if !path.exists() {
if let Some(parent) = path.parent() {
let _ = fs::create_dir_all(parent);
}
write_atomic(&path, DEFAULT_CONFIG);
return (AppConfig::default(), None);
}
match fs::read_to_string(&path) {
Ok(raw) => match toml::from_str::<AppConfig>(&raw) {
Ok(mut config) => {
config.seek_step = config.seek_step.clamp(1.0, 600.0);
config.long_seek_step = config.long_seek_step.clamp(1.0, 600.0);
config.volume_step = config.volume_step.clamp(1.0, 50.0);
config.max_volume = config.max_volume.clamp(20.0, 300.0);
config.speed_step = config.speed_step.clamp(0.05, 0.5);
config.cava_bars = config.cava_bars.clamp(4, 96);
config.fade_seconds = config.fade_seconds.clamp(0.0, 15.0);
config.replaygain = config.replaygain.to_ascii_lowercase();
if !["off", "track", "album"].contains(&config.replaygain.as_str()) {
config.replaygain = "off".to_string();
}
config.autoplay = config.autoplay.to_ascii_lowercase();
if !["off", "similar", "taste"].contains(&config.autoplay.as_str()) {
config.autoplay = "taste".to_string();
}
config.autoplay_explore = config.autoplay_explore.clamp(0.0, 1.0);
config.autoplay_probe_every = config.autoplay_probe_every.clamp(3, 100);
config.discord_client_id = config.discord_client_id.trim().to_string();
config.discord_asset = config.discord_asset.trim().to_string();
if config.discord_asset.is_empty() {
config.discord_asset = "mfe".to_string();
}
(config, None)
}
Err(err) => (
AppConfig::default(),
Some(format!("config.toml is invalid, using defaults: {err}")),
),
},
Err(err) => (
AppConfig::default(),
Some(format!("could not read config.toml: {err}")),
),
}
}
fn set_key_in_file(key: &str, value: &str) {
let path = config_path();
if let Some(parent) = path.parent() {
let _ = fs::create_dir_all(parent);
}
let raw = fs::read_to_string(&path).unwrap_or_else(|_| DEFAULT_CONFIG.to_string());
let mut replaced = false;
let mut out = String::new();
for line in raw.lines() {
let starts = line
.trim_start()
.strip_prefix(key)
.and_then(|rest| rest.trim_start().strip_prefix('='));
if !replaced && starts.is_some() {
out.push_str(&format!("{key} = {value}\n"));
replaced = true;
} else {
out.push_str(line);
out.push('\n');
}
}
if !replaced {
out.push_str(&format!("{key} = {value}\n"));
}
write_atomic(&path, &out);
}
pub fn set_theme_in_file(name: &str) {
set_key_in_file("theme", &format!("\"{name}\""));
}
pub fn set_line(key: &str, value: &str) {
set_key_in_file(key, value);
}
pub fn ensure_dirs() -> Result<()> {
fs::create_dir_all(config_dir())
.with_context(|| format!("creating {}", config_dir().display()))?;
fs::create_dir_all(plugins_dir())
.with_context(|| format!("creating {}", plugins_dir().display()))?;
fs::create_dir_all(runtime_dir())
.with_context(|| format!("creating {}", runtime_dir().display()))?;
fs::create_dir_all(downloads_dir())
.with_context(|| format!("creating {}", downloads_dir().display()))?;
Ok(())
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn transparency_is_the_default() {
assert!(AppConfig::default().transparent);
assert!(!AppConfig::default().ambient_art);
}
#[test]
fn shipped_config_parses_with_the_tuning_knobs() {
let cfg: AppConfig = toml::from_str(DEFAULT_CONFIG).expect("shipped config parses");
assert_eq!(cfg.max_volume, 130.0);
assert_eq!(cfg.speed_step, 0.1);
assert_eq!(cfg.default_view, "search");
}
}