diff --git a/Cargo.toml b/Cargo.toml index 2f178e5a..0533533d 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -15,15 +15,16 @@ exclude = ["images/", "tests/", "miette-derive/"] thiserror = "1.0.26" miette-derive = { path = "miette-derive", version = "=2.2.1-alpha.0"} once_cell = "1.8.0" -owo-colors = "2.0.0" -atty = "0.2.14" -ci_info = "0.14.2" -textwrap = "0.14.2" -term_size = "0.3.2" -unicode-width = "0.1.8" -supports-hyperlinks = "1.1.0" -supports-color = "1.0.2" -supports-unicode = "1.0.0" + +owo-colors = { version = "2.0.0", optional = true } +atty = { version = "0.2.14", optional = true } +ci_info = { version = "0.14.2", optional = true } +textwrap = { version = "0.14.2", optional = true } +term_size = { version = "0.3.2", optional = true } +unicode-width = { version = "0.1.8", optional = true } +supports-hyperlinks = { version = "1.1.0", optional = true } +supports-color = { version = "1.0.2", optional = true } +supports-unicode = { version = "1.0.0", optional = true } [dev-dependencies] semver = "1.0.4" @@ -35,5 +36,19 @@ rustversion = "1.0" trybuild = { version = "1.0.19", features = ["diff"] } syn = { version = "1.0", features = ["full"] } +[features] +default = [] +fancy = [ + "owo-colors", + "atty", + "ci_info", + "textwrap", + "term_size", + "unicode-width", + "supports-hyperlinks", + "supports-color", + "supports-unicode" +] + [workspace] members = ["miette-derive"] diff --git a/README.md b/README.md index dd30b5df..cd09dabf 100644 --- a/README.md +++ b/README.md @@ -66,6 +66,12 @@ Using [`cargo-edit`](https://crates.io/crates/cargo-edit): $ cargo add miette ``` +If you want to use the fancy printer in all these screenshots: + +```sh +$ cargo add miette --features fancy +``` + ## Example ```rust @@ -240,6 +246,14 @@ fn pretend_this_is_main() -> Result<()> { } ``` +Please note: in order to get fancy diagnostic rendering with all the pretty +colors and arrows, you should install `miette` with the `fancy` feature +enabled: + +```toml +miette = { version = "X.Y.Z", features = ["fancy"] } +``` + ### ... diagnostic code URLs `miette` supports providing a URL for individual diagnostics. This URL will be diff --git a/src/eyreish/mod.rs b/src/eyreish/mod.rs index aeedc0b4..77b8659c 100644 --- a/src/eyreish/mod.rs +++ b/src/eyreish/mod.rs @@ -26,7 +26,12 @@ pub use ReportHandler as EyreContext; #[allow(unreachable_pub)] pub use WrapErr as Context; -use crate::{Diagnostic, MietteHandler}; +use crate::Diagnostic; +#[cfg(feature = "fancy")] +use crate::MietteHandler; +#[cfg(not(feature = "fancy"))] +use crate::NarratableReportHandler; + use error::ErrorImpl; mod context; @@ -87,7 +92,10 @@ fn capture_handler(error: &(dyn Diagnostic + 'static)) -> Box } fn get_default_printer(_err: &(dyn Diagnostic + 'static)) -> Box { - Box::new(MietteHandler::new()) + #[cfg(feature = "fancy")] + return Box::new(MietteHandler::new()); + #[cfg(not(feature = "fancy"))] + return Box::new(NarratableReportHandler::new()); } impl dyn ReportHandler { diff --git a/src/handlers/mod.rs b/src/handlers/mod.rs index 521fe50b..56062689 100644 --- a/src/handlers/mod.rs +++ b/src/handlers/mod.rs @@ -3,12 +3,16 @@ Reporters included with `miette`. */ #[allow(unreachable_pub)] +#[cfg(feature = "fancy")] pub use graphical::*; #[allow(unreachable_pub)] pub use narratable::*; #[allow(unreachable_pub)] +#[cfg(feature = "fancy")] pub use theme::*; +#[cfg(feature = "fancy")] mod graphical; mod narratable; +#[cfg(feature = "fancy")] mod theme; diff --git a/src/lib.rs b/src/lib.rs index 46825dcc..3e3504a7 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -6,6 +6,7 @@ pub use miette_derive::*; pub use error::*; pub use eyreish::*; +#[cfg(feature = "fancy")] pub use handler::*; pub use handlers::*; pub use named_source::*; @@ -14,6 +15,7 @@ pub use protocol::*; mod chain; mod error; mod eyreish; +#[cfg(feature = "fancy")] mod handler; mod handlers; mod named_source; diff --git a/tests/narrated.rs b/tests/narrated.rs index 4a30d33e..e63fa8e4 100644 --- a/tests/narrated.rs +++ b/tests/narrated.rs @@ -1,13 +1,18 @@ use miette::{ - Diagnostic, GraphicalReportHandler, GraphicalTheme, MietteError, NamedSource, + Diagnostic, MietteError, NamedSource, NarratableReportHandler, Report, SourceSpan, }; + +#[cfg(feature = "fancy")] +use miette::{GraphicalReportHandler, GraphicalTheme}; + use thiserror::Error; fn fmt_report(diag: Report) -> String { let mut out = String::new(); // Mostly for dev purposes. - if std::env::var("STYLE").is_ok() { + if cfg!(feature = "fancy") && std::env::var("STYLE").is_ok() { + #[cfg(feature = "fancy")] GraphicalReportHandler::new_themed(GraphicalTheme::unicode()) .render_report(&mut out, diag.as_ref()) .unwrap(); diff --git a/tests/printer.rs b/tests/printer.rs index 0d88213b..76590a04 100644 --- a/tests/printer.rs +++ b/tests/printer.rs @@ -1,3 +1,5 @@ +#![cfg(feature = "fancy")] + use miette::{ Diagnostic, GraphicalReportHandler, GraphicalTheme, MietteError, NamedSource, NarratableReportHandler, Report, SourceSpan,