added tree

This commit is contained in:
Penwing 2024-01-17 16:20:22 +01:00
parent 1eb22767d7
commit fad1a9b107

View file

@ -2,16 +2,15 @@
//mod tools; //mod tools;
use eframe::egui; use eframe::egui;
use std::fs;
use std::path::Path; use std::path::Path;
use std::fs;
use std::io;
use std::process::Command; use std::process::Command;
const TERMINAL_HEIGHT : f32 = 200.0; const TERMINAL_HEIGHT : f32 = 200.0;
fn main() -> Result<(), eframe::Error> { fn main() -> Result<(), eframe::Error> {
//tools::code_editor::linked();
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()
@ -26,6 +25,7 @@ fn main() -> Result<(), eframe::Error> {
) )
} }
fn run_command(cmd : String) -> String { fn run_command(cmd : String) -> String {
let command = "> ".to_owned() + &cmd.clone() + "\n"; let command = "> ".to_owned() + &cmd.clone() + "\n";
let output = Command::new("sh") let output = Command::new("sh")
@ -37,6 +37,26 @@ fn run_command(cmd : String) -> String {
} }
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 paths = fs::read_dir(&path).expect("Failed to read dir");
for path_result in paths {
let result = path_result.expect("Failed to get path");
let full_path = result.path();
let _ = list_files(ui, &full_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,
@ -51,7 +71,7 @@ impl Default for MyApp {
Self { Self {
picked_path: None, picked_path: None,
language: "rs".into(), language: "rs".into(),
code: "// A very simple example\nfn main() {\n\tprintln!(\"Hello world!\");\n}\n".into(), code: "// write here".into(),
command: "".into(), command: "".into(),
command_history: "Welcome master".into(), command_history: "Welcome master".into(),
} }
@ -61,14 +81,24 @@ impl Default for MyApp {
impl eframe::App for MyApp { impl eframe::App for MyApp {
fn update(&mut self, ctx: &egui::Context, _frame: &mut eframe::Frame) { fn update(&mut self, ctx: &egui::Context, _frame: &mut eframe::Frame) {
self.draw_tree_panel(ctx);
self.draw_terminal_panel(ctx);
self.draw_code_panel(ctx);
}
}
//tree panel impl MyApp {
egui::SidePanel::left("tree").show(ctx, |ui| { fn draw_tree_panel(&self, ctx: &egui::Context) {
ui.label("Tree ?"); egui::SidePanel::left("file_tree_panel").show(ctx, |ui| {
ui.heading("File Tree");
let _ = list_files(ui, Path::new("/home/penwing/Documents/notes/"));
}); });
}
//terminal panel fn draw_terminal_panel(&mut self, ctx: &egui::Context) {
egui::TopBottomPanel::bottom("terminal").exact_height(TERMINAL_HEIGHT.clone()).show(ctx, |ui| { egui::TopBottomPanel::bottom("terminal")
.exact_height(TERMINAL_HEIGHT.clone())
.show(ctx, |ui| {
ui.with_layout(egui::Layout::bottom_up(egui::Align::LEFT), |ui| { ui.with_layout(egui::Layout::bottom_up(egui::Align::LEFT), |ui| {
ui.label(""); ui.label("");
ui.horizontal(|ui| { ui.horizontal(|ui| {
@ -83,18 +113,22 @@ impl eframe::App for MyApp {
} }
}); });
egui::ScrollArea::vertical().show(ui, |ui| { egui::ScrollArea::vertical().show(ui, |ui| {
ui.separator();
ui.label(self.command_history.clone()); ui.label(self.command_history.clone());
}); });
}); });
}); });
}
//code panel fn draw_code_panel(&mut self, ctx: &egui::Context) {
egui::CentralPanel::default().show(ctx, |ui| { egui::CentralPanel::default().show(ctx, |ui| {
if ui.button("Open file…").clicked() { if ui.button("Open file…").clicked() {
if let Some(path) = rfd::FileDialog::new().pick_file() { if let Some(path) = rfd::FileDialog::new().pick_file() {
self.picked_path = Some(path.display().to_string()); self.picked_path = Some(path.display().to_string());
let file_path = Path::new(self.picked_path.as_deref().unwrap_or_default()); let file_path = Path::new(self.picked_path.as_deref().unwrap_or_default());
self.code = fs::read_to_string(file_path).expect("Should have been able to read the file"); self.code = fs::read_to_string(file_path).expect("Should have been able to read the file");
self.language = file_path.to_str().unwrap().split('.').last().unwrap().into();
println!("{}", self.language);
} }
} }
@ -105,7 +139,6 @@ impl eframe::App for MyApp {
}); });
} }
let Self { language, code, .. } = self; let Self { language, code, .. } = self;
let theme = egui_extras::syntax_highlighting::CodeTheme::from_memory(ui.ctx()); let theme = egui_extras::syntax_highlighting::CodeTheme::from_memory(ui.ctx());
let mut layouter = |ui: &egui::Ui, string: &str, wrap_width: f32| { let mut layouter = |ui: &egui::Ui, string: &str, wrap_width: f32| {
@ -121,7 +154,7 @@ impl eframe::App for MyApp {
.font(egui::FontId::monospace(60.0)) // for cursor height .font(egui::FontId::monospace(60.0)) // for cursor height
.code_editor() .code_editor()
.lock_focus(true) .lock_focus(true)
.desired_rows(20) .desired_rows(80)
.lock_focus(true) .lock_focus(true)
.desired_width(f32::INFINITY) .desired_width(f32::INFINITY)
.layouter(&mut layouter), .layouter(&mut layouter),