better error handeling when opening/reloading files
This commit is contained in:
parent
0c466a322e
commit
c77752c05c
1
Cargo.lock
generated
1
Cargo.lock
generated
|
@ -579,7 +579,6 @@ dependencies = [
|
|||
"rfd",
|
||||
"serde",
|
||||
"serde_json",
|
||||
"toml",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
|
|
|
@ -6,7 +6,6 @@ edition = "2021"
|
|||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||
|
||||
[dependencies]
|
||||
toml = "0.8.8"
|
||||
eframe = "0.25.0"
|
||||
rfd = "0.12.1"
|
||||
egui_extras = "0.25.0"
|
||||
|
|
|
@ -56,7 +56,7 @@ fn main() -> Result<(), eframe::Error> {
|
|||
};
|
||||
|
||||
eframe::run_native(
|
||||
&format!("Calcifer{}{}", tools::version(), TITLE),
|
||||
&format!("Calcifer{}", TITLE),
|
||||
options,
|
||||
Box::new(move |_cc| Box::from(Calcifer::from_app_state(app_state))),
|
||||
)
|
||||
|
|
|
@ -6,7 +6,6 @@ use std::{
|
|||
cmp::Ordering, ffi::OsStr, fs, fs::read_to_string, fs::OpenOptions, io::Write, path::Component,
|
||||
path::Path, path::PathBuf,
|
||||
};
|
||||
use toml::Value;
|
||||
|
||||
//my tools;
|
||||
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)]
|
||||
mod tests;
|
||||
|
|
|
@ -50,17 +50,23 @@ impl Default for Tab {
|
|||
|
||||
impl Tab {
|
||||
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 {
|
||||
path: path.clone(),
|
||||
code: read_to_string(path.clone())
|
||||
.expect("Not able to read the file")
|
||||
.replace(&" ".repeat(4), "\t"),
|
||||
language: path.to_str().unwrap().split('.').last().unwrap().into(),
|
||||
path: file_path.clone(),
|
||||
code: text,
|
||||
language: extension.into(),
|
||||
saved: true,
|
||||
scroll_offset: 0.0,
|
||||
last_cursor: None,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn get_name(&self) -> String {
|
||||
self.path
|
||||
.file_name()
|
||||
|
@ -70,10 +76,25 @@ impl Tab {
|
|||
}
|
||||
|
||||
pub fn refresh(&mut self) {
|
||||
self.code = read_to_string(self.path.clone())
|
||||
.expect("Not able to read the file")
|
||||
.replace(&" ".repeat(4), "\t");
|
||||
let text = read_file_contents(&self.path).replace(&" ".repeat(4), "\t");
|
||||
let file_path = format_file_path(&self.path, &text);
|
||||
|
||||
self.code = text;
|
||||
self.path = file_path;
|
||||
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()
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue