Add file operations and UI components for file management application
This commit is contained in:
48
src/config.rs
Normal file
48
src/config.rs
Normal file
@@ -0,0 +1,48 @@
|
||||
use serde::{Deserialize, Serialize};
|
||||
use std::path::PathBuf;
|
||||
|
||||
#[derive(Serialize, Deserialize, Default, Clone)]
|
||||
pub struct Config {
|
||||
pub favorites: Vec<String>,
|
||||
}
|
||||
|
||||
impl Config {
|
||||
fn config_path() -> Option<PathBuf> {
|
||||
dirs::config_dir().map(|d| d.join("tui-fm").join("config.json"))
|
||||
}
|
||||
|
||||
pub fn load() -> Self {
|
||||
if let Some(path) = Self::config_path() {
|
||||
if let Ok(data) = std::fs::read_to_string(&path) {
|
||||
if let Ok(config) = serde_json::from_str::<Config>(&data) {
|
||||
return config;
|
||||
}
|
||||
}
|
||||
}
|
||||
Config::default()
|
||||
}
|
||||
|
||||
pub fn save(&self) {
|
||||
if let Some(path) = Self::config_path() {
|
||||
if let Some(parent) = path.parent() {
|
||||
let _ = std::fs::create_dir_all(parent);
|
||||
}
|
||||
if let Ok(data) = serde_json::to_string_pretty(self) {
|
||||
let _ = std::fs::write(&path, data);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub fn add_favorite(&mut self, path: &str) {
|
||||
let s = path.to_string();
|
||||
if !self.favorites.contains(&s) {
|
||||
self.favorites.push(s);
|
||||
self.save();
|
||||
}
|
||||
}
|
||||
|
||||
pub fn remove_favorite(&mut self, path: &str) {
|
||||
self.favorites.retain(|f| f != path);
|
||||
self.save();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user