cfonts/debug.rs
1//! The contents of this module is all about debugging
2//!
3//! We use the `debug_level` of [`Options`] to toggle visibility of debug messages
4//!
5//! The debug message _may_ later go into a file you can send to in for analysis.
6use crate::color::{color, get_background_color};
7use crate::config::{BgColors, Colors, Options};
8
9/// The `Dt` enum includes all debug types
10pub enum Dt {
11 /// A headline
12 Head,
13 /// A log
14 Log,
15 /// An error
16 Error,
17}
18
19/// The `d` function is for all debug calls
20///
21/// Calling `d` with a `level` will allow the system to hide of show debug messages based on the [`Options`].debug_level
22///
23/// ```rust
24/// extern crate cfonts;
25///
26/// use cfonts::{ Options };
27/// use cfonts::debug::{d, Dt};
28///
29/// let options = Options::default();
30/// d("My debug message for this header", 1, Dt::Head, &options, &mut std::io::stdout());
31///
32/// // only shows up on debug_level > 1
33/// d("My debug message", 2, Dt::Log, &options, &mut std::io::stdout());
34///
35/// // only shows up on debug_level > 3
36/// d("My error debug message", 4, Dt::Error, &options, &mut std::io::stdout());
37/// ```
38pub fn d(text: &str, level: u16, debug_type: Dt, options: &Options, stdout: &mut dyn std::io::Write) {
39 if !options.debug || level > options.debug_level {
40 // we discard everything if debug is disabled or if the set level is not deep enough
41 } else {
42 match debug_type {
43 Dt::Head => {
44 let (bg_start, bg_end) = get_background_color(&BgColors::Yellow);
45 writeln!(stdout, "{}\n {} {}", bg_start, color(text, Colors::Black), bg_end).unwrap_or(());
46 }
47 Dt::Log => {
48 writeln!(stdout, " {}", color(text, Colors::Yellow)).unwrap_or(());
49 }
50 Dt::Error => {
51 let (bg_start, bg_end) = get_background_color(&BgColors::Red);
52 writeln!(stdout, "{}{}{} {}", bg_start, color(" ERROR ", Colors::White), bg_end, color(text, Colors::Red))
53 .unwrap_or(());
54 }
55 }
56 }
57}