Skip to content

Commit

Permalink
lint: tidy debugger::print module
Browse files Browse the repository at this point in the history
  • Loading branch information
dxrcy committed Nov 28, 2024
1 parent fc03139 commit f489aa0
Showing 1 changed file with 21 additions and 17 deletions.
38 changes: 21 additions & 17 deletions src/debugger/print.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,25 +8,29 @@ use crate::runtime::terminal_cursor;
thread_local! {
static IS_MINIMAL: RefCell<bool> = const { RefCell::new(false) };
}
/// May be called multiple times
pub fn set_is_minimal(value: bool) {
IS_MINIMAL.with(|minimal| *minimal.borrow_mut() = value);
}
/// May be called before `set_is_minimal`
fn is_minimal() -> bool {
IS_MINIMAL.with(|minimal| *minimal.borrow())
}

pub fn write(f: &mut impl io::Write, string: String) -> Result<(), io::Error> {
if is_minimal() {
let mut chars = string.chars();
while let Some(ch) = chars.next() {
if ch == '\x1b' {
while chars.next().is_some_and(|ch| ch != 'm') {}
continue;
}
write!(f, "{}", ch)?;
}
} else {
if !is_minimal() {
write!(f, "{}", ColoredString::from(string).blue())?;
return Ok(());
}

let mut chars = string.chars();
while let Some(ch) = chars.next() {
// Skip everything between '\x1b' and 'm' (inclusive)
if ch == '\x1b' {
while chars.next().is_some_and(|ch| ch != 'm') {}
continue;
}
write!(f, "{}", ch)?;
}
Ok(())
}
Expand All @@ -37,6 +41,13 @@ pub fn print(string: String) {
terminal_cursor::set_line_start(is_line_start);
}

#[macro_export]
macro_rules! dwrite {
( $f:expr, $fmt:literal $($tt:tt)* ) => {{
crate::debugger::_write($f, format!($fmt $($tt)*))
}};
}

#[macro_export]
macro_rules! dprint {
( $fmt:literal $($tt:tt)* ) => {{
Expand All @@ -53,10 +64,3 @@ macro_rules! dprintln {
crate::debugger::_print(format!(concat!($fmt, "\n") $($tt)*));
}};
}

#[macro_export]
macro_rules! dwrite {
( $f:expr, $fmt:literal $($tt:tt)* ) => {{
crate::debugger::_write($f, format!($fmt $($tt)*))
}};
}

0 comments on commit f489aa0

Please sign in to comment.