From 508c2836e31c4bd05c50888f25c21233555e109d Mon Sep 17 00:00:00 2001 From: Compey Date: Wed, 9 Aug 2023 18:14:08 +0530 Subject: [PATCH] feat: error formatting --- src/cli/repl.rs | 32 ++++++++++++++++++++++++++++++-- src/lib.rs | 2 +- 2 files changed, 31 insertions(+), 3 deletions(-) diff --git a/src/cli/repl.rs b/src/cli/repl.rs index 49ccc10..1fdd90c 100644 --- a/src/cli/repl.rs +++ b/src/cli/repl.rs @@ -1,4 +1,5 @@ use std::{ + env, io::ErrorKind, path::PathBuf, process::{exit, ExitCode}, @@ -7,9 +8,21 @@ use std::{ use anyhow::Error; use clap::Command; use lune::{Lune, LuneError}; +use mlua::ExternalError; +use once_cell::sync::Lazy; use rustyline::{error::ReadlineError, history::FileHistory, DefaultEditor, Editor}; -use super::Cli; +use lune::lua::stdio::formatting::pretty_format_luau_error; + +fn env_var_bool(value: String) -> Option { + match value.to_lowercase().as_str() { + "true" => Some(true), + "1" => Some(true), + "0" => Some(false), + "false" => Some(false), + &_ => None, + } +} // Isn't dependency injection plain awesome?! pub async fn show_interface(cmd: Command) -> Result { @@ -56,6 +69,16 @@ pub async fn show_interface(cmd: Command) -> Result { let mut interrupt_counter = 0u32; + let colorize: Lazy = Lazy::new(|| { + let no_color = env::var("NO_COLOR").unwrap_or_else(|_| "false".to_string()); + + if no_color.is_empty() { + false + } else { + !env_var_bool(no_color).unwrap_or_else(|| false) + } + }); + loop { let mut source_code = String::new(); @@ -98,7 +121,12 @@ pub async fn show_interface(cmd: Command) -> Result { match eval_result { Ok(_) => (), - Err(err) => eprintln!("{}", err), + Err(err) => { + eprintln!( + "{}", + pretty_format_luau_error(&err.into_lua_err(), (&*colorize).to_owned()) + ) + } }; } diff --git a/src/lib.rs b/src/lib.rs index 1d30335..3895d59 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -4,4 +4,4 @@ mod roblox; #[cfg(test)] mod tests; -pub use crate::lune::{Lune, LuneError}; +pub use crate::lune::{lua, Lune, LuneError};