From f8e450cb3bdb9bb68cb20b49dc4db8f98adcbeb9 Mon Sep 17 00:00:00 2001 From: Penwing Date: Sat, 27 Jan 2024 22:16:25 +0100 Subject: [PATCH] feel better : optimized tree display --- src/tools/file_tree.rs | 10 ---- src/tools/mod.rs | 131 +++++++++++++++++++++-------------------- 2 files changed, 66 insertions(+), 75 deletions(-) diff --git a/src/tools/file_tree.rs b/src/tools/file_tree.rs index db56ec2..647d118 100644 --- a/src/tools/file_tree.rs +++ b/src/tools/file_tree.rs @@ -2,7 +2,6 @@ use std::ffi::OsStr; use std::fs; use std::io; use std::path::{Path, PathBuf}; -//use eframe::egui; use crate::tools; use crate::ALLOWED_FILE_EXTENSIONS; @@ -25,15 +24,6 @@ impl File { folder_open: false, } } - - pub fn empty() -> Self { - Self { - name: "No file found".into(), - path: Path::new("/").to_path_buf(), - folder_content: None, - folder_open: false, - } - } } diff --git a/src/tools/mod.rs b/src/tools/mod.rs index 47c6c0e..92a3133 100644 --- a/src/tools/mod.rs +++ b/src/tools/mod.rs @@ -5,8 +5,8 @@ use egui::Color32; use image::GenericImageView; use serde::{Deserialize, Serialize}; use std::{ - cmp::Ordering, error::Error, ffi::OsStr, fs, fs::read_to_string, fs::OpenOptions, io::Write, - path::Component, path::Path, path::PathBuf, + cmp::Ordering, error::Error, ffi::OsStr, fs, fs::read_to_string, fs::OpenOptions, io::Write, + path::Component, path::Path, path::PathBuf, }; //my tools; @@ -14,6 +14,7 @@ pub mod confirm; pub mod search; pub mod settings; pub mod shortcuts; +pub mod file_tree; pub mod terminal; pub use terminal::*; @@ -23,99 +24,99 @@ pub use tabs::*; #[derive(Serialize, Deserialize, Debug, PartialEq, Default)] pub struct AppState { - pub tabs: Vec, - pub theme: usize, + pub tabs: Vec, + pub theme: usize, } pub fn save_state(state: &AppState, file_path: &str) -> Result<(), std::io::Error> { - let serialized_state = serde_json::to_string(state)?; + let serialized_state = serde_json::to_string(state)?; - if let Some(parent_dir) = Path::new(file_path).parent() { - fs::create_dir_all(parent_dir)?; - } + if let Some(parent_dir) = Path::new(file_path).parent() { + fs::create_dir_all(parent_dir)?; + } - let mut file = OpenOptions::new() - .write(true) - .create(true) - .truncate(true) - .open(file_path)?; + let mut file = OpenOptions::new() + .write(true) + .create(true) + .truncate(true) + .open(file_path)?; - file.write_all(serialized_state.as_bytes())?; + file.write_all(serialized_state.as_bytes())?; - println!("Saved state at {}", file_path); + println!("Saved state at {}", file_path); - Ok(()) + Ok(()) } pub fn load_state(file_path: &str) -> Result { - let serialized_state = read_to_string(file_path)?; + let serialized_state = read_to_string(file_path)?; - Ok(serde_json::from_str(&serialized_state)?) + Ok(serde_json::from_str(&serialized_state)?) } pub fn load_icon() -> Result> { - let (icon_rgba, icon_width, icon_height) = { - let icon = include_bytes!("../../assets/icon.png"); - let image = image::load_from_memory(icon)?; - let rgba = image.clone().into_rgba8().to_vec(); - let (width, height) = image.dimensions(); - (rgba, width, height) - }; + let (icon_rgba, icon_width, icon_height) = { + let icon = include_bytes!("../../assets/icon.png"); + let image = image::load_from_memory(icon)?; + let rgba = image.clone().into_rgba8().to_vec(); + let (width, height) = image.dimensions(); + (rgba, width, height) + }; - Ok(egui::IconData { - rgba: icon_rgba, - width: icon_width, - height: icon_height, - }) + Ok(egui::IconData { + rgba: icon_rgba, + width: icon_width, + height: icon_height, + }) } pub fn to_syntax(language: &str) -> Syntax { - match language { - "py" => Syntax::python(), - "rs" => Syntax::rust(), - _ => Syntax::shell(), - } + match language { + "py" => Syntax::python(), + "rs" => Syntax::rust(), + _ => Syntax::shell(), + } } pub fn sort_directories_first(a: &std::fs::DirEntry, b: &std::fs::DirEntry) -> Ordering { - let a_is_dir = a.path().is_dir(); - let b_is_dir = b.path().is_dir(); + let a_is_dir = a.path().is_dir(); + let b_is_dir = b.path().is_dir(); - // Directories come first, then files - if a_is_dir && !b_is_dir { - Ordering::Less - } else if !a_is_dir && b_is_dir { - Ordering::Greater - } else { - // Both are either directories or files, sort alphabetically - a.path().cmp(&b.path()) - } + // Directories come first, then files + if a_is_dir && !b_is_dir { + Ordering::Less + } else if !a_is_dir && b_is_dir { + Ordering::Greater + } else { + // Both are either directories or files, sort alphabetically + a.path().cmp(&b.path()) + } } pub fn format_path(path: &Path) -> String { - let components: Vec<&OsStr> = path - .components() - .rev() - .take(DISPLAY_PATH_DEPTH) - .filter_map(|component| match component { - Component::RootDir | Component::CurDir => None, - _ => Some(component.as_os_str()), - }) - .collect(); + let components: Vec<&OsStr> = path + .components() + .rev() + .take(DISPLAY_PATH_DEPTH) + .filter_map(|component| match component { + Component::RootDir | Component::CurDir => None, + _ => Some(component.as_os_str()), + }) + .collect(); - format!( - "{}>", - components - .iter() - .rev() - .map(|&c| c.to_string_lossy()) - .collect::>() - .join("/") - ) + format!( + "{}>", + components + .iter() + .rev() + .map(|&c| c.to_string_lossy()) + .collect::>() + .join("/") + ) } pub fn hex_str_to_color(hex_str: &str) -> Color32 { - Color32::from_hex(hex_str).unwrap_or_else(|_| Color32::BLACK) + Color32::from_hex(hex_str).unwrap_or_else(|_| Color32::BLACK) } #[cfg(test)]