much better error handling

This commit is contained in:
Penwing 2024-01-26 09:46:15 +01:00
parent 5fedd82dda
commit b7b924d6b4
5 changed files with 50 additions and 50 deletions

View file

@ -1,11 +1,12 @@
use eframe::egui; use eframe::egui;
use egui::{text::CCursor, text_edit::CCursorRange, Rangef}; use egui::{text::CCursor, text_edit::CCursorRange, Rangef};
use std::{cmp::max, env, path::Path}; use std::{cmp::max, env, path::Path, path::PathBuf};
use crate::tools; use crate::tools;
use crate::Calcifer; use crate::Calcifer;
use crate::MAX_TABS; use crate::MAX_TABS;
use crate::PATH_ROOT; use crate::PATH_ROOT;
use tools::hex_str_to_color;
pub mod code_editor; pub mod code_editor;
use code_editor::CodeEditor; use code_editor::CodeEditor;
@ -77,12 +78,9 @@ impl Calcifer {
.resizable(true) .resizable(true)
.show(ctx, |ui| { .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| {
let command_color = egui::Color32::from_hex(self.theme.functions) let command_color = hex_str_to_color(self.theme.functions);
.expect("Theme color issue (functions)"); let entry_color = hex_str_to_color(self.theme.literals);
let entry_color = egui::Color32::from_hex(self.theme.literals) let bg_color = hex_str_to_color(self.theme.bg);
.expect("Theme color issue (literals)");
let bg_color =
egui::Color32::from_hex(self.theme.bg).expect("Theme color issue (bg)");
ui.label(""); ui.label("");
@ -95,7 +93,7 @@ impl Calcifer {
ui.colored_label( ui.colored_label(
command_color, command_color,
tools::format_path( tools::format_path(
&env::current_dir().expect("Could not find Shell Environnment"), &env::current_dir().unwrap_or_else(|_| PathBuf::from("/")),
), ),
); );
let response = ui.add( let response = ui.add(
@ -148,21 +146,16 @@ impl Calcifer {
.show(ctx, |ui| { .show(ctx, |ui| {
ui.horizontal(|ui| { ui.horizontal(|ui| {
ui.style_mut().visuals.selection.bg_fill = ui.style_mut().visuals.selection.bg_fill =
egui::Color32::from_hex(self.theme.functions) hex_str_to_color(self.theme.functions);
.expect("Could not convert color"); ui.style_mut().visuals.hyperlink_color = hex_str_to_color(self.theme.functions);
ui.style_mut().visuals.hyperlink_color =
egui::Color32::from_hex(self.theme.functions)
.expect("Could not convert color");
for (index, tab) in self.tabs.clone().iter().enumerate() { for (index, tab) in self.tabs.clone().iter().enumerate() {
let mut title = tab.get_name(); let mut title = tab.get_name();
if !tab.saved { if !tab.saved {
title += " ~"; title += " ~";
} }
if self.selected_tab == tools::TabNumber::from_index(index) { if self.selected_tab == tools::TabNumber::from_index(index) {
ui.style_mut().visuals.override_text_color = Some( ui.style_mut().visuals.override_text_color =
egui::Color32::from_hex(self.theme.bg) Some(hex_str_to_color(self.theme.bg));
.expect("Could not convert color"),
);
} }
ui.selectable_value( ui.selectable_value(
&mut self.selected_tab, &mut self.selected_tab,

View file

@ -9,6 +9,7 @@ use crate::MAX_TABS;
use crate::PATH_ROOT; use crate::PATH_ROOT;
use crate::SAVE_PATH; use crate::SAVE_PATH;
use crate::TIME_LABELS; use crate::TIME_LABELS;
use tools::hex_str_to_color;
impl Calcifer { impl Calcifer {
pub fn handle_confirm(&mut self) { pub fn handle_confirm(&mut self) {
@ -27,9 +28,7 @@ impl Calcifer {
if self.tabs[self.selected_tab.to_index()] if self.tabs[self.selected_tab.to_index()]
.path .path
.file_name() .file_name()
.expect("Could not get Tab Name") .map_or(true, |name| name.to_string_lossy() == "untitled")
.to_string_lossy()
== "untitled"
{ {
self.save_tab_as() self.save_tab_as()
} else { } else {
@ -77,11 +76,9 @@ impl Calcifer {
}; };
for path in app_state.tabs { for path in app_state.tabs {
if path if !path
.file_name() .file_name()
.expect("Could not get Tab Name") .map_or(true, |name| name.to_string_lossy() == "untitled")
.to_string_lossy()
!= "untitled"
{ {
new.open_file(Some(&path)); new.open_file(Some(&path));
} }
@ -195,15 +192,11 @@ impl Calcifer {
let text_color: Color32; let text_color: Color32;
if display { if display {
bg_color = Color32::from_hex(self.theme.functions) bg_color = hex_str_to_color(self.theme.functions);
.expect("Could not convert color to hex (functions)"); text_color = hex_str_to_color(self.theme.bg);
text_color =
Color32::from_hex(self.theme.bg).expect("Could not convert color to hex (bg)");
} else { } else {
bg_color = bg_color = hex_str_to_color(self.theme.bg);
Color32::from_hex(self.theme.bg).expect("Could not convert color to hex (bg)"); text_color = hex_str_to_color(self.theme.literals);
text_color = Color32::from_hex(self.theme.literals)
.expect("Could not convert color to hex (literals)");
}; };
ui.style_mut().visuals.override_text_color = Some(text_color); ui.style_mut().visuals.override_text_color = Some(text_color);

View file

@ -37,7 +37,8 @@ const DISPLAY_PATH_DEPTH: usize = 3;
const MAX_TABS: usize = 20; const MAX_TABS: usize = 20;
fn main() -> Result<(), eframe::Error> { fn main() -> Result<(), eframe::Error> {
let icon_data = tools::load_icon(); let icon_data = tools::load_icon().unwrap_or_default();
let options = eframe::NativeOptions { let options = eframe::NativeOptions {
viewport: egui::ViewportBuilder::default() viewport: egui::ViewportBuilder::default()
.with_inner_size([1200.0, 800.0]) .with_inner_size([1200.0, 800.0])
@ -47,12 +48,12 @@ fn main() -> Result<(), eframe::Error> {
// Attempt to load previous state // Attempt to load previous state
let app_state: tools::AppState = if Path::new(SAVE_PATH).exists() { let app_state: tools::AppState = if Path::new(SAVE_PATH).exists() {
tools::load_state(SAVE_PATH).expect("Failed to load the save") match tools::load_state(SAVE_PATH) {
} else { Ok(app_state) => app_state,
tools::AppState { Err(_) => tools::AppState::default(),
tabs: vec![],
theme: 0,
} }
} else {
tools::AppState::default()
}; };
eframe::run_native( eframe::run_native(

View file

@ -1,10 +1,12 @@
use crate::calcifer::code_editor::Syntax; use crate::calcifer::code_editor::Syntax;
use crate::DISPLAY_PATH_DEPTH; use crate::DISPLAY_PATH_DEPTH;
use eframe::egui; use eframe::egui;
use egui::Color32;
use image::GenericImageView;
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
use std::{ use std::{
cmp::Ordering, ffi::OsStr, fs, fs::read_to_string, fs::OpenOptions, io::Write, path::Component, cmp::Ordering, error::Error, ffi::OsStr, fs, fs::read_to_string, fs::OpenOptions, io::Write,
path::Path, path::PathBuf, path::Component, path::Path, path::PathBuf,
}; };
//my tools; //my tools;
@ -25,6 +27,15 @@ pub struct AppState {
pub theme: usize, pub theme: usize,
} }
impl Default for AppState {
fn default() -> Self {
Self {
tabs: vec![],
theme: 0,
}
}
}
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)?;
@ -51,22 +62,20 @@ pub fn load_state(file_path: &str) -> Result<AppState, std::io::Error> {
Ok(serde_json::from_str(&serialized_state)?) Ok(serde_json::from_str(&serialized_state)?)
} }
pub fn load_icon() -> egui::IconData { 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)?;
.expect("Failed to open icon path") let rgba = image.clone().into_rgba8().to_vec();
.into_rgba8();
let (width, height) = image.dimensions(); let (width, height) = image.dimensions();
let rgba = image.into_raw();
(rgba, width, height) (rgba, width, height)
}; };
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 {
@ -114,5 +123,9 @@ pub fn format_path(path: &Path) -> String {
) )
} }
pub fn hex_str_to_color(hex_str: &str) -> Color32 {
Color32::from_hex(hex_str).unwrap_or_else(|_| Color32::BLACK)
}
#[cfg(test)] #[cfg(test)]
mod tests; mod tests;

View file

@ -70,9 +70,9 @@ impl Tab {
pub fn get_name(&self) -> String { pub fn get_name(&self) -> String {
self.path self.path
.file_name() .file_name()
.expect("Could not get Tab Name") .map_or("untitled".to_string(), |name| {
.to_string_lossy() name.to_string_lossy().to_string()
.to_string() })
} }
pub fn refresh(&mut self) { pub fn refresh(&mut self) {