init, basic glogg clone
This commit is contained in:
39
src/config.rs
Normal file
39
src/config.rs
Normal file
@@ -0,0 +1,39 @@
|
||||
use serde::{Deserialize, Serialize};
|
||||
use std::fs;
|
||||
use std::path::PathBuf;
|
||||
|
||||
use crate::types::HighlightRule;
|
||||
|
||||
#[derive(Serialize, Deserialize, Default)]
|
||||
pub struct AppConfig {
|
||||
pub search_history: Vec<String>,
|
||||
pub case_sensitive: bool,
|
||||
pub use_regex: bool,
|
||||
pub last_search_query: String,
|
||||
#[serde(default)]
|
||||
pub highlight_rules: Vec<HighlightRule>,
|
||||
}
|
||||
|
||||
impl AppConfig {
|
||||
fn config_path() -> PathBuf {
|
||||
let exe_path = std::env::current_exe().unwrap_or_else(|_| PathBuf::from("."));
|
||||
let exe_dir = exe_path.parent().unwrap_or_else(|| std::path::Path::new("."));
|
||||
exe_dir.join("rlogg_config.json")
|
||||
}
|
||||
|
||||
pub fn load() -> Self {
|
||||
let path = Self::config_path();
|
||||
if let Ok(contents) = fs::read_to_string(&path) {
|
||||
serde_json::from_str(&contents).unwrap_or_default()
|
||||
} else {
|
||||
Self::default()
|
||||
}
|
||||
}
|
||||
|
||||
pub fn save(&self) {
|
||||
let path = Self::config_path();
|
||||
if let Ok(json) = serde_json::to_string_pretty(self) {
|
||||
let _ = fs::write(&path, json);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user