aboutsummaryrefslogtreecommitdiffstats
path: root/zellij-server/src/ui/components/ribbon.rs
blob: 91cc62e366960fee09ea1008ace42afc28ad619a (plain) (blame)
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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
use super::{text::stringify_text, Coordinates, Text};
use crate::panes::terminal_character::{AnsiCode, CharacterStyles, RESET_STYLES};
use zellij_utils::data::{PaletteColor, Style};

static ARROW_SEPARATOR: &str = "";

pub fn ribbon(
    content: Text,
    style: &Style,
    arrow_fonts: bool,
    component_coordinates: Option<Coordinates>,
) -> Vec<u8> {
    let colors = style.colors;
    let background = colors.text_unselected.background;
    if content.disabled {
        let declaration = colors.ribbon_unselected;
        let disabled_content = content.into_disabled();
        let (first_arrow_styles, text_style, last_arrow_styles) = (
            character_style(background, declaration.background),
            RESET_STYLES
                .foreground(Some(declaration.base.into()))
                .background(Some(declaration.background.into()))
                .italic(Some(AnsiCode::On)),
            character_style(declaration.background, background),
        );
        let (arrow, padding) = if arrow_fonts {
            (ARROW_SEPARATOR, Some(4))
        } else {
            ("", None)
        };
        let (text, _text_width) = stringify_text(
            &disabled_content,
            padding,
            &component_coordinates,
            &declaration,
            &colors,
            text_style,
        );
        let mut stringified = component_coordinates
            .map(|c| c.to_string())
            .unwrap_or_else(|| String::new());
        stringified.push_str(&format!(
            "{}{}{}{} {} {}{}{}",
            RESET_STYLES,
            first_arrow_styles,
            arrow,
            text_style,
            text,
            last_arrow_styles,
            arrow,
            RESET_STYLES
        ));
        return stringified.as_bytes().to_vec();
    }
    let declaration = if content.selected {
        colors.ribbon_selected
    } else {
        colors.ribbon_unselected
    };
    let (first_arrow_styles, text_style, last_arrow_styles) = (
        character_style(background, declaration.background),
        character_style(declaration.base, declaration.background),
        character_style(declaration.background, background),
    );

    let (arrow, padding) = if arrow_fonts {
        (ARROW_SEPARATOR, Some(4))
    } else {
        ("", None)
    };

    let (text, _text_width) = stringify_text(
        &content,
        padding,
        &component_coordinates,
        &declaration,
        &colors,
        text_style,
    );
    let mut stringified = component_coordinates
        .map(|c| c.to_string())
        .unwrap_or_else(|| String::new());
    stringified.push_str(&format!(
        "{}{}{}{} {} {}{}{}",
        RESET_STYLES,
        first_arrow_styles,
        arrow,
        text_style,
        text,
        last_arrow_styles,
        arrow,
        RESET_STYLES
    ));
    stringified.as_bytes().to_vec()
}

fn character_style(foreground: PaletteColor, background: PaletteColor) -> CharacterStyles {
    RESET_STYLES
        .foreground(Some(foreground.into()))
        .background(Some(background.into()))
        .bold(Some(AnsiCode::On))
}