Only use colors & text decorations on ANSI-capable terminals.

Fixes #404.
This commit is contained in:
KtorZ
2023-02-26 13:19:03 +01:00
parent 2f2be39813
commit a46a9fca41
16 changed files with 593 additions and 261 deletions

View File

@@ -1,7 +1,7 @@
use super::schema;
use aiken_lang::ast::Span;
use miette::{Diagnostic, NamedSource};
use owo_colors::OwoColorize;
use owo_colors::{OwoColorize, Stream::Stdout};
use std::fmt::Debug;
#[derive(Debug, thiserror::Error, Diagnostic)]
@@ -19,15 +19,25 @@ pub enum Error {
#[error("Invalid or missing project's blueprint file.")]
#[diagnostic(code("aiken::blueprint::missing"))]
#[diagnostic(help("Did you forget to {build} the project?", build = "build".purple().bold()))]
#[diagnostic(help(
"Did you forget to {build} the project?",
build = "build"
.if_supports_color(Stdout, |s| s.purple())
.if_supports_color(Stdout, |s| s.bold())
))]
InvalidOrMissingFile,
#[error("I didn't find any parameters to apply in the given validator.")]
#[diagnostic(code("aiken::blueprint::apply::no_parameters"))]
NoParametersToApply,
#[error("I couldn't compute the address of the given validator because it's parameterized by {} parameter(s)!", format!("{n}").purple())]
#[error(
"I couldn't compute the address of the given validator because it's parameterized by {} parameter(s)!",
n.if_supports_color(Stdout, |s| s.purple())
)]
#[diagnostic(code("aiken::blueprint::address::parameterized"))]
#[diagnostic(help("I can only compute addresses of validators that are fully applied. For example, a {keyword_spend} validator must have exactly 3 arguments: a datum, a redeemer and a context. If it has more, they need to be provided beforehand and applied directly in the validator. Applying parameters change the validator's compiled code, and thus the address.\n\nThis is why I need you to apply parmeters first.", keyword_spend = "spend".purple()))]
#[diagnostic(help(
"I can only compute addresses of validators that are fully applied. For example, a {keyword_spend} validator must have exactly 3 arguments: a datum, a redeemer and a context. If it has more, they need to be provided beforehand and applied directly in the validator. Applying parameters change the validator's compiled code, and thus the address.\n\nThis is why I need you to apply parmeters first.",
keyword_spend = "spend".if_supports_color(Stdout, |s| s.purple())))]
ParameterizedValidator { n: usize },
}

View File

@@ -3,7 +3,7 @@ use aiken_lang::{
ast::{DataType, Definition, TypedDefinition},
tipo::{pretty, Type, TypeVar},
};
use owo_colors::OwoColorize;
use owo_colors::{OwoColorize, Stream::Stdout};
use serde::{
self,
ser::{Serialize, SerializeStruct, Serializer},
@@ -533,7 +533,9 @@ I got there when trying to generate a blueprint specification of the following t
r#"There cannot be any unbound type variable at the contract's boundary (i.e. in types used as datum and/or redeemer). Indeed, in order to generate an outward-facing specification of the contract's interface, I need to know what concrete representations will the datum and/or the redeemer have.
If your contract doesn't need datum or redeemer, you can always give them the type {type_Void} to indicate this. It is very concrete and will help me progress forward."#,
type_Void = "Void".bright_blue().bold()
type_Void = "Void"
.if_supports_color(Stdout, |s| s.bright_blue())
.if_supports_color(Stdout, |s| s.bold())
),
ErrorContext::ExpectedData => format!(
@@ -542,7 +544,9 @@ If your contract doesn't need datum or redeemer, you can always give them the ty
There are few restrictions like this one. In this instance, here's the types I followed and that led me to this problem:
╰─▶ {breadcrumbs}"#,
type_String = "String".bright_blue().bold(),
type_String = "String"
.if_supports_color(Stdout, |s| s.bright_blue())
.if_supports_color(Stdout, |s| s.bold()),
breadcrumbs = Error::fmt_breadcrumbs(&self.breadcrumbs)
),
@@ -564,8 +568,8 @@ Here's the types I followed and that led me to this problem:
pretty::Printer::new()
.print(type_info)
.to_pretty_string(70)
.bright_blue()
.bold()
.if_supports_color(Stdout, |s| s.bright_blue())
.if_supports_color(Stdout, |s| s.bold())
.to_string()
})
.collect::<Vec<_>>()