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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
use duat_core::{
    data::{Context, FileReader},
    palette::{self, Form},
    text::{text, Text},
    ui::{Area as UiArea, PushSpecs},
    widgets::{PassiveWidget, Widget, WidgetCfg},
};

use crate::{Area, Ui};

/// A vertical line on screen, useful, for example, for the separation
/// of a [`File`] and [`LineNumbers`].
///
/// [`File`]: duat_core::widgets::File
/// [`LineNumbers`]: duat_core::widgets::LineNumbers
pub struct VertRule {
    reader: Option<FileReader<Ui>>,
    text: Text,
    sep_char: SepChar,
}

impl VertRule {
    pub fn cfg() -> VertRuleCfg {
        VertRuleCfg::new()
    }
}

impl PassiveWidget<Ui> for VertRule {
    fn build(globals: Context<Ui>, on_file: bool) -> (Widget<Ui>, impl Fn() -> bool, PushSpecs) {
        VertRuleCfg::new().build(globals, on_file)
    }

    fn update(&mut self, area: &Area) {
        self.text = if let Some(reader) = self.reader.as_ref()
            && let SepChar::ThreeWay(..) | SepChar::TwoWay(..) = self.sep_char
        {
            reader.inspect(|file, _, input| {
                let main_line = input.cursors().unwrap().main().line();
                let lines = file.printed_lines();

                let upper = lines.iter().filter(|&(line, _)| *line < main_line).count();
                let middle = lines.iter().filter(|&(line, _)| *line == main_line).count();
                let lower = lines.iter().filter(|&(line, _)| *line > main_line).count();

                let chars = self.sep_char.chars();

                text!(
                    [UpperVertRule] { form_string(chars[0], upper) }
                    [VertRule] { form_string(chars[1], middle) }
                    [LowerVertRule] { form_string(chars[2], lower) }
                )
            })
        } else {
            let full_line = format!("{}\n", self.sep_char.chars()[1]).repeat(area.height());

            text!([VertRule] full_line)
        }
    }

    fn text(&self) -> &Text {
        &self.text
    }

    fn once(_globals: Context<Ui>) {
        palette::set_weak_form("VertRule", Form::new().dark_grey());
        palette::set_weak_ref("UpperVertRule", "VertRule");
        palette::set_weak_ref("LowerVertRule", "VertRule");
    }
}

/// The [`char`]s that should be printed above, equal to, and below
/// the main line.
#[derive(Clone)]
enum SepChar {
    Uniform(char),
    /// Order: main line, other lines.
    TwoWay(char, char),
    /// Order: main line, above main line, below main line.
    ThreeWay(char, char, char),
}

impl SepChar {
    /// The [`char`]s above, equal to, and below the main line,
    /// respectively.
    fn chars(&self) -> [char; 3] {
        match self {
            SepChar::Uniform(uniform) => [*uniform, *uniform, *uniform],
            SepChar::TwoWay(main, other) => [*other, *main, *other],
            SepChar::ThreeWay(main, upper, lower) => [*upper, *main, *lower],
        }
    }
}

/// The configurations for the [`VertRule`] widget.
#[derive(Clone)]
pub struct VertRuleCfg {
    sep_char: SepChar,
    specs: PushSpecs,
}

impl VertRuleCfg {
    /// Returns a new instance of [`VertRuleCfg`].
    pub fn new() -> Self {
        Self {
            sep_char: SepChar::Uniform('│'),
            specs: PushSpecs::left().with_lenght(1.0),
        }
    }

    pub fn on_the_right(self) -> Self {
        Self {
            specs: PushSpecs::right().with_lenght(1.0),
            ..self
        }
    }

    pub fn with_char(self, char: char) -> Self {
        Self {
            sep_char: SepChar::Uniform(char),
            ..self
        }
    }

    pub fn with_main_char(self, main: char) -> Self {
        Self {
            sep_char: match self.sep_char {
                SepChar::Uniform(other) => SepChar::TwoWay(main, other),
                SepChar::TwoWay(_, other) => SepChar::TwoWay(main, other),
                SepChar::ThreeWay(_, above, below) => SepChar::ThreeWay(main, above, below),
            },
            ..self
        }
    }

    pub fn with_char_above(self, above: char) -> Self {
        Self {
            sep_char: match self.sep_char {
                SepChar::Uniform(other) => SepChar::ThreeWay(other, above, other),
                SepChar::TwoWay(main, below) => SepChar::ThreeWay(main, above, below),
                SepChar::ThreeWay(main, _, below) => SepChar::ThreeWay(main, above, below),
            },
            ..self
        }
    }

    pub fn with_char_below(self, below: char) -> Self {
        Self {
            sep_char: match self.sep_char {
                SepChar::Uniform(other) => SepChar::ThreeWay(other, other, below),
                SepChar::TwoWay(main, above) => SepChar::ThreeWay(main, above, below),
                SepChar::ThreeWay(main, above, _) => SepChar::ThreeWay(main, above, below),
            },
            ..self
        }
    }
}

impl Default for VertRuleCfg {
    fn default() -> Self {
        Self::new()
    }
}

impl WidgetCfg<Ui> for VertRuleCfg {
    type Widget = VertRule;

    fn build(
        self,
        context: Context<Ui>,
        on_file: bool,
    ) -> (Widget<Ui>, impl Fn() -> bool + 'static, PushSpecs) {
        let reader = on_file.then_some(context.fixed_reader().unwrap());

        let vert_rule = VertRule {
            reader: reader.clone(),
            text: Text::default(),
            sep_char: self.sep_char,
        };

        let checker = if let Some(reader) = reader {
            Box::new(move || reader.has_changed()) as Box<dyn Fn() -> bool>
        } else {
            Box::new(move || false)
        };

        let widget = Widget::passive(vert_rule);
        (widget, checker, self.specs)
    }
}

fn form_string(char: char, count: usize) -> String {
    [char, '\n'].repeat(count).iter().collect()
}