Skip to content

Commit 537153d

Browse files
committed
Remove the utils in emu and merge it into the main utils
1 parent 9937d4f commit 537153d

File tree

5 files changed

+88
-154
lines changed

5 files changed

+88
-154
lines changed

crates/libmwemu/src/emu/mod.rs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,8 +49,6 @@ mod threading;
4949
mod tls;
5050
mod trace;
5151
mod winapi;
52-
#[macro_use]
53-
mod utils;
5452

5553
pub struct Emu {
5654
// Global/shared state

crates/libmwemu/src/emu/utils.rs

Lines changed: 0 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -1,67 +0,0 @@
1-
#[macro_export]
2-
macro_rules! color {
3-
("Black") => {
4-
"\x1b[0;30m"
5-
};
6-
("Red") => {
7-
"\x1b[0;31m"
8-
};
9-
("Green") => {
10-
"\x1b[0;32m"
11-
};
12-
("Orange") => {
13-
"\x1b[0;33m"
14-
};
15-
("Blue") => {
16-
"\x1b[0;34m"
17-
};
18-
("Purple") => {
19-
"\x1b[0;35m"
20-
};
21-
("Cyan") => {
22-
"\x1b[0;36m"
23-
};
24-
("LightGray") => {
25-
"\x1b[0;37m"
26-
};
27-
("DarkGray") => {
28-
"\x1b[1;30m"
29-
};
30-
("LightRed") => {
31-
"\x1b[1;31m"
32-
};
33-
("LightGreen") => {
34-
"\x1b[1;32m"
35-
};
36-
("Yellow") => {
37-
"\x1b[1;33m"
38-
};
39-
("LightBlue") => {
40-
"\x1b[1;34m"
41-
};
42-
("LightPurple") => {
43-
"\x1b[1;35m"
44-
};
45-
("LightCyan") => {
46-
"\x1b[1;36m"
47-
};
48-
("White") => {
49-
"\x1b[1;37m"
50-
};
51-
("nc") => {
52-
"\x1b[0m"
53-
};
54-
("ClearScreen") => {
55-
"\x1bc"
56-
};
57-
($unknown:tt) => {
58-
compile_error!(concat!(
59-
"Unknown color name: '",
60-
$unknown,
61-
"'. Valid options are: \
62-
Black, Red, Green, Orange, Blue, Purple, Cyan, LightGray, \
63-
DarkGray, LightRed, LightGreen, Yellow, LightBlue, \
64-
LightPurple, LightCyan, White, nc, ClearScreen"
65-
))
66-
};
67-
}

crates/libmwemu/src/emu/utils/mod.rs

Lines changed: 0 additions & 84 deletions
This file was deleted.

crates/libmwemu/src/lib.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,10 +42,12 @@ pub mod threading;
4242
pub mod tracing;
4343
pub mod winapi;
4444

45+
// re-export the helper so the macro can reach it
46+
pub use utils::color_enabled;
47+
4548
#[cfg(test)]
4649
mod tests;
4750
mod utils;
48-
4951
use config::Config;
5052
use emu::Emu;
5153

crates/libmwemu/src/utils.rs

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
use std::sync::atomic::{AtomicBool, Ordering};
2+
3+
static COLOR_ENABLED: AtomicBool = AtomicBool::new(true);
4+
15
// TODO: remove the code when 'likely' and 'unlikely' are stable
26
#[inline(always)]
37
#[cold]
@@ -21,4 +25,85 @@ pub(crate) fn unlikely(b: bool) -> bool {
2125
} else {
2226
false
2327
}
28+
}
29+
30+
pub fn disable_color() {
31+
COLOR_ENABLED.store(false, Ordering::Relaxed);
32+
}
33+
34+
pub fn enable_color() {
35+
COLOR_ENABLED.store(true, Ordering::Relaxed);
36+
}
37+
38+
#[inline]
39+
pub fn color_enabled() -> bool {
40+
COLOR_ENABLED.load(Ordering::Relaxed)
41+
}
42+
43+
#[macro_export]
44+
macro_rules! color {
45+
("Black") => {
46+
if $crate::color_enabled() { "\x1b[0;30m" } else { "" }
47+
};
48+
("Red") => {
49+
if $crate::color_enabled() { "\x1b[0;31m" } else { "" }
50+
};
51+
("Green") => {
52+
if $crate::color_enabled() { "\x1b[0;32m" } else { "" }
53+
};
54+
("Orange") => {
55+
if $crate::color_enabled() { "\x1b[0;33m" } else { "" }
56+
};
57+
("Blue") => {
58+
if $crate::color_enabled() { "\x1b[0;34m" } else { "" }
59+
};
60+
("Purple") => {
61+
if $crate::color_enabled() { "\x1b[0;35m" } else { "" }
62+
};
63+
("Cyan") => {
64+
if $crate::color_enabled() { "\x1b[0;36m" } else { "" }
65+
};
66+
("LightGray") => {
67+
if $crate::color_enabled() { "\x1b[0;37m" } else { "" }
68+
};
69+
("DarkGray") => {
70+
if $crate::color_enabled() { "\x1b[1;30m" } else { "" }
71+
};
72+
("LightRed") => {
73+
if $crate::color_enabled() { "\x1b[1;31m" } else { "" }
74+
};
75+
("LightGreen") => {
76+
if $crate::color_enabled() { "\x1b[1;32m" } else { "" }
77+
};
78+
("Yellow") => {
79+
if $crate::color_enabled() { "\x1b[1;33m" } else { "" }
80+
};
81+
("LightBlue") => {
82+
if $crate::color_enabled() { "\x1b[1;34m" } else { "" }
83+
};
84+
("LightPurple") => {
85+
if $crate::color_enabled() { "\x1b[1;35m" } else { "" }
86+
};
87+
("LightCyan") => {
88+
if $crate::color_enabled() { "\x1b[1;36m" } else { "" }
89+
};
90+
("White") => {
91+
if $crate::color_enabled() { "\x1b[1;37m" } else { "" }
92+
};
93+
("nc") => {
94+
if $crate::color_enabled() { "\x1b[0m" } else { "" }
95+
};
96+
("ClearScreen") => {
97+
if $crate::color_enabled() { "\x1bc" } else { "" }
98+
};
99+
($unknown:tt) => {
100+
compile_error!(concat!(
101+
"Unknown color name: '",
102+
$unknown,
103+
"'. Valid options are: \
104+
Black, Red, Green, Orange, Blue, Purple, Cyan, LightGray, \
105+
DarkGray, LightRed, LightGreen, Yellow, LightBlue, \
106+
LightPurple, LightCyan, White, nc, ClearScreen"
107+
))
108+
};
24109
}

0 commit comments

Comments
 (0)