From 593e2ada3297636b505372d43cd7cf8fcfe2d213 Mon Sep 17 00:00:00 2001 From: Nadrieril Date: Sun, 15 Sep 2024 15:39:08 +0200 Subject: [PATCH] Dim solver comments --- src/printer.rs | 12 ++++++++++++ src/solver.rs | 13 ++++++------- 2 files changed, 18 insertions(+), 7 deletions(-) diff --git a/src/printer.rs b/src/printer.rs index 947c40b..abf1861 100644 --- a/src/printer.rs +++ b/src/printer.rs @@ -7,6 +7,7 @@ pub trait Style { fn green(&self) -> String; fn red(&self) -> String; fn dimmed(&self) -> String; + fn comment(&self) -> String; fn tooltip(&self, text: &str) -> String; fn code(&self) -> String; } @@ -36,6 +37,14 @@ impl Style for &str { ::dimmed(self).to_string() } } + fn comment(&self) -> String { + if cfg!(target_arch = "wasm32") { + format!("{self}") + } else { + use colored::Colorize; + ::dimmed(self).to_string() + } + } fn tooltip(&self, text: &str) -> String { if cfg!(target_arch = "wasm32") { format!("{self}") @@ -62,6 +71,9 @@ impl Style for String { fn dimmed(&self) -> String { self.as_str().dimmed() } + fn comment(&self) -> String { + self.as_str().comment() + } fn tooltip(&self, text: &str) -> String { self.as_str().tooltip(text) } diff --git a/src/solver.rs b/src/solver.rs index 3f85cbd..7efbe50 100644 --- a/src/solver.rs +++ b/src/solver.rs @@ -1,7 +1,9 @@ use std::collections::VecDeque; use std::fmt; +use std::fmt::Write; use itertools::Itertools; +use printer::Style; use crate::*; @@ -97,7 +99,6 @@ pub fn trace_solver<'a>( options: RuleOptions, style: PredicateStyle, ) -> String { - use std::fmt::Write; let arenas = &Arenas::default(); let ctx = TypingCtx { arenas, options }; let mut solver = TypingSolver::new(request); @@ -106,7 +107,8 @@ pub fn trace_solver<'a>( loop { match solver.step(ctx) { Ok(rule) => { - let _ = write!(&mut trace, "// Applying rule `{}`\n", rule.display(options)); + let line = format!("// Applying rule `{}`", rule.display(options)); + let _ = write!(&mut trace, "{}\n", line.comment()); let _ = write!(&mut trace, "{}\n", solver.display_state(style)); } Err(e) => { @@ -116,11 +118,8 @@ pub fn trace_solver<'a>( let _ = write!(&mut trace, "{}\n", solver.display_final_state(ctx, style)); } CantStep::NoApplicableRule(pred, err) => { - let _ = write!( - &mut trace, - "// Type error for `{}`: {err:?}\n", - pred.display(style) - ); + let line = format!("// Type error for `{}`: {err:?}", pred.display(style)); + let _ = write!(&mut trace, "{}\n", line.red()); } } break;