From 1b6a04ca5504955c571d1c97504fb45ea0befee4 Mon Sep 17 00:00:00 2001 From: Valentin Popov Date: Mon, 8 Jan 2024 01:21:28 +0400 Subject: Initial vendor packages Signed-off-by: Valentin Popov --- vendor/miette/src/handlers/theme.rs | 275 ++++++++++++++++++++++++++++++++++++ 1 file changed, 275 insertions(+) create mode 100644 vendor/miette/src/handlers/theme.rs (limited to 'vendor/miette/src/handlers/theme.rs') diff --git a/vendor/miette/src/handlers/theme.rs b/vendor/miette/src/handlers/theme.rs new file mode 100644 index 0000000..1f5236a --- /dev/null +++ b/vendor/miette/src/handlers/theme.rs @@ -0,0 +1,275 @@ +use is_terminal::IsTerminal; +use owo_colors::Style; + +/** +Theme used by [`GraphicalReportHandler`](crate::GraphicalReportHandler) to +render fancy [`Diagnostic`](crate::Diagnostic) reports. + +A theme consists of two things: the set of characters to be used for drawing, +and the +[`owo_colors::Style`](https://docs.rs/owo-colors/latest/owo_colors/struct.Style.html)s to be used to paint various items. + +You can create your own custom graphical theme using this type, or you can use +one of the predefined ones using the methods below. +*/ +#[derive(Debug, Clone)] +pub struct GraphicalTheme { + /// Characters to be used for drawing. + pub characters: ThemeCharacters, + /// Styles to be used for painting. + pub styles: ThemeStyles, +} + +impl GraphicalTheme { + /// ASCII-art-based graphical drawing, with ANSI styling. + pub fn ascii() -> Self { + Self { + characters: ThemeCharacters::ascii(), + styles: ThemeStyles::ansi(), + } + } + + /// Graphical theme that draws using both ansi colors and unicode + /// characters. + /// + /// Note that full rgb colors aren't enabled by default because they're + /// an accessibility hazard, especially in the context of terminal themes + /// that can change the background color and make hardcoded colors illegible. + /// Such themes typically remap ansi codes properly, treating them more + /// like CSS classes than specific colors. + pub fn unicode() -> Self { + Self { + characters: ThemeCharacters::unicode(), + styles: ThemeStyles::ansi(), + } + } + + /// Graphical theme that draws in monochrome, while still using unicode + /// characters. + pub fn unicode_nocolor() -> Self { + Self { + characters: ThemeCharacters::unicode(), + styles: ThemeStyles::none(), + } + } + + /// A "basic" graphical theme that skips colors and unicode characters and + /// just does monochrome ascii art. If you want a completely non-graphical + /// rendering of your `Diagnostic`s, check out + /// [crate::NarratableReportHandler], or write your own + /// [crate::ReportHandler]! + pub fn none() -> Self { + Self { + characters: ThemeCharacters::ascii(), + styles: ThemeStyles::none(), + } + } +} + +impl Default for GraphicalTheme { + fn default() -> Self { + match std::env::var("NO_COLOR") { + _ if !std::io::stdout().is_terminal() || !std::io::stderr().is_terminal() => { + Self::ascii() + } + Ok(string) if string != "0" => Self::unicode_nocolor(), + _ => Self::unicode(), + } + } +} + +/** +Styles for various parts of graphical rendering for the [crate::GraphicalReportHandler]. +*/ +#[derive(Debug, Clone)] +pub struct ThemeStyles { + /// Style to apply to things highlighted as "error". + pub error: Style, + /// Style to apply to things highlighted as "warning". + pub warning: Style, + /// Style to apply to things highlighted as "advice". + pub advice: Style, + /// Style to apply to the help text. + pub help: Style, + /// Style to apply to filenames/links/URLs. + pub link: Style, + /// Style to apply to line numbers. + pub linum: Style, + /// Styles to cycle through (using `.iter().cycle()`), to render the lines + /// and text for diagnostic highlights. + pub highlights: Vec