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<_>>()

View File

@@ -10,7 +10,7 @@ use aiken_lang::{
use miette::{
Diagnostic, EyreContext, LabeledSpan, MietteHandlerOpts, NamedSource, RgbColors, SourceCode,
};
use owo_colors::OwoColorize;
use owo_colors::{OwoColorize, Stream::Stdout};
use std::{
fmt::{Debug, Display},
io,
@@ -121,7 +121,7 @@ pub enum Error {
impl Error {
pub fn report(&self) {
eprintln!("Error: {self:?}")
println!("{self:?}")
}
pub fn from_parse_errors(errs: Vec<ParseError>, path: &Path, src: &str) -> Vec<Self> {
@@ -314,13 +314,27 @@ impl Diagnostic for Error {
Error::NoValidatorNotFound { known_validators } => {
Some(Box::new(format!(
"Here's a list of all validators I've found in your project. Please double-check this list against the options that you've provided:\n\n{}",
known_validators.iter().map(|title| format!("{title}", title = title.purple().bold())).collect::<Vec<String>>().join("\n")
known_validators
.iter()
.map(|title| format!(
"{title}",
title = title.if_supports_color(Stdout, |s| s.purple())
))
.collect::<Vec<String>>()
.join("\n")
)))
},
Error::MoreThanOneValidatorFound { known_validators } => {
Some(Box::new(format!(
"Here's a list of all validators I've found in your project. Select one of them using the appropriate options:\n\n{}",
known_validators.iter().map(|title| format!("{title}", title = title.purple().bold())).collect::<Vec<String>>().join("\n")
known_validators
.iter()
.map(|title| format!(
"{title}",
title = title.if_supports_color(Stdout, |s| s.purple())
))
.collect::<Vec<String>>()
.join("\n")
)))
},
Error::Module(e) => e.help(),
@@ -526,7 +540,7 @@ impl Warning {
}
pub fn report(&self) {
eprintln!("Warning: {self:?}")
eprintln!("{self:?}")
}
}

View File

@@ -1,4 +1,4 @@
use owo_colors::OwoColorize;
use owo_colors::{OwoColorize, Stream::Stdout};
use serde::{de::Visitor, Deserialize, Serialize};
use std::{
fmt::{self, Display},
@@ -109,12 +109,19 @@ impl<'de> Deserialize<'de> for PackageName {
#[derive(Debug, Error, miette::Diagnostic)]
pub enum Error {
#[error("{} is not a valid project name: {}", name.red(), reason.to_string())]
#[error(
"{} is not a valid project name: {}",
name.if_supports_color(Stdout, |s| s.red()),
reason.to_string()
)]
InvalidProjectName {
name: String,
reason: InvalidProjectNameReason,
},
#[error("A project named {} already exists.", name.red())]
#[error(
"A project named {} already exists.",
name.if_supports_color(Stdout, |s| s.red())
)]
ProjectExists { name: String },
}
@@ -134,9 +141,9 @@ impl fmt::Display for InvalidProjectNameReason {
{}/{}\n\nEach part must start with a lowercase letter \
and may only contain lowercase letters, numbers, hyphens or underscores.\
\nFor example,\n\n\t{}",
"{owner}".bright_blue(),
"{project}".bright_blue(),
"aiken-lang/stdlib".bright_blue(),
"{owner}".if_supports_color(Stdout, |s| s.bright_blue()),
"{project}".if_supports_color(Stdout, |s| s.bright_blue()),
"aiken-lang/stdlib".if_supports_color(Stdout, |s| s.bright_blue()),
),
}
}