Skip to main content

renderdag/pipeline/
prefix_lines_to_text.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::output::OutputRendererState;
9use crate::pad::pad_lines;
10use crate::pipeline::types::LinkLinePlacement;
11use crate::pipeline::types::PrefixLine;
12use crate::pipeline::types::PrefixLineKind;
13use crate::pipeline::types::PrefixLinePart;
14
15/// Stateful renderer for the final pipeline stage.
16///
17/// It fills node glyph slots, attaches message lines, and writes complete text
18/// lines for one rendered graph row at a time.
19#[derive(Default)]
20pub struct PrefixLinesToText {
21    state: OutputRendererState,
22    link_line_placement: LinkLinePlacement,
23}
24
25impl PrefixLinesToText {
26    /// Create a renderer with empty output state.
27    pub fn new() -> Self {
28        Self::default()
29    }
30
31    /// Choose where link-line padding is placed. Defaults to `Top`.
32    pub fn with_link_line_placement(mut self, placement: LinkLinePlacement) -> Self {
33        self.link_line_placement = placement;
34        self
35    }
36
37    /// Render the next row of prefix lines into a `String`.
38    pub fn next_text(
39        &mut self,
40        prefix_lines: Vec<PrefixLine>,
41        separator_line: bool,
42        glyph: &str,
43        message: &str,
44        min_row_height: usize,
45    ) -> String {
46        let mut out = String::new();
47        self.write_next_text(
48            &mut out,
49            prefix_lines,
50            separator_line,
51            glyph,
52            message,
53            min_row_height,
54        );
55        out
56    }
57
58    /// Write the next row of prefix lines into `out`.
59    pub fn write_next_text(
60        &mut self,
61        out: &mut String,
62        prefix_lines: Vec<PrefixLine>,
63        separator_line: bool,
64        glyph: &str,
65        message: &str,
66        min_row_height: usize,
67    ) {
68        let mut message_lines = pad_lines(message.lines(), min_row_height);
69        let extra_message_lines = if self.link_line_placement == LinkLinePlacement::Bottom {
70            let fixed_lines = prefix_lines
71                .iter()
72                .filter(|line| !line.kind.is_repeatable())
73                .count();
74            message
75                .lines()
76                .count()
77                .max(min_row_height)
78                .saturating_sub(fixed_lines)
79        } else {
80            0
81        };
82        let mut repeatable_line = None;
83        let mut has_term_row = false;
84
85        self.state.begin_row(out, separator_line);
86
87        for prefix_line in prefix_lines {
88            // Text starts at the node; extra message lines precede links only for Bottom.
89            if prefix_line.kind == PrefixLineKind::PreNode {
90                continue;
91            }
92            if prefix_line.kind == PrefixLineKind::PostNode {
93                if extra_message_lines > 0 {
94                    let base_line = render_prefix_line(&prefix_line, glyph);
95                    // Reserve message lines for the mandatory Link, Term, and Ancestry lines.
96                    self.state.push_pad_lines(
97                        out,
98                        &base_line,
99                        message_lines.by_ref().take(extra_message_lines),
100                    );
101                }
102                continue;
103            }
104            if prefix_line.kind.is_repeatable() {
105                repeatable_line = Some(prefix_line);
106                continue;
107            }
108            has_term_row |= prefix_line.kind == PrefixLineKind::Term;
109            let text = render_prefix_line(&prefix_line, glyph);
110            self.state
111                .push_line_with_message(out, text, message_lines.next());
112        }
113
114        if let Some(repeatable_line) = repeatable_line {
115            let base_line = render_prefix_line(&repeatable_line, glyph);
116            if !self.state.push_pad_lines(out, &base_line, message_lines) && has_term_row {
117                self.state.queue_pad_line(base_line);
118            }
119        }
120    }
121}
122
123fn render_prefix_line(prefix_line: &PrefixLine, glyph: &str) -> String {
124    let mut out = String::new();
125    for part in prefix_line.parts.iter() {
126        match part {
127            PrefixLinePart::Text(text) => out.push_str(text),
128            PrefixLinePart::NodeGlyph => out.push_str(glyph),
129        }
130    }
131    out
132}