Skip to main content

renderdag/pipeline/
graph_text_renderer.rs

1/*
2 * Copyright (c) Meta Platforms, Inc. and affiliates.
3 *
4 * This source code is licensed under the MIT license found in the
5 * LICENSE file in the root directory of this source tree.
6 */
7
8use 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
16/// A convenience renderer that runs all text rendering pipeline stages.
17///
18/// This keeps the individual pipeline stages available for callers that need
19/// to cache or replace one stage, while keeping the common streaming text path
20/// short.
21pub struct GraphTextRenderer<N> {
22    row_shaper: GraphRowShaper<N>,
23    // Use trait object instead of a type parameter to make runtime switch easier.
24    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    /// Create a text renderer with default options and prefix renderer.
33    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    /// Create a text renderer with a custom prefix renderer.
46    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    /// Configure options.
57    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    /// Reserve a column for a node before it is rendered.
69    pub fn reserve(&mut self, node: N) {
70        self.row_shaper.reserve(node);
71    }
72
73    /// Render the next node into text.
74    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    /// Write the next rendered node into `out`.
87    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    /// Calculate the next prefix lines.
109    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}