stylus_tools/utils/
color.rs

1// Copyright 2020-2025, Offchain Labs, Inc.
2// For licensing, see https://github.com/OffchainLabs/cargo-stylus/blob/main/licenses/COPYRIGHT.md
3
4#![allow(unused)]
5
6use std::fmt::{Debug, Display};
7
8pub const BLUE: &str = "\x1b[34;1m";
9pub const DIM: &str = "\x1b[2m";
10pub const GREY: &str = "\x1b[0;0m\x1b[90m";
11pub const MINT: &str = "\x1b[38;5;48;1m";
12pub const PINK: &str = "\x1b[38;5;161;1m";
13pub const RED: &str = "\x1b[31;1m";
14pub const CLEAR: &str = "\x1b[0;0m";
15pub const WHITE: &str = "\x1b[0;1m";
16pub const YELLOW: &str = "\x1b[33;1m";
17pub const LAVENDER: &str = "\x1b[38;5;183;1m";
18
19pub trait Color {
20    fn color(&self, color: &str) -> String;
21
22    fn blue(&self) -> String;
23    fn dim(&self) -> String;
24    fn clear(&self) -> String;
25    fn grey(&self) -> String;
26    fn mint(&self) -> String;
27    fn pink(&self) -> String;
28    fn red(&self) -> String;
29    fn white(&self) -> String;
30    fn yellow(&self) -> String;
31    fn lavender(&self) -> String;
32}
33
34#[rustfmt::skip]
35impl<T> Color for T where T: Display {
36    fn color(&self, color: &str) -> String {
37        format!("{color}{self}{CLEAR}")
38    }
39
40    fn blue(&self)     -> String { self.color(BLUE)     }
41    fn dim(&self)      -> String { self.color(DIM)      }
42    fn clear(&self)    -> String { self.color(CLEAR)    }
43    fn grey(&self)     -> String { self.color(GREY)     }
44    fn mint(&self)     -> String { self.color(MINT)     }
45    fn pink(&self)     -> String { self.color(PINK)     }
46    fn red(&self)      -> String { self.color(RED)      }
47    fn white(&self)    -> String { self.color(WHITE)    }
48    fn yellow(&self)   -> String { self.color(YELLOW)   }
49    fn lavender(&self) -> String { self.color(LAVENDER) }
50}
51
52pub fn when<T: Display>(cond: bool, text: T, when_color: &str) -> String {
53    match cond {
54        true => text.color(when_color),
55        false => format!("{text}"),
56    }
57}
58
59pub trait DebugColor {
60    fn debug_color(&self, color: &str) -> String;
61
62    fn debug_blue(&self) -> String;
63    fn debug_dim(&self) -> String;
64    fn debug_clear(&self) -> String;
65    fn debug_grey(&self) -> String;
66    fn debug_mint(&self) -> String;
67    fn debug_pink(&self) -> String;
68    fn debug_red(&self) -> String;
69    fn debug_white(&self) -> String;
70    fn debug_yellow(&self) -> String;
71    fn debug_lavender(&self) -> String;
72}
73
74#[rustfmt::skip]
75impl<T> DebugColor for T where T: Debug {
76    fn debug_color(&self, color: &str) -> String {
77        format!("{color}{self:?}{CLEAR}")
78    }
79
80    fn debug_blue(&self)     -> String { self.debug_color(BLUE)     }
81    fn debug_dim(&self)      -> String { self.debug_color(DIM)      }
82    fn debug_clear(&self)    -> String { self.debug_color(CLEAR)    }
83    fn debug_grey(&self)     -> String { self.debug_color(GREY)     }
84    fn debug_mint(&self)     -> String { self.debug_color(MINT)     }
85    fn debug_pink(&self)     -> String { self.debug_color(PINK)     }
86    fn debug_red(&self)      -> String { self.debug_color(RED)      }
87    fn debug_white(&self)    -> String { self.debug_color(WHITE)    }
88    fn debug_yellow(&self)   -> String { self.debug_color(YELLOW)   }
89    fn debug_lavender(&self) -> String { self.debug_color(LAVENDER) }
90}