mirror of
https://github.com/rtk-ai/rtk.git
synced 2026-09-19 07:33:17 +08:00
126 lines
2.9 KiB
Rust
126 lines
2.9 KiB
Rust
use anyhow::Result;
|
|
use serde::{Deserialize, Serialize};
|
|
use std::path::PathBuf;
|
|
|
|
#[derive(Debug, Serialize, Deserialize, Default)]
|
|
pub struct Config {
|
|
#[serde(default)]
|
|
pub tracking: TrackingConfig,
|
|
#[serde(default)]
|
|
pub display: DisplayConfig,
|
|
#[serde(default)]
|
|
pub filters: FilterConfig,
|
|
}
|
|
|
|
#[derive(Debug, Serialize, Deserialize)]
|
|
pub struct TrackingConfig {
|
|
pub enabled: bool,
|
|
pub history_days: u32,
|
|
#[serde(skip_serializing_if = "Option::is_none")]
|
|
pub database_path: Option<PathBuf>,
|
|
}
|
|
|
|
impl Default for TrackingConfig {
|
|
fn default() -> Self {
|
|
Self {
|
|
enabled: true,
|
|
history_days: 90,
|
|
database_path: None,
|
|
}
|
|
}
|
|
}
|
|
|
|
#[derive(Debug, Serialize, Deserialize)]
|
|
pub struct DisplayConfig {
|
|
pub colors: bool,
|
|
pub emoji: bool,
|
|
pub max_width: usize,
|
|
}
|
|
|
|
impl Default for DisplayConfig {
|
|
fn default() -> Self {
|
|
Self {
|
|
colors: true,
|
|
emoji: true,
|
|
max_width: 120,
|
|
}
|
|
}
|
|
}
|
|
|
|
#[derive(Debug, Serialize, Deserialize)]
|
|
pub struct FilterConfig {
|
|
pub ignore_dirs: Vec<String>,
|
|
pub ignore_files: Vec<String>,
|
|
}
|
|
|
|
impl Default for FilterConfig {
|
|
fn default() -> Self {
|
|
Self {
|
|
ignore_dirs: vec![
|
|
".git".into(),
|
|
"node_modules".into(),
|
|
"target".into(),
|
|
"__pycache__".into(),
|
|
".venv".into(),
|
|
"vendor".into(),
|
|
],
|
|
ignore_files: vec!["*.lock".into(), "*.min.js".into(), "*.min.css".into()],
|
|
}
|
|
}
|
|
}
|
|
|
|
impl Config {
|
|
pub fn load() -> Result<Self> {
|
|
let path = get_config_path()?;
|
|
|
|
if path.exists() {
|
|
let content = std::fs::read_to_string(&path)?;
|
|
let config: Config = toml::from_str(&content)?;
|
|
Ok(config)
|
|
} else {
|
|
Ok(Config::default())
|
|
}
|
|
}
|
|
|
|
pub fn save(&self) -> Result<()> {
|
|
let path = get_config_path()?;
|
|
|
|
if let Some(parent) = path.parent() {
|
|
std::fs::create_dir_all(parent)?;
|
|
}
|
|
|
|
let content = toml::to_string_pretty(self)?;
|
|
std::fs::write(&path, content)?;
|
|
Ok(())
|
|
}
|
|
|
|
pub fn create_default() -> Result<PathBuf> {
|
|
let config = Config::default();
|
|
config.save()?;
|
|
get_config_path()
|
|
}
|
|
}
|
|
|
|
fn get_config_path() -> Result<PathBuf> {
|
|
let config_dir = dirs::config_dir().unwrap_or_else(|| PathBuf::from("."));
|
|
Ok(config_dir.join("rtk").join("config.toml"))
|
|
}
|
|
|
|
pub fn show_config() -> Result<()> {
|
|
let path = get_config_path()?;
|
|
println!("Config: {}", path.display());
|
|
println!();
|
|
|
|
if path.exists() {
|
|
let config = Config::load()?;
|
|
println!("{}", toml::to_string_pretty(&config)?);
|
|
} else {
|
|
println!("(default config, file not created)");
|
|
println!();
|
|
let config = Config::default();
|
|
println!("{}", toml::to_string_pretty(&config)?);
|
|
}
|
|
|
|
Ok(())
|
|
}
|