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
8#[cfg(feature = "coloring")]
9use std::sync::Arc;
10
11#[cfg(feature = "coloring")]
12use crate::pipeline::coloring::LineIdAllocator;
13use crate::pipeline::coloring::PrefixLineColor;
14use crate::pipeline::graph_to_row_shape::GraphRowShaper;
15use crate::pipeline::prefix_lines_to_text::PrefixLinesToText;
16use crate::pipeline::row_shape_to_prefix_lines::box_drawing::BoxDrawingPrefixLineRenderer;
17use crate::pipeline::types::Ancestor;
18use crate::pipeline::types::GraphRowShapeOptions;
19use crate::pipeline::types::PrefixLine;
20use crate::pipeline::types::PrefixLineRenderer;
21use crate::pipeline::types::TextLine;
22
23/// A convenience renderer that runs all text rendering pipeline stages.
24///
25/// This keeps the individual pipeline stages available for callers that need
26/// to cache or replace one stage, while keeping the common streaming text path
27/// short.
28pub struct GraphTextRenderer<N> {
29    row_shaper: GraphRowShaper<N>,
30    // Use trait object instead of a type parameter to make runtime switch easier.
31    prefix_lines: Box<dyn PrefixLineRenderer<N> + Send + Sync + 'static>,
32    text: PrefixLinesToText,
33}
34
35impl<N> GraphTextRenderer<N>
36where
37    N: Clone + Eq,
38{
39    /// Create a text renderer with default options and prefix renderer.
40    pub fn new<P>() -> Self
41    where
42        P: PrefixLineRenderer<N> + Default + Send + Sync + 'static,
43    {
44        Self::with_prefix_lines(Box::new(P::default()))
45    }
46}
47
48impl<N> GraphTextRenderer<N>
49where
50    N: Clone + Eq,
51{
52    /// Create a text renderer with a custom prefix renderer.
53    pub fn with_prefix_lines(
54        prefix_lines: Box<dyn PrefixLineRenderer<N> + Send + Sync + 'static>,
55    ) -> Self {
56        Self {
57            row_shaper: GraphRowShaper::new(),
58            prefix_lines,
59            text: PrefixLinesToText::new(),
60        }
61    }
62
63    /// Configure line tracking before rendering the first row.
64    /// Prefix renderers attach the tracked IDs to graph fragments.
65    #[cfg(feature = "coloring")]
66    pub fn with_line_id_allocator(mut self, allocator: Option<Arc<LineIdAllocator<N>>>) -> Self {
67        self.row_shaper = self.row_shaper.with_line_id_allocator(allocator);
68        self
69    }
70
71    /// Configure options.
72    pub fn configure(mut self, func: impl FnOnce(&mut GraphRowShapeOptions)) -> Self {
73        func(self.row_shaper.options_mut());
74        self.text = self
75            .text
76            .with_text_gap(self.row_shaper.options().graph_row_shape_style.text_gap)
77            .with_link_line_placement(
78                self.row_shaper
79                    .options()
80                    .graph_row_shape_style
81                    .link_line_placement,
82            );
83        self
84    }
85
86    /// Reserve a column for a node before it is rendered.
87    pub fn reserve(&mut self, node: N) {
88        self.row_shaper.reserve(node);
89    }
90
91    /// Render the next node into text.
92    pub fn next_text(
93        &mut self,
94        node: N,
95        parents: Vec<Ancestor<N>>,
96        glyph: &str,
97        message: &str,
98    ) -> String {
99        let mut out = String::new();
100        self.write_next_text(&mut out, node, parents, glyph, message);
101        out
102    }
103
104    /// Write the next rendered node into `out`.
105    pub fn write_next_text(
106        &mut self,
107        out: &mut String,
108        node: N,
109        parents: Vec<Ancestor<N>>,
110        glyph: &str,
111        message: &str,
112    ) {
113        let row_shape = self.row_shaper.next_row_shape(node, parents);
114        let separator_line = row_shape.separator_line;
115        let prefix_lines = self.prefix_lines.next_prefix_lines(&row_shape);
116        self.text.write_next_text(
117            out,
118            prefix_lines,
119            separator_line,
120            glyph,
121            message,
122            self.row_shaper.options().min_row_height,
123        );
124    }
125
126    /// Render the next node into text fragments, retaining graph line IDs.
127    pub fn next_lines(
128        &mut self,
129        node: N,
130        parents: Vec<Ancestor<N>>,
131        glyph: &str,
132        message: &str,
133    ) -> Vec<TextLine> {
134        let row_shape = self.row_shaper.next_row_shape(node, parents);
135        let prefix_lines = self.prefix_lines.next_prefix_lines(&row_shape);
136        self.text.next_lines(
137            prefix_lines,
138            row_shape.separator_line,
139            glyph,
140            message,
141            self.row_shaper.options().min_row_height,
142        )
143    }
144
145    /// Stream borrowed text and IDs. A separate newline fragment ends each line.
146    pub fn write_next_lines(
147        &mut self,
148        out: impl FnMut(&str, &PrefixLineColor),
149        node: N,
150        parents: Vec<Ancestor<N>>,
151        glyph: &str,
152        message: &str,
153    ) {
154        let row_shape = self.row_shaper.next_row_shape(node, parents);
155        let prefix_lines = self.prefix_lines.next_prefix_lines(&row_shape);
156        self.text.write_next_lines(
157            out,
158            prefix_lines,
159            row_shape.separator_line,
160            glyph,
161            message,
162            self.row_shaper.options().min_row_height,
163        );
164    }
165
166    /// Calculate the next prefix lines.
167    pub fn next_prefix_lines(&mut self, node: N, parents: Vec<Ancestor<N>>) -> Vec<PrefixLine> {
168        let row_shape = self.row_shaper.next_row_shape(node, parents);
169        self.prefix_lines.next_prefix_lines(&row_shape)
170    }
171}
172
173impl<N> Default for GraphTextRenderer<N>
174where
175    N: Clone + Eq,
176{
177    fn default() -> Self {
178        Self::new::<BoxDrawingPrefixLineRenderer>()
179    }
180}