Skip to main content

renderdag/
output.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 std::marker::PhantomData;
9
10use super::ascii::AsciiRenderer;
11use super::ascii_large::AsciiLargeRenderer;
12use super::box_drawing::BoxDrawingRenderer;
13use super::render::GraphRow;
14use super::render::Renderer;
15
16pub(crate) const DEFAULT_MIN_ROW_HEIGHT: usize = 2;
17
18#[derive(Clone, Copy, Debug)]
19pub struct OutputRendererOptions {
20    pub min_row_height: usize,
21    pub stagger_consecutive_disconnected_nodes: bool,
22    pub graph_row_shape_style: crate::pipeline::types::GraphRowShapeStyle,
23}
24
25/// Common stateful string line output utilities shared by ASCII and box-drawing
26/// renderers.
27#[derive(Default)]
28pub(crate) struct OutputRendererState {
29    queued_pad_line: Option<String>,
30    /// If > 1, no need for an extra separator for the next row.
31    /// Separator is only needed for adjacent single-line node lines.
32    /// If there are link, pad, term, or other kinds of node lines,
33    /// no need to draw separator.
34    row_height: usize,
35}
36
37impl OutputRendererState {
38    pub(crate) fn begin_row(&mut self, out: &mut String, separator_line: bool) {
39        self.flush_queued_pad_line(out);
40        if separator_line && self.row_height == 1 {
41            self.push_line(out, "");
42        }
43        self.row_height = 0;
44    }
45
46    pub(crate) fn push_line_with_message(
47        &mut self,
48        out: &mut String,
49        mut line: String,
50        message: Option<&str>,
51    ) {
52        if let Some(message) = message {
53            line.push(' ');
54            line.push_str(message);
55        }
56        self.push_line(out, &line);
57    }
58
59    pub(crate) fn push_pad_lines<'a>(
60        &mut self,
61        out: &mut String,
62        base_pad_line: &str,
63        message_lines: impl Iterator<Item = &'a str>,
64    ) -> bool {
65        let mut emitted = false;
66        for message in message_lines {
67            let mut pad_line = String::with_capacity(base_pad_line.len() + message.len() + 1);
68            pad_line.push_str(base_pad_line);
69            pad_line.push(' ');
70            pad_line.push_str(message);
71            self.push_line(out, &pad_line);
72            emitted = true;
73        }
74        emitted
75    }
76
77    pub(crate) fn queue_pad_line(&mut self, pad_line: String) {
78        self.queued_pad_line = Some(pad_line);
79    }
80
81    fn push_line(&mut self, out: &mut String, line: &str) {
82        out.push_str(line.trim_end());
83        out.push('\n');
84        self.row_height = self.row_height.saturating_add(1);
85    }
86
87    fn flush_queued_pad_line(&mut self, out: &mut String) {
88        if let Some(pad_line) = self.queued_pad_line.take() {
89            self.push_line(out, &pad_line);
90        }
91    }
92}
93
94pub struct OutputRendererBuilder<N, R>
95where
96    R: Renderer<N, Output = GraphRow<N>> + Sized,
97{
98    inner: R,
99    _phantom: PhantomData<N>,
100}
101
102impl Default for OutputRendererOptions {
103    fn default() -> Self {
104        Self {
105            min_row_height: DEFAULT_MIN_ROW_HEIGHT,
106            stagger_consecutive_disconnected_nodes: false,
107            graph_row_shape_style: Default::default(),
108        }
109    }
110}
111
112impl<N, R> OutputRendererBuilder<N, R>
113where
114    R: Renderer<N, Output = GraphRow<N>> + Sized,
115{
116    pub fn new(inner: R) -> Self {
117        OutputRendererBuilder {
118            inner,
119            _phantom: PhantomData,
120        }
121    }
122
123    pub fn with_options(mut self, options: OutputRendererOptions) -> Self {
124        *self.inner.output_options_mut() = options;
125        self
126    }
127
128    pub fn with_min_row_height(mut self, min_row_height: usize) -> Self {
129        self.inner.output_options_mut().min_row_height = min_row_height;
130        self
131    }
132
133    pub fn with_stagger_consecutive_disconnected_nodes(mut self, stagger: bool) -> Self {
134        self.inner
135            .output_options_mut()
136            .stagger_consecutive_disconnected_nodes = stagger;
137        self
138    }
139
140    pub fn build_ascii(self) -> AsciiRenderer<N, R> {
141        AsciiRenderer::new(self.inner)
142    }
143
144    pub fn build_ascii_large(self) -> AsciiLargeRenderer<N, R> {
145        AsciiLargeRenderer::new(self.inner)
146    }
147
148    pub fn build_box_drawing(self) -> BoxDrawingRenderer<N, R> {
149        BoxDrawingRenderer::new(self.inner)
150    }
151}