feel better : optimized tree display

This commit is contained in:
Penwing 2024-01-27 22:16:25 +01:00
parent 7fabb8a9ec
commit f8e450cb3b
2 changed files with 66 additions and 75 deletions

View file

@ -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,
}
}
}

View file

@ -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<PathBuf>,
pub theme: usize,
pub tabs: Vec<PathBuf>,
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<AppState, std::io::Error> {
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<egui::IconData, Box<dyn Error>> {
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::<Vec<_>>()
.join("/")
)
format!(
"{}>",
components
.iter()
.rev()
.map(|&c| c.to_string_lossy())
.collect::<Vec<_>>()
.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)]