use std::fs;
use std::path::PathBuf;
use serde::{Deserialize, Serialize};
use crate::voice::Voice;
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(default)]
pub struct Config {
pub require_confirmation: bool,
pub wait_command: u64,
pub wait_slow_command: u64,
pub slow_commands: Vec<String>,
pub no_rerun: Vec<String>,
pub exclude_rules: Vec<String>,
pub voice: Voice,
pub quiet: bool,
pub remember: bool,
}
impl Default for Config {
fn default() -> Self {
Self {
require_confirmation: true,
wait_command: 3,
wait_slow_command: 15,
slow_commands: ["lein", "react-native", "gradle", "./gradlew", "vagrant"]
.map(String::from)
.to_vec(),
no_rerun: [
"rm", "rmdir", "mv", "dd", "mkfs", "shred", "kill", "killall", "pkill", "shutdown",
"reboot", "halt", "poweroff", "vim", "vi", "nvim", "nano", "emacs", "less", "more",
"top", "htop", "ssh",
]
.map(String::from)
.to_vec(),
exclude_rules: Vec::new(),
voice: Voice::Chill,
quiet: false,
remember: true,
}
}
}
pub fn config_dir() -> PathBuf {
std::env::var_os("XDG_CONFIG_HOME")
.filter(|dir| !dir.is_empty())
.map(PathBuf::from)
.or_else(|| dirs::home_dir().map(|home| home.join(".config")))
.unwrap_or_else(|| PathBuf::from("."))
.join("welp")
}
pub fn config_path() -> PathBuf {
config_dir().join("config.toml")
}
impl Config {
pub fn voice(&self) -> Voice {
if self.quiet { Voice::Quiet } else { self.voice }
}
pub fn load() -> Self {
fs::read_to_string(config_path())
.ok()
.and_then(|text| toml::from_str(&text).ok())
.unwrap_or_default()
}
}