From e6d21f09362b54a837d83a21202fc7562a53c3e8 Mon Sep 17 00:00:00 2001 From: Greg Shuflin Date: Mon, 2 Dec 2024 02:57:32 -0800 Subject: [PATCH] Handle colors correctly --- justfile | 1 - src/compile_error.rs | 64 +++++--------------------------------------- src/error.rs | 2 +- tests/string.rs | 10 +++---- tests/subsequents.rs | 14 +++++----- 5 files changed, 19 insertions(+), 72 deletions(-) diff --git a/justfile b/justfile index 8e94d44432..759022e285 100755 --- a/justfile +++ b/justfile @@ -34,7 +34,6 @@ run: filter PATTERN: cargo test {{PATTERN}} -[group: 'misc'] [group: 'misc'] build: cargo build diff --git a/src/compile_error.rs b/src/compile_error.rs index 6847900f06..7c17c1c7bd 100644 --- a/src/compile_error.rs +++ b/src/compile_error.rs @@ -19,8 +19,8 @@ impl<'src> CompileError<'src> { } } -pub(crate) fn render_compile_error(error: &CompileError) { - use ariadne::{Label, Report, ReportKind, Source}; +pub(crate) fn render_compile_error(error: &CompileError, color: Color) { + use ariadne::{Config, Label, Report, ReportKind, Source}; let token = error.token; let source = Source::from(token.src); @@ -31,7 +31,8 @@ pub(crate) fn render_compile_error(error: &CompileError) { let path = format!("{}", token.path.display()); let label = Label::new((&path, start..end)); - let report = Report::build(ReportKind::Error, &path, start); + let config = Config::default().with_color(color.stderr().active()); + let report = Report::build(ReportKind::Error, &path, start).with_config(config); let report = match &*error.kind { CompileErrorKind::AttributeArgumentCountMismatch { @@ -49,27 +50,17 @@ pub(crate) fn render_compile_error(error: &CompileError) { }; report - .with_code("E01") .with_message("Attribute argument count mismatch") .with_label(label.with_message(label_msg)) .with_note(note) .finish() } - /* - CompileErrorKind::BacktickShebang => todo!(), - CompileErrorKind::CircularRecipeDependency { recipe, circle } => todo!(), - CompileErrorKind::CircularVariableDependency { variable, circle } => todo!(), - CompileErrorKind::DependencyArgumentCountMismatch { dependency, found, min, max } => todo!(), - CompileErrorKind::Redefinition { first, first_type, name, second_type } => todo!(), - */ CompileErrorKind::DuplicateAttribute { attribute, first } => { let original_label = source .line(*first) .map(|line| Label::new((&path, line.span())).with_message("original")); - let mut report = report - .with_code("E02") - .with_message(format!("Duplicate attribute `{attribute}`")); + let mut report = report.with_message(format!("Duplicate attribute `{attribute}`")); if let Some(original) = original_label { report = report.with_label(original); } @@ -78,50 +69,7 @@ pub(crate) fn render_compile_error(error: &CompileError) { _ => { let message = format!("{error}"); report.with_message(message).with_label(label).finish() - } /* - CompileErrorKind::DuplicateParameter { recipe, parameter } => todo!(), - CompileErrorKind::DuplicateSet { setting, first } => todo!(), - CompileErrorKind::DuplicateVariable { variable } => todo!(), - CompileErrorKind::DuplicateUnexport { variable } => todo!(), - CompileErrorKind::ExpectedKeyword { expected, found } => todo!(), - CompileErrorKind::ExportUnexported { variable } => todo!(), - CompileErrorKind::ExtraLeadingWhitespace => todo!(), - CompileErrorKind::ExtraneousAttributes { count } => todo!(), - CompileErrorKind::FunctionArgumentCountMismatch { function, found, expected } => todo!(), - CompileErrorKind::Include => todo!(), - CompileErrorKind::InconsistentLeadingWhitespace { expected, found } => todo!(), - CompileErrorKind::Internal { message } => todo!(), - CompileErrorKind::InvalidAttribute { item_kind, item_name, attribute } => todo!(), - CompileErrorKind::InvalidEscapeSequence { character } => todo!(), - CompileErrorKind::MismatchedClosingDelimiter { close, open, open_line } => todo!(), - CompileErrorKind::MixedLeadingWhitespace { whitespace } => todo!(), - CompileErrorKind::ParameterFollowsVariadicParameter { parameter } => todo!(), - CompileErrorKind::ParsingRecursionDepthExceeded => todo!(), - CompileErrorKind::RequiredParameterFollowsDefaultParameter { parameter } => todo!(), - CompileErrorKind::ShebangAndScriptAttribute { recipe } => todo!(), - CompileErrorKind::ShellExpansion { err } => todo!(), - CompileErrorKind::UndefinedVariable { variable } => todo!(), - CompileErrorKind::UnexpectedCharacter { expected } => todo!(), - CompileErrorKind::UnexpectedClosingDelimiter { close } => todo!(), - CompileErrorKind::UnexpectedEndOfToken { expected } => todo!(), - CompileErrorKind::UnexpectedToken { expected, found } => todo!(), - CompileErrorKind::UnicodeEscapeCharacter { character } => todo!(), - CompileErrorKind::UnicodeEscapeDelimiter { character } => todo!(), - CompileErrorKind::UnicodeEscapeEmpty => todo!(), - CompileErrorKind::UnicodeEscapeLength { hex } => todo!(), - CompileErrorKind::UnicodeEscapeRange { hex } => todo!(), - CompileErrorKind::UnicodeEscapeUnterminated => todo!(), - CompileErrorKind::UnknownAliasTarget { alias, target } => todo!(), - CompileErrorKind::UnknownAttribute { attribute } => todo!(), - CompileErrorKind::UnknownDependency { recipe, unknown } => todo!(), - CompileErrorKind::UnknownFunction { function } => todo!(), - CompileErrorKind::UnknownSetting { setting } => todo!(), - CompileErrorKind::UnknownStartOfToken => todo!(), - CompileErrorKind::UnpairedCarriageReturn => todo!(), - CompileErrorKind::UnterminatedBacktick => todo!(), - CompileErrorKind::UnterminatedInterpolation => todo!(), - CompileErrorKind::UnterminatedString => todo!(), - */ + } }; report.eprint((&path, source)).unwrap(); diff --git a/src/error.rs b/src/error.rs index 93707105cd..7b91b2fe03 100644 --- a/src/error.rs +++ b/src/error.rs @@ -506,7 +506,7 @@ impl ColorDisplay for Error<'_> { pub(crate) fn render_error(error: &Error, color: Color) { match error { - Error::Compile { compile_error } => compile_error::render_compile_error(compile_error), + Error::Compile { compile_error } => compile_error::render_compile_error(compile_error, color), _ => eprintln!("{}", error.color_display(color.stderr())), } } diff --git a/tests/string.rs b/tests/string.rs index 12bcbac0f7..3d28e1cedf 100644 --- a/tests/string.rs +++ b/tests/string.rs @@ -481,11 +481,11 @@ fn unicode_escape_non_hex() { .status(1) .stderr( r#" -error: expected hex digit [0-9A-Fa-f] but found `o` - ——▶ justfile:1:6 - │ -1 │ x := "\u{foo}" - │ ^^^^^^^^^ +Error: expected hex digit [0-9A-Fa-f] but found `o` + ╭─[justfile:1:6] + │ + 1 │ x := "\u{foo}" +───╯ "#, ) .run(); diff --git a/tests/subsequents.rs b/tests/subsequents.rs index 9ef4600067..c0a7ff9fe6 100644 --- a/tests/subsequents.rs +++ b/tests/subsequents.rs @@ -45,13 +45,13 @@ test! { justfile: " foo: && foo ", - stderr: " - error: Recipe `foo` depends on itself - ——▶ justfile:1:9 - │ - 1 │ foo: && foo - │ ^^^ - ", + stderr: +"Error: Recipe `foo` depends on itself + ╭─[justfile:1:9] + │ + 1 │ foo: && foo +───╯ +", status: EXIT_FAILURE, }