Skip to content

Commit

Permalink
feat: print some debugger info only if not --minimal
Browse files Browse the repository at this point in the history
  • Loading branch information
dxrcy committed Dec 1, 2024
1 parent 527acc3 commit 781b5a1
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 14 deletions.
31 changes: 23 additions & 8 deletions src/debugger/print.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ pub struct Writer;
impl io::Write for Writer {
fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
// TODO: Make this good
print(String::from_utf8_lossy(buf).to_string());
print(String::from_utf8_lossy(buf).to_string(), true);
Ok(buf.len())
}

Expand All @@ -34,11 +34,14 @@ pub(super) mod is_minimal {
}
}

pub fn write(f: &mut impl io::Write, string: String) -> Result<(), io::Error> {
pub fn write(f: &mut impl io::Write, string: String, minimal: bool) -> Result<(), io::Error> {
if !is_minimal::get() {
write!(f, "{}", ColoredString::from(string).blue())?;
return Ok(());
}
if !minimal {
return Ok(());
}

let mut chars = string.chars();
while let Some(ch) = chars.next() {
Expand All @@ -52,32 +55,44 @@ pub fn write(f: &mut impl io::Write, string: String) -> Result<(), io::Error> {
Ok(())
}

pub fn print(string: String) {
pub fn print(string: String, minimal: bool) {
let is_line_start = string.chars().next_back() == Some('\n');
write(&mut io::stderr(), string).expect("write to stderr should not fail");
write(&mut io::stderr(), string, minimal).expect("write to stderr should not fail");
terminal_line_start::set(is_line_start);
}

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

#[macro_export]
macro_rules! dprint {
(% $fmt:literal $($tt:tt)* ) => {{
crate::debugger::_print(format!($fmt $($tt)*), false);
}};
( $fmt:literal $($tt:tt)* ) => {{
crate::debugger::_print(format!($fmt $($tt)*));
crate::debugger::_print(format!($fmt $($tt)*), true);
}};
}

#[macro_export]
macro_rules! dprintln {
(%) => {{
crate::debugger::_print("\n".to_string(), false);
}};
(% $fmt:literal $($tt:tt)* ) => {{
crate::debugger::_print(format!(concat!($fmt, "\n") $($tt)*), false);
}};
() => {{
crate::debugger::_print("\n".to_string());
crate::debugger::_print("\n".to_string(), true);
}};
( $fmt:literal $($tt:tt)* ) => {{
crate::debugger::_print(format!(concat!($fmt, "\n") $($tt)*));
crate::debugger::_print(format!(concat!($fmt, "\n") $($tt)*), true);
}};
}
6 changes: 3 additions & 3 deletions src/debugger/source.rs
Original file line number Diff line number Diff line change
Expand Up @@ -80,8 +80,8 @@ impl SourceReader for SourceMode {
};
// Echo prompt and command for non-terminal source
// Equivalent code found in terminal source
dprint!("\x1b[1mCommand: ");
dprintln!("{}", command.unwrap_or("\x1b[3m(end of input)").trim());
dprint!(%"\x1b[1mCommand: ");
dprintln!(%"{}", command.unwrap_or("\x1b[3m(end of input)").trim());
command
}
}
Expand Down Expand Up @@ -251,7 +251,7 @@ impl Terminal {

// Print prompt and current input
// Equivalent code found in non-terminal source
dwrite!(&mut self.term, "\x1b[1mCommand: \x1b[0m").unwrap();
dwrite!(% &mut self.term, "\x1b[1mCommand: \x1b[0m").unwrap();

// Inline `self.get_current()` due to borrowing issues
let current = if self.is_next() {
Expand Down
6 changes: 3 additions & 3 deletions src/runtime.rs
Original file line number Diff line number Diff line change
Expand Up @@ -140,10 +140,10 @@ impl RunEnvironment {
loop {
if let Some(debugger) = &mut self.debugger {
if !terminal_line_start::get() {
dprintln!();
dprintln!(%);
}
dprintln!();
dprintln!("Program counter at: 0x{:04x}", self.state.pc);
dprintln!(%);
dprintln!(%"Program counter at: 0x{:04x}", self.state.pc);
match debugger.wait_for_action(&mut self.state) {
Action::Proceed => (),
Action::StopDebugger => {
Expand Down

0 comments on commit 781b5a1

Please sign in to comment.