error_stack/fmt/color.rs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330
use core::fmt::{self, Display, Formatter};
use crate::{
fmt::r#override::{AtomicOverride, AtomicPreference},
Report,
};
/// The available modes of color support
///
/// Can be accessed through [`crate::fmt::HookContext::color_mode`], and set via
/// [`Report::set_color_mode`]
#[derive(Debug, Copy, Clone, Eq, PartialEq, Hash, Default)]
pub enum ColorMode {
/// User preference to disable all colors
None,
/// User preference to enable colors
Color,
/// User preference to enable styles, but discourage colors
///
/// This is the same as [`ColorMode::Color`], but signals to the user that while colors are
/// supported, the user prefers instead the use of emphasis, like bold and italic text.
// The default is `ColorMode::Emphasis`, because colors are hard. ANSI colors are not
// standardized, and some colors may not show at all.
#[default]
Emphasis,
}
impl ColorMode {
pub(super) fn load() -> Self {
COLOR_OVERRIDE.load()
}
}
/// Value layout:
/// `0x00`: `ColorMode::None`
/// `0x01`: `ColorMode::Color`
/// `0x02`: `ColorMode::Emphasis`
///
/// all others: [`Self::default`]
impl AtomicPreference for ColorMode {
fn from_u8(value: u8) -> Self {
match value {
0x00 => Self::None,
0x01 => Self::Color,
0x02 => Self::Emphasis,
_ => Self::default(),
}
}
fn into_u8(self) -> u8 {
match self {
Self::None => 0x00,
Self::Color => 0x01,
Self::Emphasis => 0x02,
}
}
}
static COLOR_OVERRIDE: AtomicOverride<ColorMode> = AtomicOverride::new();
impl Report<()> {
/// Set the color mode preference
///
/// If no [`ColorMode`] is set, it defaults to [`ColorMode::Emphasis`].
///
/// # Example
///
/// ```rust
/// # // we only test the snapshot on nightly, therefore report is unused (so is render)
/// # #![cfg_attr(not(nightly), allow(dead_code, unused_variables, unused_imports))]
/// use std::io::{Error, ErrorKind};
/// use owo_colors::OwoColorize;
///
/// use error_stack::{report, Report};
/// use error_stack::fmt::ColorMode;
///
/// struct Suggestion(&'static str);
///
/// Report::install_debug_hook::<Suggestion>(|Suggestion(value), context| {
/// let body = format!("suggestion: {value}");
/// match context.color_mode() {
/// ColorMode::Color => context.push_body(body.green().to_string()),
/// ColorMode::Emphasis => context.push_body(body.italic().to_string()),
/// ColorMode::None => context.push_body(body)
/// };
/// });
///
/// let report =
/// report!(Error::from(ErrorKind::InvalidInput)).attach(Suggestion("oh no, try again"));
///
/// # fn render(value: String) -> String {
/// # let backtrace = regex::Regex::new(r"backtrace no\. (\d+)\n(?: .*\n)* .*").unwrap();
/// # let backtrace_info = regex::Regex::new(r"backtrace( with (\d+) frames)? \((\d+)\)").unwrap();
/// #
/// # let value = backtrace.replace_all(&value, "backtrace no. $1\n [redacted]");
/// # let value = backtrace_info.replace_all(value.as_ref(), "backtrace ($3)");
/// #
/// # ansi_to_html::convert(value.as_ref()).unwrap()
/// # }
/// #
/// Report::set_color_mode(ColorMode::None);
/// println!("{report:?}");
/// # #[cfg(nightly)]
/// # expect_test::expect_file![concat!(env!("CARGO_MANIFEST_DIR"), "/tests/snapshots/doc/fmt__preference_none.snap")].assert_eq(&render(format!("{report:?}")));
///
/// Report::set_color_mode(ColorMode::Emphasis);
/// println!("{report:?}");
/// # #[cfg(nightly)]
/// # expect_test::expect_file![concat!(env!("CARGO_MANIFEST_DIR"), "/tests/snapshots/doc/fmt__preference_emphasis.snap")].assert_eq(&render(format!("{report:?}")));
///
/// Report::set_color_mode(ColorMode::Color);
/// println!("{report:?}");
/// # #[cfg(nightly)]
/// # expect_test::expect_file![concat!(env!("CARGO_MANIFEST_DIR"), "/tests/snapshots/doc/fmt__preference_color.snap")].assert_eq(&render(format!("{report:?}")));
/// ```
///
/// Which will result in something like:
///
/// <pre>
#[doc = include_str!(concat!(env!("CARGO_MANIFEST_DIR"), "/tests/snapshots/doc/fmt__preference_none.snap"))]
/// </pre>
///
/// <pre>
#[doc = include_str!(concat!(env!("CARGO_MANIFEST_DIR"), "/tests/snapshots/doc/fmt__preference_emphasis.snap"))]
/// </pre>
///
/// <pre>
#[doc = include_str!(concat!(env!("CARGO_MANIFEST_DIR"), "/tests/snapshots/doc/fmt__preference_color.snap"))]
/// </pre>
pub fn set_color_mode(mode: ColorMode) {
COLOR_OVERRIDE.store(mode);
}
}
#[derive(Copy, Clone, Debug, Eq, PartialEq)]
pub(crate) enum Color {
Black,
Red,
}
impl Color {
const fn digit(self) -> u8 {
match self {
Self::Black => b'0',
Self::Red => b'1',
}
}
}
#[derive(Copy, Clone, Debug, Eq, PartialEq)]
pub(crate) struct Foreground {
color: Color,
bright: bool,
}
impl Foreground {
fn start_ansi(self, sequence: &mut ControlSequence) -> fmt::Result {
let Self { color, bright } = self;
let buffer = &[if bright { b'9' } else { b'3' }, color.digit()];
// This should never fail because both are valid ASCII
let control = core::str::from_utf8(buffer).expect("should be valid utf-8 buffer");
sequence.push_control(control)
}
fn end_ansi(sequence: &mut ControlSequence) -> fmt::Result {
sequence.push_control("39")
}
}
struct ControlSequence<'a, 'b> {
fmt: &'a mut Formatter<'b>,
empty: bool,
}
impl<'a, 'b> ControlSequence<'a, 'b> {
fn new(fmt: &'a mut Formatter<'b>) -> Self {
Self { fmt, empty: true }
}
fn finish(self) -> Result<&'a mut Formatter<'b>, fmt::Error> {
if !self.empty {
// we wrote a specific formatting character, therefore we need to end
self.fmt.write_str("m")?;
}
Ok(self.fmt)
}
}
impl ControlSequence<'_, '_> {
fn push_control(&mut self, control: &str) -> fmt::Result {
if self.empty {
self.fmt.write_str("\u{1b}[")?;
} else {
self.fmt.write_str(";")?;
}
self.fmt.write_str(control)?;
self.empty = false;
Ok(())
}
}
#[derive(Copy, Clone, Debug, Eq, PartialEq)]
pub(crate) struct DisplayStyle {
bold: bool,
italic: bool,
}
impl DisplayStyle {
pub(crate) const fn new() -> Self {
Self {
bold: false,
italic: false,
}
}
pub(crate) fn set_bold(&mut self, value: bool) {
self.bold = value;
}
pub(crate) fn with_bold(mut self, value: bool) -> Self {
self.set_bold(value);
self
}
pub(crate) fn set_italic(&mut self, value: bool) {
self.italic = value;
}
pub(crate) fn with_italic(mut self, value: bool) -> Self {
self.set_italic(value);
self
}
}
impl DisplayStyle {
fn start_ansi(self, sequence: &mut ControlSequence) -> fmt::Result {
if self.bold {
sequence.push_control("1")?;
}
if self.italic {
sequence.push_control("3")?;
}
Ok(())
}
fn end_ansi(self, sequence: &mut ControlSequence) -> fmt::Result {
if self.bold {
sequence.push_control("22")?;
}
if self.italic {
sequence.push_control("23")?;
}
Ok(())
}
}
#[derive(Default, Debug, Copy, Clone, Eq, PartialEq)]
pub(crate) struct Style {
display: Option<DisplayStyle>,
foreground: Option<Foreground>,
}
impl Style {
pub(crate) const fn new() -> Self {
Self {
display: None,
foreground: None,
}
}
pub(crate) const fn apply<T: Display>(self, value: &T) -> StyleDisplay<T> {
StyleDisplay { style: self, value }
}
pub(crate) fn set_foreground(&mut self, color: Color, bright: bool) {
self.foreground = Some(Foreground { color, bright });
}
pub(crate) fn set_display(&mut self, style: DisplayStyle) {
self.display = Some(style);
}
}
pub(crate) struct StyleDisplay<'a, T: Display> {
style: Style,
value: &'a T,
}
impl<'a, T: Display> Display for StyleDisplay<'a, T> {
fn fmt(&self, mut f: &mut Formatter<'_>) -> fmt::Result {
let mut sequence = ControlSequence::new(f);
if let Some(display) = self.style.display {
display.start_ansi(&mut sequence)?;
}
if let Some(foreground) = self.style.foreground {
foreground.start_ansi(&mut sequence)?;
}
f = sequence.finish()?;
Display::fmt(&self.value, f)?;
let mut sequence = ControlSequence::new(f);
if let Some(display) = self.style.display {
display.end_ansi(&mut sequence)?;
}
if self.style.foreground.is_some() {
Foreground::end_ansi(&mut sequence)?;
}
sequence.finish()?;
Ok(())
}
}