panic hook

This commit is contained in:
core 2024-01-21 01:42:20 -05:00
parent 7fdc97b619
commit 65f375dd97
Signed by: core
GPG Key ID: FDBF740DADDCEECF
4 changed files with 29 additions and 2 deletions

7
Cargo.lock generated
View File

@ -40,6 +40,12 @@ version = "0.3.3"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d77f7ec81a6d05a3abb01ab6eb7590f6083d08449fe5a1c8b1e620283546ccb7" checksum = "d77f7ec81a6d05a3abb01ab6eb7590f6083d08449fe5a1c8b1e620283546ccb7"
[[package]]
name = "hex"
version = "0.4.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7f24254aa9a54b5c858eaee2f5bccdb46aaf0e486a595ed5fd8f86ba55232a70"
[[package]] [[package]]
name = "is-terminal" name = "is-terminal"
version = "0.4.10" version = "0.4.10"
@ -56,6 +62,7 @@ name = "kabel"
version = "0.1.0" version = "0.1.0"
dependencies = [ dependencies = [
"colored", "colored",
"hex",
"is-terminal", "is-terminal",
"libkabel", "libkabel",
"tracing-subscriber", "tracing-subscriber",

View File

@ -12,3 +12,4 @@ libkabel = { version = "0.1", path = "../libkabel", features = ["pretty-emitter"
is-terminal = "0.4" is-terminal = "0.4"
tracing-subscriber = "0.3" tracing-subscriber = "0.3"
colored = "2" colored = "2"
hex = "0.4"

View File

@ -7,8 +7,27 @@ use libkabel::source::SourceFile;
use std::error::Error; use std::error::Error;
use std::io; use std::io;
use std::{env, fs}; use std::{env, fs};
use std::fmt::Write;
fn main() -> Result<(), Box<dyn Error>> { fn main() -> Result<(), Box<dyn Error>> {
std::panic::set_hook(Box::new(|i| {
eprintln!("{}", "----- Kabel has crashed! -----".red());
eprintln!("{} {} {}", "--".red(), "THIS IS NOT YOUR FAULT".bold().red(), "--".red());
eprintln!("{}", "Please report the following Error ID to the Kabel developers, along with your program's complete source code.".red());
let mut panic_info = String::new();
write!(panic_info, "{i}").unwrap();
let error_id = hex::encode(panic_info);
eprintln!("{}{}", "Error ID: ".red(), error_id.italic().red());
eprintln!("{}", "Please, either:".red());
eprintln!("{}", "- Open an issue at https://git.e3t.cc/tm85/kabel, or".red());
eprintln!("{}", "- E-Mail the developers at kabel@e3t.cc".red());
eprintln!("{} {} {}", "-- Remember:".red(), "THIS IS NOT YOUR FAULT".bold().red(), "--".red());
eprintln!("{}", "----- Kabel has crashed! -----".red());
}));
let argv: Vec<String> = env::args().collect(); let argv: Vec<String> = env::args().collect();
if argv.len() != 2 { if argv.len() != 2 {
println!("Usage: {} <file.kab>", &argv[0]); println!("Usage: {} <file.kab>", &argv[0]);
@ -49,7 +68,7 @@ fn main() -> Result<(), Box<dyn Error>> {
"error: Please report this error to the kabel developers along with your" "error: Please report this error to the kabel developers along with your"
.red() .red()
); );
eprintln!("{}", "fail: program's complete source code. Either"); eprintln!("{}", "fail: program's complete source code. Either".red());
eprintln!( eprintln!(
"{}", "{}",
"fail: - Open an Issue at https://git.e3t.cc/tm85/kabel, or".red() "fail: - Open an Issue at https://git.e3t.cc/tm85/kabel, or".red()

View File

@ -46,7 +46,7 @@ impl Display for TokenType {
TokenType::Minus => write!(f, "-"), TokenType::Minus => write!(f, "-"),
TokenType::Plus => write!(f, "+"), TokenType::Plus => write!(f, "+"),
TokenType::Star => write!(f, "*"), TokenType::Star => write!(f, "*"),
TokenType::Semicolon => write!(f, ";\n"), TokenType::Semicolon => writeln!(f, ";"),
TokenType::To => write!(f, "to"), TokenType::To => write!(f, "to"),
TokenType::Identifier(i) => write!(f, "{i}"), TokenType::Identifier(i) => write!(f, "{i}"),
TokenType::Colon => write!(f, ":"), TokenType::Colon => write!(f, ":"),