renderdag/pipeline/
graph_text_renderer.rs1use crate::pipeline::graph_to_row_shape::GraphRowShaper;
9use crate::pipeline::prefix_lines_to_text::PrefixLinesToText;
10use crate::pipeline::row_shape_to_prefix_lines::box_drawing::BoxDrawingPrefixLineRenderer;
11use crate::pipeline::types::Ancestor;
12use crate::pipeline::types::GraphRowShapeOptions;
13use crate::pipeline::types::PrefixLine;
14use crate::pipeline::types::PrefixLineRenderer;
15
16pub struct GraphTextRenderer<N> {
22 row_shaper: GraphRowShaper<N>,
23 prefix_lines: Box<dyn PrefixLineRenderer<N> + Send + Sync + 'static>,
25 text: PrefixLinesToText,
26}
27
28impl<N> GraphTextRenderer<N>
29where
30 N: Clone + Eq,
31{
32 pub fn new<P>() -> Self
34 where
35 P: PrefixLineRenderer<N> + Default + Send + Sync + 'static,
36 {
37 Self::with_prefix_lines(Box::new(P::default()))
38 }
39}
40
41impl<N> GraphTextRenderer<N>
42where
43 N: Clone + Eq,
44{
45 pub fn with_prefix_lines(
47 prefix_lines: Box<dyn PrefixLineRenderer<N> + Send + Sync + 'static>,
48 ) -> Self {
49 Self {
50 row_shaper: GraphRowShaper::new(),
51 prefix_lines,
52 text: PrefixLinesToText::new(),
53 }
54 }
55
56 pub fn configure(mut self, func: impl FnOnce(&mut GraphRowShapeOptions)) -> Self {
58 func(self.row_shaper.options_mut());
59 self.text = self.text.with_link_line_placement(
60 self.row_shaper
61 .options()
62 .graph_row_shape_style
63 .link_line_placement,
64 );
65 self
66 }
67
68 pub fn reserve(&mut self, node: N) {
70 self.row_shaper.reserve(node);
71 }
72
73 pub fn next_text(
75 &mut self,
76 node: N,
77 parents: Vec<Ancestor<N>>,
78 glyph: &str,
79 message: &str,
80 ) -> String {
81 let mut out = String::new();
82 self.write_next_text(&mut out, node, parents, glyph, message);
83 out
84 }
85
86 pub fn write_next_text(
88 &mut self,
89 out: &mut String,
90 node: N,
91 parents: Vec<Ancestor<N>>,
92 glyph: &str,
93 message: &str,
94 ) {
95 let row_shape = self.row_shaper.next_row_shape(node, parents);
96 let separator_line = row_shape.separator_line;
97 let prefix_lines = self.prefix_lines.next_prefix_lines(&row_shape);
98 self.text.write_next_text(
99 out,
100 prefix_lines,
101 separator_line,
102 glyph,
103 message,
104 self.row_shaper.options().min_row_height,
105 );
106 }
107
108 pub fn next_prefix_lines(&mut self, node: N, parents: Vec<Ancestor<N>>) -> Vec<PrefixLine> {
110 let row_shape = self.row_shaper.next_row_shape(node, parents);
111 self.prefix_lines.next_prefix_lines(&row_shape)
112 }
113}
114
115impl<N> Default for GraphTextRenderer<N>
116where
117 N: Clone + Eq,
118{
119 fn default() -> Self {
120 Self::new::<BoxDrawingPrefixLineRenderer>()
121 }
122}