From 0d06b6259fae1cff4250bce23e45d003e54da006 Mon Sep 17 00:00:00 2001 From: Stefan Holderbach Date: Thu, 19 May 2022 20:59:14 +0200 Subject: [PATCH] Change miette theme based on ANSI config (#5588) * Change miette theme based on ANSI config Use the base ansi colors to simplify the use of the terminal emulator theming. Turn of most eye-candy (including unicode) when using `$config.use_ansi_coloring: false` Addresses #5582 * Fix error test affected by changed styling --- crates/nu-command/tests/commands/get.rs | 2 +- crates/nu-protocol/src/cli_error.rs | 15 +++++++++++++-- 2 files changed, 14 insertions(+), 3 deletions(-) diff --git a/crates/nu-command/tests/commands/get.rs b/crates/nu-command/tests/commands/get.rs index 09aafeb3fc..6bcf12dab2 100644 --- a/crates/nu-command/tests/commands/get.rs +++ b/crates/nu-command/tests/commands/get.rs @@ -181,7 +181,7 @@ fn errors_fetching_by_column_using_a_number() { .contains("Data cannot be accessed with a cell path"),); assert!(actual .err - .contains(" record<0: string> doesn't support cell paths"),); + .contains("record<0: string> doesn't support cell paths"),); }) } diff --git a/crates/nu-protocol/src/cli_error.rs b/crates/nu-protocol/src/cli_error.rs index 2c794a2f92..25a05abfc8 100644 --- a/crates/nu-protocol/src/cli_error.rs +++ b/crates/nu-protocol/src/cli_error.rs @@ -1,5 +1,5 @@ use crate::engine::StateWorkingSet; -use miette::{LabeledSpan, MietteHandler, ReportHandler, Severity, SourceCode}; +use miette::{LabeledSpan, MietteHandlerOpts, ReportHandler, Severity, SourceCode}; use thiserror::Error; /// This error exists so that we can defer SourceCode handling. It simply @@ -20,7 +20,18 @@ pub fn format_error( impl std::fmt::Debug for CliError<'_> { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { - MietteHandler::default().debug(self, f)?; + let ansi_support = self.1.get_config().use_ansi_coloring; + + let miette_handler = MietteHandlerOpts::new() + // For better support of terminal themes use the ANSI coloring + .rgb_colors(false) + .ansi_colors(true) + // If ansi support is disabled in the config disable the eye-candy + .color(ansi_support) + .unicode(ansi_support) + .terminal_links(ansi_support) + .build(); + miette_handler.debug(self, f)?; Ok(()) } }