big ass refactor (rip client)

This commit is contained in:
🪞👃🪞 2024-07-03 14:51:48 +03:00
parent 94c1f83ef2
commit 8c3cf53c67
56 changed files with 2232 additions and 1891 deletions

View file

@ -1,29 +1,85 @@
use crate::core::*;
use std::path::{Path, PathBuf};
use std::fs::{File, create_dir_all};
const CONFIG_FILE_NAME: &'static str = "tek.toml";
const PROJECT_FILE_NAME: &'static str = "project.toml";
pub fn create_dirs (xdg: &microxdg::XdgApp) -> Result<(), Box<dyn Error>> {
use std::{path::Path,fs::{File,create_dir_all}};
let config_dir = xdg.app_config()?;
if !Path::new(&config_dir).exists() {
println!("Creating {config_dir:?}");
create_dir_all(&config_dir)?;
}
let config_path = config_dir.join(CONFIG_FILE_NAME);
if !Path::new(&config_path).exists() {
println!("Creating {config_path:?}");
File::create_new(&config_path)?;
}
let data_dir = xdg.app_data()?;
if !Path::new(&data_dir).exists() {
println!("Creating {data_dir:?}");
create_dir_all(&data_dir)?;
}
let project_path = data_dir.join(PROJECT_FILE_NAME);
if !Path::new(&project_path).exists() {
println!("Creating {project_path:?}");
File::create_new(&project_path)?;
}
Ok(())
pub struct AppPaths {
config_dir: PathBuf,
config_file: PathBuf,
data_dir: PathBuf,
project_file: PathBuf,
}
impl AppPaths {
pub fn new (xdg: &XdgApp) -> Usually<Self> {
let config_dir = PathBuf::from(xdg.app_config()?);
let config_file = PathBuf::from(config_dir.join(CONFIG_FILE_NAME));
let data_dir = PathBuf::from(xdg.app_data()?);
let project_file = PathBuf::from(data_dir.join(PROJECT_FILE_NAME));
Ok(Self { config_dir, config_file, data_dir, project_file })
}
pub fn should_create (&self) -> bool {
for path in [
&self.config_dir,
&self.config_file,
&self.data_dir,
&self.project_file,
].iter() {
if !Path::new(path).exists() {
return true
}
}
false
}
pub fn create (&self) -> Usually<()> {
for dir in [&self.config_dir, &self.data_dir].iter() {
if !Path::new(dir).exists() {
create_dir_all(&dir)?;
}
}
for file in [&self.config_file, &self.project_file].iter() {
if !Path::new(file).exists() {
File::create_new(&file)?;
}
}
Ok(())
}
}
pub struct SetupModal(pub Option<Arc<XdgApp>>);
render!(SetupModal |self, buf, area| {
let lines = [
(" ", Style::default().white().on_black().not_dim().bold()),
(" Welcome to TEK. ", Style::default().white().on_black().not_dim().bold()),
(" ", Style::default().white().on_black().not_dim().bold()),
(" Press ENTER to create the ", Style::default().white().on_black().not_dim()),
(" following directories: ", Style::default().white().on_black().not_dim()),
(" ", Style::default().white().on_black().not_dim().bold()),
(" Configuration directory: ", Style::default().white().on_black().not_dim()),
(" ~/.config/tek ", Style::default().white().on_black().not_dim().bold()),
(" ", Style::default().white().on_black().not_dim()),
(" Data directory: ", Style::default().white().on_black().not_dim()),
(" ~/.local/share/tek ", Style::default().white().on_black().not_dim().bold()),
(" ", Style::default().white().on_black().not_dim()),
];
let width = lines[0].0.len() as u16;
let x = area.x + (area.width - width) / 2;
for (i, (line, style)) in lines.iter().enumerate() {
line.blit(buf, x, area.y + area.height / 2 - (lines.len() / 2) as u16 + i as u16, Some(*style));
}
Ok(area)
});
handle!(SetupModal |self, e| {
if let AppEvent::Input(::crossterm::event::Event::Key(KeyEvent {
code: KeyCode::Enter,
..
})) = e {
AppPaths::new(&self.0.as_ref().unwrap())?.create()?;
self.0 = None;
Ok(true)
} else {
Ok(false)
}
});