better error handeling when opening/reloading files

This commit is contained in:
Penwing 2024-01-26 08:07:47 +01:00
parent 0c466a322e
commit c77752c05c
5 changed files with 31 additions and 35 deletions

1
Cargo.lock generated
View file

@ -579,7 +579,6 @@ dependencies = [
"rfd", "rfd",
"serde", "serde",
"serde_json", "serde_json",
"toml",
] ]
[[package]] [[package]]

View file

@ -6,7 +6,6 @@ edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies] [dependencies]
toml = "0.8.8"
eframe = "0.25.0" eframe = "0.25.0"
rfd = "0.12.1" rfd = "0.12.1"
egui_extras = "0.25.0" egui_extras = "0.25.0"

View file

@ -56,7 +56,7 @@ fn main() -> Result<(), eframe::Error> {
}; };
eframe::run_native( eframe::run_native(
&format!("Calcifer{}{}", tools::version(), TITLE), &format!("Calcifer{}", TITLE),
options, options,
Box::new(move |_cc| Box::from(Calcifer::from_app_state(app_state))), Box::new(move |_cc| Box::from(Calcifer::from_app_state(app_state))),
) )

View file

@ -6,7 +6,6 @@ use std::{
cmp::Ordering, ffi::OsStr, fs, fs::read_to_string, fs::OpenOptions, io::Write, path::Component, cmp::Ordering, ffi::OsStr, fs, fs::read_to_string, fs::OpenOptions, io::Write, path::Component,
path::Path, path::PathBuf, path::Path, path::PathBuf,
}; };
use toml::Value;
//my tools; //my tools;
pub mod confirm; pub mod confirm;
@ -115,27 +114,5 @@ pub fn format_path(path: &Path) -> String {
) )
} }
pub fn version() -> String {
// Read the contents of the Cargo.toml file
if !Path::new("Cargo.toml").exists() {
return "".to_string();
}
let toml_content = fs::read_to_string("Cargo.toml").expect("Failed to read Cargo.toml");
// Parse the TOML content
let toml: Value = toml::from_str(&toml_content).expect("Failed to parse TOML");
// Extract version information
if let Some(package) = toml.get("package") {
if let Some(version) = package.get("version") {
if let Some(version_string) = version.as_str() {
println!("Version: {}", version_string);
return format!(" v{}", version_string);
}
}
}
"".to_string()
}
#[cfg(test)] #[cfg(test)]
mod tests; mod tests;

View file

@ -50,17 +50,23 @@ impl Default for Tab {
impl Tab { impl Tab {
pub fn new(path: PathBuf) -> Self { pub fn new(path: PathBuf) -> Self {
let text = read_file_contents(&path).replace(&" ".repeat(4), "\t");
let file_path = format_file_path(&path, &text);
let extension = file_path
.extension()
.and_then(|ext| ext.to_str())
.unwrap_or_default();
Self { Self {
path: path.clone(), path: file_path.clone(),
code: read_to_string(path.clone()) code: text,
.expect("Not able to read the file") language: extension.into(),
.replace(&" ".repeat(4), "\t"),
language: path.to_str().unwrap().split('.').last().unwrap().into(),
saved: true, saved: true,
scroll_offset: 0.0, scroll_offset: 0.0,
last_cursor: None, last_cursor: None,
} }
} }
pub fn get_name(&self) -> String { pub fn get_name(&self) -> String {
self.path self.path
.file_name() .file_name()
@ -70,10 +76,25 @@ impl Tab {
} }
pub fn refresh(&mut self) { pub fn refresh(&mut self) {
self.code = read_to_string(self.path.clone()) let text = read_file_contents(&self.path).replace(&" ".repeat(4), "\t");
.expect("Not able to read the file") let file_path = format_file_path(&self.path, &text);
.replace(&" ".repeat(4), "\t");
self.code = text;
self.path = file_path;
self.saved = true; self.saved = true;
println!("refreshed {}", self.path.display()); }
}
fn read_file_contents(path: &PathBuf) -> String {
read_to_string(path.clone())
.map_err(|err| format!("// Error reading file: {}", err))
.unwrap_or_else(|err_msg| err_msg)
}
fn format_file_path(path: &PathBuf, contents: &str) -> PathBuf {
if contents.contains("Error reading file") {
"untitled".into()
} else {
path.clone()
} }
} }