better organisation

This commit is contained in:
Penwing 2024-01-17 21:03:41 +01:00
parent 048554a7b2
commit 9b96cb69b2
2 changed files with 58 additions and 72 deletions

View file

@ -1,17 +1,15 @@
//mod tools; mod tools;
use eframe::egui; use eframe::egui;
use std::path::Path; use std::path::Path;
use std::fs; use std::fs;
use std::io;
use std::process::Command;
use std::cmp::Ordering;
const TERMINAL_HEIGHT : f32 = 200.0; const TERMINAL_HEIGHT : f32 = 200.0;
fn main() -> Result<(), eframe::Error> { fn main() -> Result<(), eframe::Error> {
tools::loaded();
env_logger::init(); // Log to stderr (if you run with `RUST_LOG=debug`). env_logger::init(); // Log to stderr (if you run with `RUST_LOG=debug`).
let options = eframe::NativeOptions { let options = eframe::NativeOptions {
viewport: egui::ViewportBuilder::default() viewport: egui::ViewportBuilder::default()
@ -27,54 +25,6 @@ fn main() -> Result<(), eframe::Error> {
} }
fn run_command(cmd : String) -> String {
let command = "> ".to_owned() + &cmd.clone() + "\n";
let output = Command::new("sh")
.arg("-c")
.arg(cmd)
.output()
.expect("failed to execute process");
(command + &String::from_utf8_lossy(&output.stdout)).to_string()
}
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();
// 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())
}
}
fn list_files(ui: &mut egui::Ui, path: &Path) -> io::Result<()> {
if let Some(name) = path.file_name() {
if path.is_dir() {
egui::CollapsingHeader::new(name.to_string_lossy()).show(ui, |ui| {
let mut paths: Vec<_> = fs::read_dir(&path).expect("Failed to read dir").map(|r| r.unwrap()).collect();
// Sort the vector using the custom sorting function
paths.sort_by(|a, b| sort_directories_first(a, b));
for result in paths {
//let result = path_result.expect("Failed to get path");
//let full_path = result.path();
let _ = list_files(ui, &result.path());
}
});
} else {
ui.label(name.to_string_lossy());
}
}
Ok(())
}
struct MyApp { struct MyApp {
picked_path: Option<String>, picked_path: Option<String>,
language: String, language: String,
@ -109,7 +59,7 @@ impl MyApp {
fn draw_tree_panel(&self, ctx: &egui::Context) { fn draw_tree_panel(&self, ctx: &egui::Context) {
egui::SidePanel::left("file_tree_panel").show(ctx, |ui| { egui::SidePanel::left("file_tree_panel").show(ctx, |ui| {
ui.heading("Bookshelves"); ui.heading("Bookshelves");
let _ = list_files(ui, Path::new("/home/penwing/Documents/")); let _ = tools::list_files(ui, Path::new("/home/penwing/Documents/"));
ui.separator(); ui.separator();
}); });
} }
@ -126,7 +76,7 @@ impl MyApp {
let response = ui.add(egui::TextEdit::singleline(command).desired_width(f32::INFINITY).lock_focus(true)); let response = ui.add(egui::TextEdit::singleline(command).desired_width(f32::INFINITY).lock_focus(true));
if response.lost_focus() && ctx.input(|i| i.key_pressed(egui::Key::Enter)) { if response.lost_focus() && ctx.input(|i| i.key_pressed(egui::Key::Enter)) {
self.command_history.push_str(&("\n".to_string() + &run_command(self.command.clone()))); self.command_history.push_str(&("\n".to_string() + &tools::run_command(self.command.clone())));
self.command = "".into(); self.command = "".into();
response.request_focus(); response.request_focus();
} }

View file

@ -1,25 +1,61 @@
use eframe::egui; use eframe::egui;
use std::io;
//pub mod code_editor; use std::process::Command;
use std::cmp::Ordering;
use std::path::Path;
use std::fs;
// ---------------------------------------------------------------------------- pub fn loaded() {
println!("Tools loaded");
/// Something to view in the demo windows
pub trait View {
fn ui(&mut self, ui: &mut egui::Ui);
} }
/// Something to view
pub trait Demo { pub fn run_command(cmd : String) -> String {
/// Is the demo enabled for this integraton? let command = "> ".to_owned() + &cmd.clone() + "\n";
fn is_enabled(&self, _ctx: &egui::Context) -> bool { let output = Command::new("sh")
true .arg("-c")
.arg(cmd)
.output()
.expect("failed to execute process");
(command + &String::from_utf8_lossy(&output.stdout)).to_string()
}
pub fn list_files(ui: &mut egui::Ui, path: &Path) -> io::Result<()> {
if let Some(name) = path.file_name() {
if path.is_dir() {
egui::CollapsingHeader::new(name.to_string_lossy()).show(ui, |ui| {
let mut paths: Vec<_> = fs::read_dir(&path).expect("Failed to read dir").map(|r| r.unwrap()).collect();
// Sort the vector using the custom sorting function
paths.sort_by(|a, b| sort_directories_first(a, b));
for result in paths {
//let result = path_result.expect("Failed to get path");
//let full_path = result.path();
let _ = list_files(ui, &result.path());
}
});
} else {
ui.label(name.to_string_lossy());
}
}
Ok(())
}
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();
// 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())
} }
/// `&'static` so we can also use it as a key to store open/close state.
fn name(&self) -> &'static str;
/// Show windows, etc
fn show(&mut self, ctx: &egui::Context, open: &mut bool);
} }