From f489aa030f3b2829de942859ed42570a4835cd08 Mon Sep 17 00:00:00 2001 From: darcy Date: Thu, 28 Nov 2024 12:09:46 +1100 Subject: [PATCH] lint: tidy `debugger::print` module --- src/debugger/print.rs | 38 +++++++++++++++++++++----------------- 1 file changed, 21 insertions(+), 17 deletions(-) diff --git a/src/debugger/print.rs b/src/debugger/print.rs index 3f44484..86ca52f 100644 --- a/src/debugger/print.rs +++ b/src/debugger/print.rs @@ -8,25 +8,29 @@ use crate::runtime::terminal_cursor; thread_local! { static IS_MINIMAL: RefCell = 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(()) } @@ -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)* ) => {{ @@ -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)*)) - }}; -}