Capture pretty code-frames.
capture_codeframe!()
makes use of line! and file! to capture the codeframe from the place it was originally invoked.
Imagine having a macro assert_equal!(left, right)
that checks whether left is equal to right. We can use capture_codeframe!()
to get code-frame of assert_equal!(left, right)
invocation with some context.
It also accepts Color
argument (capture_codeframe!(Color::Blue)
) which will default to Color::Red
.
```Rust use codeframe::{capture_codeframe, Color};
macrorules! assertequal { ($left:expr, $right:expr) => {{ if $left != $right { let codeframe = capture_codeframe!(Color::Red); println!("Left does not match Right"); if let Some(codeframe) = codeframe { println!("{}", codeframe) } } else { println!("Left and right are equal"); } }}; }
fn withcontext() { super::setuptestenv(); assertequal!(1, 2); }
```
Note let codeframe = capture_codeframe!(Color::Red);
in the assert_equal
macro. This captures the code-frame where it was originally invoked. In our case, assert_equal!(1, 2);
. So the output would be:
capture_codeframe!()
capture_codeframe!(Color::Red)
capture_codeframe!(Color::Blue)
View currently supported colors
You can also capture codeframes with code snippet by making use of the Builder Pattern.
```Rust let rawlines = "macrorules! testsimplestyle { ($string:expr, $style:ident) => { #[test] fn $style() { asserteq!( s.$style().tostring(), ansiterm::Style::new().$style().paint(s).tostring() ) } }; }".toowned(); let codeframe = Codeframe::new(rawlines, 5).set_color(Color::Red).capture();
if let Some(codeframe) = codeframe { println!("{}", codeframe) } ```
The Builder takes raw lines and line number(to highlight) as mandatory arguments. You can additionaly set the highlight color using set_color(Color::Red)
. This will result with the following:
Codeframe::new(raw_lines, 5).capture();
Codeframe::new(raw_lines, 5).set_color(Color::Red).capture();
Codeframe::new(raw_lines, 5).set_color(Color::Blue).capture();
View currently supported colors
Option<String>