feel better : optimized tree display
This commit is contained in:
parent
7fabb8a9ec
commit
f8e450cb3b
|
@ -2,7 +2,6 @@ use std::ffi::OsStr;
|
||||||
use std::fs;
|
use std::fs;
|
||||||
use std::io;
|
use std::io;
|
||||||
use std::path::{Path, PathBuf};
|
use std::path::{Path, PathBuf};
|
||||||
//use eframe::egui;
|
|
||||||
|
|
||||||
use crate::tools;
|
use crate::tools;
|
||||||
use crate::ALLOWED_FILE_EXTENSIONS;
|
use crate::ALLOWED_FILE_EXTENSIONS;
|
||||||
|
@ -25,15 +24,6 @@ impl File {
|
||||||
folder_open: false,
|
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,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
131
src/tools/mod.rs
131
src/tools/mod.rs
|
@ -5,8 +5,8 @@ use egui::Color32;
|
||||||
use image::GenericImageView;
|
use image::GenericImageView;
|
||||||
use serde::{Deserialize, Serialize};
|
use serde::{Deserialize, Serialize};
|
||||||
use std::{
|
use std::{
|
||||||
cmp::Ordering, error::Error, ffi::OsStr, fs, fs::read_to_string, fs::OpenOptions, io::Write,
|
cmp::Ordering, error::Error, ffi::OsStr, fs, fs::read_to_string, fs::OpenOptions, io::Write,
|
||||||
path::Component, path::Path, path::PathBuf,
|
path::Component, path::Path, path::PathBuf,
|
||||||
};
|
};
|
||||||
|
|
||||||
//my tools;
|
//my tools;
|
||||||
|
@ -14,6 +14,7 @@ pub mod confirm;
|
||||||
pub mod search;
|
pub mod search;
|
||||||
pub mod settings;
|
pub mod settings;
|
||||||
pub mod shortcuts;
|
pub mod shortcuts;
|
||||||
|
pub mod file_tree;
|
||||||
|
|
||||||
pub mod terminal;
|
pub mod terminal;
|
||||||
pub use terminal::*;
|
pub use terminal::*;
|
||||||
|
@ -23,99 +24,99 @@ pub use tabs::*;
|
||||||
|
|
||||||
#[derive(Serialize, Deserialize, Debug, PartialEq, Default)]
|
#[derive(Serialize, Deserialize, Debug, PartialEq, Default)]
|
||||||
pub struct AppState {
|
pub struct AppState {
|
||||||
pub tabs: Vec<PathBuf>,
|
pub tabs: Vec<PathBuf>,
|
||||||
pub theme: usize,
|
pub theme: usize,
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn save_state(state: &AppState, file_path: &str) -> Result<(), std::io::Error> {
|
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() {
|
if let Some(parent_dir) = Path::new(file_path).parent() {
|
||||||
fs::create_dir_all(parent_dir)?;
|
fs::create_dir_all(parent_dir)?;
|
||||||
}
|
}
|
||||||
|
|
||||||
let mut file = OpenOptions::new()
|
let mut file = OpenOptions::new()
|
||||||
.write(true)
|
.write(true)
|
||||||
.create(true)
|
.create(true)
|
||||||
.truncate(true)
|
.truncate(true)
|
||||||
.open(file_path)?;
|
.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> {
|
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>> {
|
pub fn load_icon() -> Result<egui::IconData, Box<dyn Error>> {
|
||||||
let (icon_rgba, icon_width, icon_height) = {
|
let (icon_rgba, icon_width, icon_height) = {
|
||||||
let icon = include_bytes!("../../assets/icon.png");
|
let icon = include_bytes!("../../assets/icon.png");
|
||||||
let image = image::load_from_memory(icon)?;
|
let image = image::load_from_memory(icon)?;
|
||||||
let rgba = image.clone().into_rgba8().to_vec();
|
let rgba = image.clone().into_rgba8().to_vec();
|
||||||
let (width, height) = image.dimensions();
|
let (width, height) = image.dimensions();
|
||||||
(rgba, width, height)
|
(rgba, width, height)
|
||||||
};
|
};
|
||||||
|
|
||||||
Ok(egui::IconData {
|
Ok(egui::IconData {
|
||||||
rgba: icon_rgba,
|
rgba: icon_rgba,
|
||||||
width: icon_width,
|
width: icon_width,
|
||||||
height: icon_height,
|
height: icon_height,
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn to_syntax(language: &str) -> Syntax {
|
pub fn to_syntax(language: &str) -> Syntax {
|
||||||
match language {
|
match language {
|
||||||
"py" => Syntax::python(),
|
"py" => Syntax::python(),
|
||||||
"rs" => Syntax::rust(),
|
"rs" => Syntax::rust(),
|
||||||
_ => Syntax::shell(),
|
_ => Syntax::shell(),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn sort_directories_first(a: &std::fs::DirEntry, b: &std::fs::DirEntry) -> Ordering {
|
pub fn sort_directories_first(a: &std::fs::DirEntry, b: &std::fs::DirEntry) -> Ordering {
|
||||||
let a_is_dir = a.path().is_dir();
|
let a_is_dir = a.path().is_dir();
|
||||||
let b_is_dir = b.path().is_dir();
|
let b_is_dir = b.path().is_dir();
|
||||||
|
|
||||||
// Directories come first, then files
|
// Directories come first, then files
|
||||||
if a_is_dir && !b_is_dir {
|
if a_is_dir && !b_is_dir {
|
||||||
Ordering::Less
|
Ordering::Less
|
||||||
} else if !a_is_dir && b_is_dir {
|
} else if !a_is_dir && b_is_dir {
|
||||||
Ordering::Greater
|
Ordering::Greater
|
||||||
} else {
|
} else {
|
||||||
// Both are either directories or files, sort alphabetically
|
// Both are either directories or files, sort alphabetically
|
||||||
a.path().cmp(&b.path())
|
a.path().cmp(&b.path())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn format_path(path: &Path) -> String {
|
pub fn format_path(path: &Path) -> String {
|
||||||
let components: Vec<&OsStr> = path
|
let components: Vec<&OsStr> = path
|
||||||
.components()
|
.components()
|
||||||
.rev()
|
.rev()
|
||||||
.take(DISPLAY_PATH_DEPTH)
|
.take(DISPLAY_PATH_DEPTH)
|
||||||
.filter_map(|component| match component {
|
.filter_map(|component| match component {
|
||||||
Component::RootDir | Component::CurDir => None,
|
Component::RootDir | Component::CurDir => None,
|
||||||
_ => Some(component.as_os_str()),
|
_ => Some(component.as_os_str()),
|
||||||
})
|
})
|
||||||
.collect();
|
.collect();
|
||||||
|
|
||||||
format!(
|
format!(
|
||||||
"{}>",
|
"{}>",
|
||||||
components
|
components
|
||||||
.iter()
|
.iter()
|
||||||
.rev()
|
.rev()
|
||||||
.map(|&c| c.to_string_lossy())
|
.map(|&c| c.to_string_lossy())
|
||||||
.collect::<Vec<_>>()
|
.collect::<Vec<_>>()
|
||||||
.join("/")
|
.join("/")
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn hex_str_to_color(hex_str: &str) -> Color32 {
|
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)]
|
#[cfg(test)]
|
||||||
|
|
Loading…
Reference in a new issue