Skip to content

Commit

Permalink
Dynamically load directory content
Browse files Browse the repository at this point in the history
  • Loading branch information
fluxxcode committed Jan 14, 2024
1 parent 4a3a071 commit 9023fc3
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 12 deletions.
6 changes: 4 additions & 2 deletions examples/sandbox/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,11 @@ struct MyApp {

impl MyApp {
pub fn new(_cc: &eframe::CreationContext) -> Self {
Self {
let mut obj = Self {
file_explorer: FileExplorer::new()
}
};
obj.file_explorer.open();
obj
}
}

Expand Down
55 changes: 45 additions & 10 deletions src/file_explorer.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
use std::{fs, io};

pub struct FileExplorer {
directory_content: Vec<fs::DirEntry>,
search_value: String
}

Expand All @@ -11,7 +13,15 @@ impl Default for FileExplorer {

impl FileExplorer {
pub fn new() -> Self {
FileExplorer { search_value: String::new() }
FileExplorer {
directory_content: vec![],
search_value: String::new() }
}

// TODO: Enable option to set initial directory
pub fn open(&mut self) {
// TODO: Error handling
let _ = self.load_directory("./");
}

pub fn update(&mut self, ctx: &egui::Context) {
Expand Down Expand Up @@ -146,15 +156,40 @@ impl FileExplorer {
}

fn update_central_panel(&mut self, ui: &mut egui::Ui) {
// NOTE: These are currently only hardcoded test values!
let _ = ui.selectable_label(false, "🗀 projects");
let _ = ui.selectable_label(false, "🗀 documents");
let _ = ui.selectable_label(false, "🗀 images");
let _ = ui.selectable_label(false, "🗀 music");

let _ = ui.selectable_label(false, "🖹 some_config.txt");
let _ = ui.selectable_label(false, "🖹 README.md");
let _ = ui.selectable_label(false, "🖹 LICENSE.md");
for item in self.directory_content.iter() {
let path = item.path();

let icon = match path.is_dir() {
true => "🗀",
_ => "🖹"
};

// Is there a way to write this better?
let file_name = match path.file_name() {
Some(x) => {
match x.to_str() {
Some(v) => v,
_ => continue
}
},
_ => continue
};

let _ = ui.selectable_label(false, format!("{} {}", icon, file_name));
}
}

fn load_directory(&mut self, path: &str) -> io::Result<()> {
let paths = fs::read_dir(path)?;

self.directory_content.clear();
for path in paths {
match path {
Ok(entry) => self.directory_content.push(entry),
_ => continue
};
}

Ok(())
}
}

0 comments on commit 9023fc3

Please sign in to comment.