aboutsummaryrefslogtreecommitdiff
path: root/src/util.rs
blob: bac2a4ef4cc31a814366487bd54fdc37ccaa97f6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
//! Utilities related to the dialog widget display

use bevy_terminal_display::ratatui::style::{Color, Style, Stylize};
use yarnspinner::runtime::MarkupValue;
use zalgo::{Generator, GeneratorArgs, ZalgoSize};

/// Splits a marked-up yarnspinner line into sections of text and their corresponding ratatui styles
pub fn style_line(line: &yarnspinner::runtime::Line) -> Vec<(String, Style)> {
    if line.attributes.is_empty() {
        return vec![(line.text_without_character_name(), Style::new())];
    }

    let mut line_segments = Vec::<(String, Style)>::new();
    let mut attributes = line.attributes.clone();
    attributes.sort_by_key(|attribute| attribute.position);
    line_segments.push((
        line.text[..attributes[0].position].to_string(),
        Style::new(),
    ));
    for (i, attribute) in attributes.iter().enumerate() {
        let mut attrib_text = line.text_for_attribute(&attribute).to_string();
        let mut style = Style::new();
        match attribute.name.as_str() {
            "style" => {
                for (property_name, property_value) in attribute.properties.iter() {
                    match property_name.as_str() {
                        "bold" => {
                            if let MarkupValue::Bool(value) = property_value {
                                if *value {
                                    style = style.bold();
                                }
                            }
                        }
                        "italic" => {
                            if let MarkupValue::Bool(value) = property_value {
                                if *value {
                                    style = style.italic();
                                }
                            }
                        }
                        "color" => {
                            if let MarkupValue::Integer(value) = property_value {
                                style = style.fg(Color::Indexed(*value as u8))
                            }
                        }
                        "zalgo" => {
                            if let MarkupValue::Bool(value) = property_value {
                                if *value {
                                    let mut generator = Generator::new();
                                    let mut out = String::new();
                                    let args =
                                        GeneratorArgs::new(true, true, true, ZalgoSize::Mini);
                                    generator.gen(&attrib_text, &mut out, &args);
                                    attrib_text = out;
                                }
                            }
                        }
                        "bg" => {
                            if let MarkupValue::Integer(value) = property_value {
                                style = style.bg(Color::Indexed(*value as u8))
                            }
                        }
                        _ => (),
                    }
                }
            }
            _ => (),
        }
        if attribute.name != "character" {
            line_segments.push((attrib_text, style))
        }

        if let Some(next_attribute) = attributes.get(i + 1) {
            line_segments.push((
                line.text[attribute.position + attribute.length..next_attribute.position]
                    .to_string(),
                Style::new(),
            ));
        }
    }
    let last_attribute = attributes.last().unwrap();
    line_segments.push((
        line.text[last_attribute.position + last_attribute.length..].to_string(),
        Style::new(),
    ));

    line_segments
}