Change to use json for config
cleanup unused codes
This commit is contained in:
@@ -5,6 +5,7 @@ use clap::{command, Args, Command, Parser, Subcommand};
|
||||
use color_eyre::Result;
|
||||
use ratatui::crossterm;
|
||||
use std::path::PathBuf;
|
||||
use color_eyre::eyre::eyre;
|
||||
|
||||
#[derive(Parser, Debug)]
|
||||
struct FolderAddCommand {
|
||||
@@ -111,11 +112,15 @@ impl FolderAddCommand {
|
||||
let mut config = ApplicationConfig::from_file(&APP_CONIFG_FILE_PATH.to_path_buf())?;
|
||||
let path = PathBuf::from(&self.path);
|
||||
let abs_path = path.canonicalize()?;
|
||||
if !abs_path.is_dir() {
|
||||
return Err(eyre!("{:?} is not a directory", abs_path));
|
||||
}
|
||||
config
|
||||
.path_config
|
||||
.dlsite_paths
|
||||
.push(abs_path.to_str().unwrap().to_string());
|
||||
config.save()?;
|
||||
println!("Added {:?} to path config", abs_path);
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,32 +1,16 @@
|
||||
use crate::config::types::{ApplicationConfig, BasicConfig, PathConfig};
|
||||
use crate::constants::{APP_CONIFG_FILE_PATH, APP_DATA_DIR};
|
||||
use color_eyre::Result;
|
||||
use ini::Ini;
|
||||
use std::path::PathBuf;
|
||||
use serde_json;
|
||||
|
||||
pub mod types;
|
||||
|
||||
impl ApplicationConfig {
|
||||
pub fn from_file(path: &PathBuf) -> Result<Self> {
|
||||
let conf = Ini::load_from_file(path)?;
|
||||
let basic_conf_section = conf.section(Some("Basic")).unwrap();
|
||||
let basic_conf = BasicConfig {
|
||||
db_path: basic_conf_section.get("DBPath").unwrap().to_string(),
|
||||
tick_rate: basic_conf_section.get("TickRate").unwrap().parse()?,
|
||||
};
|
||||
let path_conf_section = conf.section(Some("Path")).unwrap();
|
||||
let path_conf = PathConfig {
|
||||
dlsite_paths: path_conf_section
|
||||
.get("DLSite")
|
||||
.unwrap()
|
||||
.split(":")
|
||||
.map(|s| s.to_string())
|
||||
.collect(),
|
||||
};
|
||||
Ok(Self {
|
||||
basic_config: basic_conf,
|
||||
path_config: path_conf,
|
||||
})
|
||||
let reader = std::fs::File::open(path)?;
|
||||
let result = serde_json::from_reader(reader)?;
|
||||
Ok(result)
|
||||
}
|
||||
|
||||
pub fn new() -> Self {
|
||||
@@ -51,21 +35,10 @@ impl ApplicationConfig {
|
||||
}
|
||||
|
||||
fn write_to_file(self, path: &PathBuf) -> Result<()> {
|
||||
let mut conf = Ini::new();
|
||||
conf.with_section(Some("Basic"))
|
||||
.set("DBPath", self.basic_config.db_path)
|
||||
.set("TickRate", self.basic_config.tick_rate.to_string());
|
||||
conf.with_section(Some("Path")).set(
|
||||
"DLSite",
|
||||
self.path_config
|
||||
.dlsite_paths
|
||||
.into_iter()
|
||||
.filter(|x| !x.is_empty())
|
||||
.collect::<Vec<String>>()
|
||||
.join(":"),
|
||||
);
|
||||
conf.write_to_file(path)?;
|
||||
Ok(())
|
||||
let writer = std::fs::File::create(path)?;
|
||||
let result = serde_json::to_writer_pretty(writer, &self)?;
|
||||
Ok(result)
|
||||
|
||||
}
|
||||
|
||||
pub fn save(&self) -> Result<()> {
|
||||
|
||||
@@ -1,16 +1,18 @@
|
||||
#[derive(Clone)]
|
||||
use serde::{Deserialize, Serialize};
|
||||
|
||||
#[derive(Clone, Debug, Serialize, Deserialize)]
|
||||
pub(crate) struct ApplicationConfig {
|
||||
pub(crate) basic_config: BasicConfig,
|
||||
pub(crate) path_config: PathConfig,
|
||||
}
|
||||
|
||||
#[derive(Clone)]
|
||||
#[derive(Clone, Debug, Serialize, Deserialize)]
|
||||
pub(crate) struct BasicConfig {
|
||||
pub(crate) db_path: String,
|
||||
pub(crate) tick_rate: u64,
|
||||
}
|
||||
|
||||
#[derive(Clone)]
|
||||
#[derive(Clone, Debug, Serialize, Deserialize)]
|
||||
pub(crate) struct PathConfig {
|
||||
pub(crate) dlsite_paths: Vec<String>,
|
||||
}
|
||||
|
||||
@@ -9,5 +9,5 @@ lazy_static! {
|
||||
BASE_DIRS.config_dir().to_path_buf().join(APP_DIR_NAME);
|
||||
pub static ref APP_DATA_DIR: PathBuf = BASE_DIRS.data_dir().to_path_buf().join(APP_DIR_NAME);
|
||||
pub static ref APP_CACHE_PATH: PathBuf = BASE_DIRS.cache_dir().to_path_buf().join(APP_DIR_NAME);
|
||||
pub static ref APP_CONIFG_FILE_PATH: PathBuf = APP_CONFIG_DIR.clone().join("config.ini");
|
||||
pub static ref APP_CONIFG_FILE_PATH: PathBuf = APP_CONFIG_DIR.clone().join("config.json");
|
||||
}
|
||||
@@ -1 +0,0 @@
|
||||
|
||||
|
||||
@@ -6,7 +6,7 @@ mod crawler;
|
||||
mod event;
|
||||
mod helpers;
|
||||
mod schema;
|
||||
mod types;
|
||||
mod models;
|
||||
mod widgets;
|
||||
|
||||
use crate::cli::Cli;
|
||||
@@ -19,4 +19,4 @@ async fn main() -> Result<()> {
|
||||
color_eyre::install()?;
|
||||
let cli = Cli::parse();
|
||||
cli.run().await
|
||||
}
|
||||
}
|
||||
@@ -91,16 +91,14 @@ impl TextArea {
|
||||
pub fn new(title: &str,
|
||||
placeholder_text: &str,
|
||||
validate_fn: fn(&str) -> bool,
|
||||
style: Option<TextAreaStyle>,
|
||||
auto_scroll: Option<bool>,
|
||||
)
|
||||
-> Self
|
||||
{
|
||||
let func = Arc::new(validate_fn);
|
||||
Self {
|
||||
title: title.to_string(),
|
||||
style: style.unwrap_or(TextAreaStyle::SingleLine),
|
||||
auto_scroll: auto_scroll.unwrap_or(true),
|
||||
style: TextAreaStyle::SingleLine,
|
||||
auto_scroll: true,
|
||||
validate_fn: func,
|
||||
state: TextAreaState {
|
||||
input: Input::new(placeholder_text.to_string()),
|
||||
@@ -112,6 +110,16 @@ impl TextArea {
|
||||
}
|
||||
}
|
||||
|
||||
pub fn display_style(mut self, style: TextAreaStyle) -> Self {
|
||||
self.style = style;
|
||||
self
|
||||
}
|
||||
|
||||
pub fn set_auto_scroll(mut self, auto_scroll: bool) -> Self {
|
||||
self.auto_scroll = auto_scroll;
|
||||
self
|
||||
}
|
||||
|
||||
pub fn handle_input(&mut self, event: &Event) -> Result<()> {
|
||||
let _ = self.state.input.handle_event(event);
|
||||
self.state.is_valid = (self.validate_fn)(self.state.input.value());
|
||||
|
||||
@@ -18,9 +18,7 @@ impl AddFolderPopup {
|
||||
|x| {
|
||||
let path = Path::new(x);
|
||||
path.exists() && path.is_dir()
|
||||
},
|
||||
None,
|
||||
None
|
||||
}
|
||||
);
|
||||
textarea.state.is_active = true;
|
||||
Self { textarea }
|
||||
|
||||
@@ -2,9 +2,8 @@ mod main_view;
|
||||
|
||||
use crossterm::event::{Event, KeyEvent};
|
||||
pub use main_view::MainView;
|
||||
use std::any::Any;
|
||||
|
||||
pub trait View: Any + 'static {
|
||||
pub trait View {
|
||||
fn handle_input(&mut self, event: &Event) -> color_eyre::Result<()>;
|
||||
fn handle_key_input(&mut self, key: &KeyEvent) -> color_eyre::Result<()>;
|
||||
fn is_running(&self) -> bool;
|
||||
|
||||
Reference in New Issue
Block a user