fixed path

This commit is contained in:
Penwing 2024-01-27 23:14:39 +01:00
parent 16650a2ca1
commit 0f7a95bff3
3 changed files with 636 additions and 635 deletions

View file

@ -1,11 +1,10 @@
use eframe::egui;
use egui::{text::CCursor, text_edit::CCursorRange, Rangef};
use std::{cmp::max, env, path::Path, path::PathBuf};
use std::{cmp::max, env, path::PathBuf};// path::Path,
use crate::tools;
use crate::Calcifer;
use crate::MAX_TABS;
use crate::PATH_ROOT;
use crate::PROJECT_EXTENSION;
use tools::hex_str_to_color;
@ -23,7 +22,7 @@ impl Calcifer {
ui.vertical(|ui| {
if ui.add(egui::Button::new("📁")).clicked() {
if let Some(path) = rfd::FileDialog::new()
.set_directory(Path::new(&PATH_ROOT))
.set_directory(self.home.as_path())
.pick_file()
{
self.open_file(Some(&path));
@ -55,10 +54,21 @@ impl Calcifer {
return;
}
egui::SidePanel::left("file_tree_panel").show(ctx, |ui| {
ui.heading("Bookshelf");
ui.horizontal(|ui| {
ui.label("Bookshelf ");
if ui.add(egui::Button::new("📖")).clicked() {
self.file_tree = tools::file_tree::generate_file_tree(self.home.as_path(), 7);
}
});
ui.separator();
let _ = self.list_files(ui, Path::new(&PATH_ROOT));
let mut n_files : usize = 0;
if let Some(file_tree) = self.file_tree.clone() {
self.list_files(ui, &file_tree, 1, &mut n_files);
} else {
ui.label("No book on the Bookshelf");
}
ui.separator();
ui.label(format!("{} files displayed", n_files));
});
}
@ -196,7 +206,7 @@ impl Calcifer {
pub fn draw_content_panel(&mut self, ctx: &egui::Context) {
egui::CentralPanel::default().show(ctx, |ui| {
ui.horizontal(|ui| {
if ui.add(egui::Button::new("open in terminal")).clicked() {
if ui.add(egui::Button::new("open directory in terminal")).clicked() {
let mut path = self.tabs[self.selected_tab.to_index()].path.clone();
path.pop();
tools::send_command(format!("cd {}", path.display()));

View file

@ -1,15 +1,15 @@
use eframe::egui;
use egui::Color32;
use std::{cmp::min, ffi::OsStr, fs, io, path::Path, path::PathBuf};
use std::{cmp::min, fs, path::Path, path::PathBuf};
use crate::tools;
use crate::Calcifer;
use crate::DEFAULT_THEMES;
use crate::MAX_TABS;
use crate::PATH_ROOT;
use crate::SAVE_PATH;
use crate::TIME_LABELS;
use crate::save_path;
use tools::hex_str_to_color;
use tools::file_tree;
impl Calcifer {
pub fn handle_confirm(&mut self) {
@ -45,7 +45,7 @@ impl Calcifer {
pub fn save_tab_as(&self) -> Option<PathBuf> {
if let Some(path) = rfd::FileDialog::new()
.set_directory(Path::new(&PATH_ROOT))
.set_directory(self.home.as_path())
.save_file()
{
if let Err(err) = fs::write(&path, &self.tabs[self.selected_tab.to_index()].code) {
@ -107,7 +107,7 @@ impl Calcifer {
theme: state_theme,
};
let _ = tools::save_state(&app_state, SAVE_PATH);
let _ = tools::save_state(&app_state, save_path().as_path());
}
pub fn move_through_tabs(&mut self, forward: bool) {
@ -122,55 +122,6 @@ impl Calcifer {
self.selected_tab = tools::TabNumber::from_index(new_index);
}
pub fn list_files(&mut self, ui: &mut egui::Ui, path: &Path) -> io::Result<()> {
if path.file_name().is_none() {
return Ok(());
}
let name = path
.file_name()
.unwrap_or_else(|| OsStr::new(""))
.to_string_lossy()
.into_owned();
if !path.is_dir() {
if ui.button(name).clicked() {
self.open_file(Some(path));
}
return Ok(());
}
egui::CollapsingHeader::new(name).show(ui, |ui| match fs::read_dir(path) {
Err(err) => {
ui.label(format!("Error reading directory: {}", err));
}
Ok(entries) => {
let mut paths: Vec<Result<fs::DirEntry, io::Error>> = entries
.map(|r| r.map_err(|e| io::Error::new(io::ErrorKind::Other, e)))
.collect();
paths.sort_by(|a, b| match (a, b) {
(Ok(entry_a), Ok(entry_b)) => tools::sort_directories_first(entry_a, entry_b),
(Err(_), Ok(_)) => std::cmp::Ordering::Greater,
(Ok(_), Err(_)) => std::cmp::Ordering::Less,
(Err(_), Err(_)) => std::cmp::Ordering::Equal,
});
for result in paths {
match result {
Ok(entry) => {
let _ = self.list_files(ui, &entry.path());
}
Err(err) => {
ui.label(format!("Error processing directory entry: {}", err));
}
}
}
}
});
Ok(())
}
pub fn open_file(&mut self, path_option: Option<&Path>) {
if let Some(path) = path_option.clone() {
for (index, tab) in self.tabs.clone().iter().enumerate() {
@ -234,4 +185,32 @@ impl Calcifer {
));
result
}
pub fn list_files(&mut self, ui: &mut egui::Ui, file: &file_tree::File, depth: isize, n_files: &mut usize) {
*n_files += 1;
if let Some(folder_content) = &file.folder_content {
let collapsing_response = egui::CollapsingHeader::new(file.name.clone())
.default_open(depth > 0)
.show(ui, |ui| {
if !self.tree_dir_opened.contains(&file.name) {
return
}
for deeper_file in folder_content {
self.list_files(ui, &deeper_file, depth - 1, n_files);
}
});
if collapsing_response.fully_closed() {
self.tree_dir_opened.retain(|s| s != &file.name);
} else {
if !self.tree_dir_opened.contains(&file.name) {
self.tree_dir_opened.push(file.name.clone());
}
}
} else {
if ui.button(&file.name).clicked() {
self.open_file(Some(&file.path));
}
}
}
}

View file

@ -6,25 +6,21 @@ use eframe::egui;
use egui::FontFamily::Proportional;
use egui::FontId;
use egui::TextStyle::{Body, Button, Heading, Monospace, Small};
use std::{ops::Range, path::Path, sync::Arc, thread, time};
use std::{ops::Range, path::PathBuf, sync::Arc, thread, time};
use homedir::get_my_home;
use calcifer::code_editor::themes::DEFAULT_THEMES;
#[cfg(debug_assertions)]
mod build {
pub const SAVE_PATH: &str = "/home/penwing/Documents/.save/debug/calcifer_save.json";
pub const TITLE: &str = " debug";
}
const TITLE: &str = " debug";
#[cfg(not(debug_assertions))]
mod build {
pub const SAVE_PATH: &str = "/home/penwing/Documents/.save/calcifer_save.json";
pub const TITLE: &str = "";
}
const TITLE: &str = "";
use build::SAVE_PATH;
use build::TITLE;
const ALLOWED_FILE_EXTENSIONS: [&str; 6] = [
"", "rs", "toml", "txt", "project", "sh",
];
const PROJECT_EXTENSION: &str = "project";
const TERMINAL_HEIGHT: f32 = 200.0;
const TERMINAL_RANGE: Range<f32> = 100.0..500.0;
@ -33,7 +29,6 @@ const TIME_LABELS: [&str; 7] = [
"input", "settings", "tree", "terminal", "tabs", "content", "windows",
];
const MAX_FPS: f32 = 30.0;
const PATH_ROOT: &str = "/home/penwing/Documents/";
const DISPLAY_PATH_DEPTH: usize = 3;
const MAX_TABS: usize = 20;
@ -48,8 +43,8 @@ fn main() -> Result<(), eframe::Error> {
};
// Attempt to load previous state
let app_state: tools::AppState = if Path::new(SAVE_PATH).exists() {
match tools::load_state(SAVE_PATH) {
let app_state: tools::AppState = if save_path().exists() {
match tools::load_state(save_path().as_path()) {
Ok(app_state) => app_state,
Err(_) => tools::AppState::default(),
}
@ -76,6 +71,10 @@ struct Calcifer {
project_mode: bool,
home: PathBuf,
tree_dir_opened: Vec<String>,
file_tree: Option<tools::file_tree::File>,
tree_visible: bool,
profiler_visible: bool,
terminal_visible: bool,
@ -107,6 +106,10 @@ impl Default for Calcifer {
project_mode: true,
home: get_my_home().unwrap().unwrap(),
tree_dir_opened: vec![],
file_tree: None,
tree_visible: false,
profiler_visible: false,
terminal_visible: false,
@ -250,3 +253,12 @@ impl eframe::App for Calcifer {
self.save_state();
}
}
fn save_path() -> PathBuf {
if TITLE.is_empty() {
get_my_home().unwrap().unwrap().as_path().join(".calcifer").join("save.json").to_path_buf()
} else {
get_my_home().unwrap().unwrap().as_path().join(".calcifer").join("debug").join("save.json").to_path_buf()
}
}