From 3eec886fe83b30636622d6ba97880bf473de8a0a Mon Sep 17 00:00:00 2001 From: Akshay Date: Tue, 21 Sep 2021 16:41:47 +0530 Subject: use stderr by default, improved macro code --- bin/src/main.rs | 41 +++++++++++++++++++++++++++++------------ 1 file changed, 29 insertions(+), 12 deletions(-) (limited to 'bin') diff --git a/bin/src/main.rs b/bin/src/main.rs index ae45bfe..60afd5e 100644 --- a/bin/src/main.rs +++ b/bin/src/main.rs @@ -4,9 +4,12 @@ use std::{ }; use anyhow::{Context, Result}; -use ariadne::{Color, Fmt, Label, Report as CliReport, ReportKind as CliReportKind, Source}; +use ariadne::{ + CharSet, Color, Config as CliConfig, Label, Report as CliReport, + ReportKind as CliReportKind, Source, +}; use lib::{Report, LINTS}; -use rnix::WalkEvent; +use rnix::{TextRange, WalkEvent}; fn analyze(file: &str) -> Result> { let parsed = rnix::parse(file).as_result()?; @@ -28,6 +31,7 @@ fn analyze(file: &str) -> Result> { } fn print_report(report: Report, file_src: &str, file_path: &Path) -> Result<()> { + let range = |at: TextRange| at.start().into()..at.end().into(); let src_id = file_path.to_str().unwrap_or(""); let offset = report .diagnostics @@ -39,20 +43,33 @@ fn print_report(report: Report, file_src: &str, file_path: &Path) -> Result<()> .diagnostics .iter() .fold( - CliReport::build(CliReportKind::Warning, src_id, offset), + CliReport::build(CliReportKind::Warning, src_id, offset) + .with_config( + CliConfig::default() + .with_cross_gap(true) + .with_char_set(CharSet::ExtendedAscii), + ) + .with_message(report.note), |cli_report, diagnostic| { - let range = { - let at = diagnostic.at; - at.start().into()..at.end().into() - }; - cli_report.with_label( - Label::new((src_id, range)) - .with_message(diagnostic.message.as_str().fg(Color::Yellow)), - ) + let cli_report = cli_report.with_label( + Label::new((src_id, range(diagnostic.at))) + .with_message(&diagnostic.message) + .with_color(Color::Magenta), + ); + if let Some(s) = &diagnostic.suggestion { + let replacement_msg = format!("Try: `{}`", s.fix); + cli_report.with_label( + Label::new((src_id, range(s.at))) + .with_message(replacement_msg) + .with_color(Color::Yellow), + ) + } else { + cli_report + } }, ) .finish() - .print((src_id, Source::from(file_src))) + .eprint((src_id, Source::from(file_src))) .context("failed to print report to stdout") } -- cgit v1.2.3