use std::marker::PhantomData;
use itertools::Itertools;
use super::output::OutputRendererOptions;
use super::render::Ancestor;
use super::render::GraphRow;
use super::render::LinkLine;
use super::render::NodeLine;
use super::render::PadLine;
use super::render::Renderer;
mod glyph {
pub(super) const SPACE: usize = 0;
pub(super) const HORIZONTAL: usize = 1;
pub(super) const PARENT: usize = 2;
pub(super) const ANCESTOR: usize = 3;
pub(super) const MERGE_LEFT: usize = 4;
pub(super) const MERGE_RIGHT: usize = 5;
pub(super) const MERGE_BOTH: usize = 6;
pub(super) const FORK_LEFT: usize = 7;
pub(super) const FORK_RIGHT: usize = 8;
pub(super) const FORK_BOTH: usize = 9;
pub(super) const JOIN_LEFT: usize = 10;
pub(super) const JOIN_RIGHT: usize = 11;
pub(super) const JOIN_BOTH: usize = 12;
pub(super) const TERMINATION: usize = 13;
pub(super) const COUNT: usize = 14;
}
const SQUARE_GLYPHS: [&str; glyph::COUNT] = [
" ", "โโ", "โ ", "ยท ", "โ ", "โโ", "โดโ", "โ ", "โโ", "โฌโ", "โค ", "โโ", "โผโ", "~ ",
];
const CURVED_GLYPHS: [&str; glyph::COUNT] = [
" ", "โโ", "โ ", "โท ", "โฏ ", "โฐโ", "โดโ", "โฎ ", "โญโ", "โฌโ", "โค ", "โโ", "โผโ", "~ ",
];
const DEC_GLYPHS: [&str; glyph::COUNT] = [
" ",
"\x1B(0qq\x1B(B",
"\x1B(0x \x1B(B",
"\x1B(0~ \x1B(B",
"\x1B(0j \x1B(B",
"\x1B(0mq\x1B(B",
"\x1B(0vq\x1B(B",
"\x1B(0k \x1B(B",
"\x1B(0lq\x1B(B",
"\x1B(0wq\x1B(B",
"\x1B(0u \x1B(B",
"\x1B(0tq\x1B(B",
"\x1B(0nq\x1B(B",
"~ ",
];
impl PadLine {
fn to_glyph(&self) -> usize {
match *self {
PadLine::Parent => glyph::PARENT,
PadLine::Ancestor => glyph::ANCESTOR,
PadLine::Blank => glyph::SPACE,
}
}
}
pub struct BoxDrawingRenderer<N, R>
where
R: Renderer<N, Output = GraphRow<N>> + Sized,
{
inner: R,
options: OutputRendererOptions,
extra_pad_line: Option<String>,
glyphs: &'static [&'static str; glyph::COUNT],
_phantom: PhantomData<N>,
}
impl<N, R> BoxDrawingRenderer<N, R>
where
R: Renderer<N, Output = GraphRow<N>> + Sized,
{
pub(crate) fn new(inner: R, options: OutputRendererOptions) -> Self {
BoxDrawingRenderer {
inner,
options,
extra_pad_line: None,
glyphs: &CURVED_GLYPHS,
_phantom: PhantomData,
}
}
pub fn with_square_glyphs(mut self) -> Self {
self.glyphs = &SQUARE_GLYPHS;
self
}
pub fn with_dec_graphics_glyphs(mut self) -> Self {
self.glyphs = &DEC_GLYPHS;
self
}
}
impl<N, R> Renderer<N> for BoxDrawingRenderer<N, R>
where
N: Clone + Eq,
R: Renderer<N, Output = GraphRow<N>> + Sized,
{
type Output = String;
fn width(&self, node: Option<&N>, parents: Option<&Vec<Ancestor<N>>>) -> u64 {
self.inner
.width(node, parents)
.saturating_mul(2)
.saturating_add(1)
}
fn reserve(&mut self, node: N) {
self.inner.reserve(node);
}
fn next_row(
&mut self,
node: N,
parents: Vec<Ancestor<N>>,
glyph: String,
message: String,
) -> String {
let glyphs = self.glyphs;
let line = self.inner.next_row(node, parents, glyph, message);
let mut out = String::new();
let mut message_lines = line
.message
.lines()
.pad_using(self.options.min_row_height, |_| "");
let mut need_extra_pad_line = false;
if let Some(extra_pad_line) = self.extra_pad_line.take() {
out.push_str(extra_pad_line.trim_end());
out.push('\n');
}
let mut node_line = String::new();
for entry in line.node_line.iter() {
match entry {
NodeLine::Node => {
node_line.push_str(&line.glyph);
node_line.push(' ');
}
NodeLine::Parent => node_line.push_str(glyphs[glyph::PARENT]),
NodeLine::Ancestor => node_line.push_str(glyphs[glyph::ANCESTOR]),
NodeLine::Blank => node_line.push_str(glyphs[glyph::SPACE]),
}
}
if let Some(msg) = message_lines.next() {
node_line.push(' ');
node_line.push_str(msg);
}
out.push_str(node_line.trim_end());
out.push('\n');
#[allow(clippy::if_same_then_else)]
if let Some(link_row) = line.link_line {
let mut link_line = String::new();
for cur in link_row.iter() {
if cur.intersects(LinkLine::HORIZONTAL) {
if cur.intersects(LinkLine::CHILD) {
link_line.push_str(glyphs[glyph::JOIN_BOTH]);
} else if cur.intersects(LinkLine::ANY_FORK)
&& cur.intersects(LinkLine::ANY_MERGE)
{
link_line.push_str(glyphs[glyph::JOIN_BOTH]);
} else if cur.intersects(LinkLine::ANY_FORK)
&& cur.intersects(LinkLine::VERT_PARENT)
&& !line.merge
{
link_line.push_str(glyphs[glyph::JOIN_BOTH]);
} else if cur.intersects(LinkLine::ANY_FORK) {
link_line.push_str(glyphs[glyph::FORK_BOTH]);
} else if cur.intersects(LinkLine::ANY_MERGE) {
link_line.push_str(glyphs[glyph::MERGE_BOTH]);
} else {
link_line.push_str(glyphs[glyph::HORIZONTAL]);
}
} else if cur.intersects(LinkLine::VERT_PARENT) && !line.merge {
let left = cur.intersects(LinkLine::LEFT_MERGE | LinkLine::LEFT_FORK);
let right = cur.intersects(LinkLine::RIGHT_MERGE | LinkLine::RIGHT_FORK);
match (left, right) {
(true, true) => link_line.push_str(glyphs[glyph::JOIN_BOTH]),
(true, false) => link_line.push_str(glyphs[glyph::JOIN_LEFT]),
(false, true) => link_line.push_str(glyphs[glyph::JOIN_RIGHT]),
(false, false) => link_line.push_str(glyphs[glyph::PARENT]),
}
} else if cur.intersects(LinkLine::VERT_PARENT | LinkLine::VERT_ANCESTOR)
&& !cur.intersects(LinkLine::LEFT_FORK | LinkLine::RIGHT_FORK)
{
let left = cur.intersects(LinkLine::LEFT_MERGE);
let right = cur.intersects(LinkLine::RIGHT_MERGE);
match (left, right) {
(true, true) => link_line.push_str(glyphs[glyph::JOIN_BOTH]),
(true, false) => link_line.push_str(glyphs[glyph::JOIN_LEFT]),
(false, true) => link_line.push_str(glyphs[glyph::JOIN_RIGHT]),
(false, false) => {
if cur.intersects(LinkLine::VERT_ANCESTOR) {
link_line.push_str(glyphs[glyph::ANCESTOR]);
} else {
link_line.push_str(glyphs[glyph::PARENT]);
}
}
}
} else if cur.intersects(LinkLine::LEFT_FORK)
&& cur.intersects(LinkLine::LEFT_MERGE | LinkLine::CHILD)
{
link_line.push_str(glyphs[glyph::JOIN_LEFT]);
} else if cur.intersects(LinkLine::RIGHT_FORK)
&& cur.intersects(LinkLine::RIGHT_MERGE | LinkLine::CHILD)
{
link_line.push_str(glyphs[glyph::JOIN_RIGHT]);
} else if cur.intersects(LinkLine::LEFT_MERGE)
&& cur.intersects(LinkLine::RIGHT_MERGE)
{
link_line.push_str(glyphs[glyph::MERGE_BOTH]);
} else if cur.intersects(LinkLine::LEFT_FORK)
&& cur.intersects(LinkLine::RIGHT_FORK)
{
link_line.push_str(glyphs[glyph::FORK_BOTH]);
} else if cur.intersects(LinkLine::LEFT_FORK) {
link_line.push_str(glyphs[glyph::FORK_LEFT]);
} else if cur.intersects(LinkLine::LEFT_MERGE) {
link_line.push_str(glyphs[glyph::MERGE_LEFT]);
} else if cur.intersects(LinkLine::RIGHT_FORK) {
link_line.push_str(glyphs[glyph::FORK_RIGHT]);
} else if cur.intersects(LinkLine::RIGHT_MERGE) {
link_line.push_str(glyphs[glyph::MERGE_RIGHT]);
} else {
link_line.push_str(glyphs[glyph::SPACE]);
}
}
if let Some(msg) = message_lines.next() {
link_line.push(' ');
link_line.push_str(msg);
}
out.push_str(link_line.trim_end());
out.push('\n');
}
if let Some(term_row) = line.term_line {
let term_strs = [glyphs[glyph::PARENT], glyphs[glyph::TERMINATION]];
for term_str in term_strs.iter() {
let mut term_line = String::new();
for (i, term) in term_row.iter().enumerate() {
if *term {
term_line.push_str(term_str);
} else {
term_line.push_str(glyphs[line.pad_lines[i].to_glyph()]);
}
}
if let Some(msg) = message_lines.next() {
term_line.push(' ');
term_line.push_str(msg);
}
out.push_str(term_line.trim_end());
out.push('\n');
}
need_extra_pad_line = true;
}
let mut base_pad_line = String::new();
for entry in line.pad_lines.iter() {
base_pad_line.push_str(glyphs[entry.to_glyph()]);
}
for msg in message_lines {
let mut pad_line = base_pad_line.clone();
pad_line.push(' ');
pad_line.push_str(msg);
out.push_str(pad_line.trim_end());
out.push('\n');
need_extra_pad_line = false;
}
if need_extra_pad_line {
self.extra_pad_line = Some(base_pad_line);
}
out
}
}
#[cfg(test)]
mod tests {
use super::super::test_fixtures;
use super::super::test_fixtures::TestFixture;
use super::super::test_utils::render_string;
use super::super::test_utils::render_string_with_order;
use crate::GraphRowRenderer;
fn render(fixture: &TestFixture) -> String {
let mut renderer = GraphRowRenderer::new().output().build_box_drawing();
render_string(fixture, &mut renderer)
}
#[test]
fn basic() {
assert_eq!(
render(&test_fixtures::BASIC),
r#"
o C
โ
o B
โ
o A"#
);
}
#[test]
fn branches_and_merges() {
assert_eq!(
render(&test_fixtures::BRANCHES_AND_MERGES),
r#"
o W
โ
o V
โโโฎ
โ o U
โ โโโฎ
โ โ o T
โ โ โ
โ o โ S
โ โ
o โ R
โ โ
o โ Q
โโโฎ โ
โ o โ P
โ โโโโโฎ
โ โ โ o O
โ โ โ โ
โ โ โ o N
โ โ โ โโโฎ
โ o โ โ โ M
โ โ โ โ โ
โ o โ โ โ L
โ โ โ โ โ
o โ โ โ โ K
โโโโโโโโโฏ
o โ โ โ J
โ โ โ โ
o โ โ โ I
โโโฏ โ โ
o โ โ H
โ โ โ
o โ โ G
โโโโโโโฎ
โ โ o F
โ โโโฏ
โ o E
โ โ
o โ D
โ โ
o โ C
โโโโโฏ
o B
โ
o A"#
);
}
#[test]
fn octopus_branch_and_merge() {
assert_eq!(
render(&test_fixtures::OCTOPUS_BRANCH_AND_MERGE),
r#"
o J
โโโฌโโฎ
โ โ o I
โ โ โ
โ o โ H
โญโโผโโฌโโฌโโฎ
โ โ โ โ o G
โ โ โ โ โ
โ โ โ o โ E
โ โ โ โโโฏ
โ โ o โ D
โ โ โโโฎ
โ o โ โ C
โ โโโโโฏ
o โ โ F
โโโฏ โ
o โ B
โโโโโฏ
o A"#
);
}
#[test]
fn reserved_column() {
assert_eq!(
render(&test_fixtures::RESERVED_COLUMN),
r#"
o Z
โ
o Y
โ
o X
โญโโฏ
โ o W
โโโฏ
o G
โ
o F
โโโฎ
โ o E
โ โ
โ o D
โ
o C
โ
o B
โ
o A"#
);
}
#[test]
fn ancestors() {
assert_eq!(
render(&test_fixtures::ANCESTORS),
r#"
o Z
โ
o Y
โญโโฏ
o F
โท
โท o X
โญโโฏ
โ o W
โโโฏ
o E
โท
o D
โโโฎ
โ o C
โ โท
o โท B
โโโฏ
o A"#
);
}
#[test]
fn split_parents() {
assert_eq!(
render(&test_fixtures::SPLIT_PARENTS),
r#"
o E
โญโโฌโโฌโโค
โท o โ โท D
โญโโดโโฎ โท
โ o โท C
โ โโโฏ
o โ B
โโโโโฏ
o A"#
);
}
#[test]
fn terminations() {
assert_eq!(
render(&test_fixtures::TERMINATIONS),
r#"
o K
โ
โ o J
โโโฏ
o I
โญโโผโโฎ
โ โ โ
โ ~ โ
โ โ
o โ E
โ โ
โ o H
โโโโโฏ
o D
โ
~
o C
โ
o B
โ
~"#
);
}
#[test]
fn long_messages() {
assert_eq!(
render(&test_fixtures::LONG_MESSAGES),
r#"
o F
โโโฌโโฎ very long message 1
โ โ โ very long message 2
โ โ ~ very long message 3
โ โ
โ โ very long message 4
โ โ very long message 5
โ โ very long message 6
โ โ
โ o E
โ โ
โ o D
โ โ
o โ C
โโโฏ long message 1
โ long message 2
โ long message 3
โ
o B
โ
o A
โ long message 1
~ long message 2
long message 3"#
);
}
#[test]
fn different_orders() {
let order = |order: &str| {
let order = order.matches(|_: char| true).collect::<Vec<_>>();
let mut renderer = GraphRowRenderer::new().output().build_box_drawing();
render_string_with_order(&test_fixtures::ORDERS1, &mut renderer, Some(&order))
};
assert_eq!(
order("KJIHGFEDCBZA"),
r#"
o K
โโโฎ
โ o J
โ โโโฎ
โ โ o I
โ โ โโโฎ
โ โ โ o H
โ โ โ โโโฎ
โ โ โ โ o G
โ โ โ โ โโโฎ
o โ โ โ โ โ F
โ โ โ โ โ โ
โ o โ โ โ โ E
โโโฏ โ โ โ โ
โ o โ โ โ D
โโโโโฏ โ โ โ
โ o โ โ C
โโโโโโโฏ โ โ
โ o โ B
โโโโโโโโโฏ โ
โ o Z
โ
o A"#
);
assert_eq!(
order("KJIHGZBCDEFA"),
r#"
o K
โโโฎ
โ o J
โ โโโฎ
โ โ o I
โ โ โโโฎ
โ โ โ o H
โ โ โ โโโฎ
โ โ โ โ o G
โ โ โ โ โโโฎ
โ โ โ โ โ o Z
โ โ โ โ โ
โ โ โ โ o B
โ โ โ โ โ
โ โ โ o โ C
โ โ โ โโโฏ
โ โ o โ D
โ โ โโโฏ
โ o โ E
โ โโโฏ
o โ F
โโโฏ
o A"#
);
assert_eq!(
render(&test_fixtures::ORDERS2),
r#"
o K
โโโฎ
โ o J
โ โ
o โ F
โโโโโฎ
โ โ o I
โ โโโฏ
o โ E
โโโโโฎ
โ โ o H
โ โโโฏ
o โ D
โโโโโฎ
โ โ o G
โ โโโฏ
o โ C
โโโโโฎ
โ โ o Z
โ โ
o โ B
โโโฏ
o A"#
);
assert_eq!(
order("KFJEIDHCGZBA"),
r#"
o K
โโโฎ
o โ F
โ โ
โ o J
โ โโโฎ
โ o โ E
โโโฏ โ
โ o I
โ โญโโค
โ โ o D
โโโโโฏ
โ o H
โ โโโฎ
โ o โ C
โโโฏ โ
โ o G
โ โญโโค
โ o โ Z
โ โ
โ o B
โโโโโฏ
o A"#
);
}
}