-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
`dbg!` on types which are Graphviz will print out the object in dot format. Most types will only implement visualize or stringify. A way of getting distinguishable colors is also provided.
- Loading branch information
Showing
4 changed files
with
136 additions
and
1 deletion.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
47 changes: 47 additions & 0 deletions
47
ti-basic-optimizer/src/analyze/control_flow/graph/graphviz.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
use std::fmt::Debug; | ||
|
||
use dot_writer::{DotWriter, Scope}; | ||
|
||
use equidistributed_colors::EquiColor; | ||
|
||
static mut COLORS: Option<EquiColor> = None; | ||
|
||
pub fn next_color() -> String { | ||
let color = unsafe { | ||
if COLORS.is_none() { | ||
COLORS = Some(EquiColor::new(0.5, 0.5)) | ||
} | ||
|
||
COLORS.unwrap().next().unwrap() | ||
}; | ||
|
||
format!("#{:02X}{:02X}{:02X}", color.r, color.g, color.b) | ||
} | ||
|
||
pub trait Visualize<T: Sized> { | ||
fn visualize(&self, context: &mut Scope, config: T); | ||
} | ||
|
||
pub trait Graphviz { | ||
fn graphviz(&self, writer: &mut DotWriter); | ||
} | ||
|
||
impl Debug for dyn Graphviz { | ||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { | ||
let mut output_bytes = Vec::new(); | ||
{ | ||
let mut writer = DotWriter::from(&mut output_bytes); | ||
|
||
self.graphviz(&mut writer); | ||
} | ||
|
||
f.write_str(&String::from_utf8(output_bytes).unwrap()) | ||
} | ||
} | ||
|
||
pub fn escape(string: &str) -> String { | ||
string | ||
.replace('\\', "\\\\") | ||
.replace('\n', "\\n") | ||
.replace('"', "\\\"") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters