color_your_life/
lib.rs

1//!
2
3pub use ansi_term::Color;
4use std::fmt::Write;
5
6pub trait ColorDisplay<F> {
7    const INDENTATION: &'static str = "    ";
8    const NEWLINE: &'static str = "\n";
9
10    fn color_fmt(
11        &self,
12        sink: &mut impl Write,
13        format: &F,
14    ) -> std::fmt::Result;
15
16    /// Utility method to simpify writing the proper amount of indentation.
17    /// In order to print the right indentation token, it takes into account
18    /// the implementing type as well as the format type `F`.
19    fn write_indentation(
20        &self,
21        sink: &mut impl Write,
22        count: u16,
23        _: &F,
24    ) -> std::fmt::Result {
25        for _ in 0..count {
26            write!(sink, "{}", <Self as ColorDisplay<F>>::INDENTATION)?;
27        }
28        Ok(())
29    }
30
31    /// Utility method to simpify writing newlines.
32    /// In order to print the right newline character(s), it takes into
33    /// account the implementing type as well as the format type `F`.
34    fn write_newlines(
35        &self,
36        sink: &mut impl Write,
37        count: u16,
38        _: &F,
39    ) -> std::fmt::Result {
40        for _ in 0..count {
41            write!(sink, "{}", <Self as ColorDisplay<F>>::NEWLINE)?;
42        }
43        Ok(())
44    }
45}
46
47pub trait Format {
48    fn colored(indent: u16) -> Self;
49
50    fn monochrome(indent: u16) -> Self;
51}
52
53#[derive(Clone, Copy, PartialEq)]
54pub struct StyleDesc {
55    pub color: Color,
56    pub bold: bool,
57    pub italic: bool,
58    pub underline: bool,
59    pub dimmed: bool,
60}
61
62pub fn compute_leaf_style(
63    desc: impl Into<Option<StyleDesc>>
64) -> ansi_term::Style {
65    if let Some(desc) = desc.into() {
66        let style = desc.color.normal();
67        let style = if desc.bold      { style.bold()      } else { style };
68        let style = if desc.italic    { style.italic()    } else { style };
69        let style = if desc.underline { style.underline() } else { style };
70        let style = if desc.dimmed    { style.dimmed()    } else { style };
71        style
72    } else {
73        ansi_term::Style::default()
74    }
75}
76
77pub mod bool;
78pub mod char;
79pub mod btree_map;
80pub mod btree_set;
81pub mod hash_map;
82pub mod hash_set;
83pub mod primitive;
84pub mod result;
85pub mod slice;
86pub mod str;
87pub mod vec;
88pub mod vec_deque;