makeup 0.0.8

Stylish CLIs/TUIs for Rust!
Documentation
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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
use std::fmt::Display;

use crate::components::EchoText;
use crate::DrawCommand;

use eyre::Result;

pub struct DrawCommandDiff {
    pub expected: Vec<DrawCommand>,
    pub actual: Vec<DrawCommand>,

    pub diff: Vec<DiffLine>,
}

impl DrawCommandDiff {
    pub fn new(expected: Vec<DrawCommand>, actual: Vec<DrawCommand>) -> Self {
        let mut diff = vec![];

        let mut expected_iter = expected.iter();
        let mut actual_iter = actual.iter();

        let mut line_number = 0;

        loop {
            let expected = expected_iter.next();
            let actual = actual_iter.next();

            if expected.is_none() && actual.is_none() {
                break;
            }

            let expected = expected.map(|c| Some(c.clone())).unwrap_or_default();
            let actual = actual.map(|c| Some(c.clone())).unwrap_or_default();

            diff.push(DiffLine {
                line_number,
                different: expected != actual,
                expected,
                actual,
            });

            line_number += 1;
        }

        Self {
            expected,
            actual,
            diff,
        }
    }

    pub fn is_empty(&self) -> bool {
        self.diff.is_empty()
    }

    pub async fn render(&self) -> Result<()> {
        let mut data = String::from("error rendering test ui!\n\n----------------\n\n");

        for line in &self.diff {
            let colour = if line.different {
                makeup_ansi::Ansi::Sgr(vec![makeup_ansi::SgrParameter::HexForegroundColour(
                    0xFF0000,
                )])
            } else {
                makeup_ansi::Ansi::Sgr(vec![makeup_ansi::SgrParameter::Reset])
            };

            data.push_str(&format!(
                "{colour}{line}{}",
                makeup_ansi::Ansi::Sgr(vec![makeup_ansi::SgrParameter::Reset])
            ));
        }

        let mut data = EchoText::<()>::new(data);

        let ui = {
            use crate::input::TerminalInput;
            use crate::render::TerminalRenderer;
            use crate::MUI;

            let renderer = TerminalRenderer::new();
            let input = TerminalInput::new().await?;

            MUI::new(&mut data, Box::new(renderer), input)?
        };
        ui.render_once().await?;

        Ok(())
    }

    pub async fn into_visual_diff(&self) -> Result<VisualDiff> {
        VisualDiff::new(self).await
    }
}

#[derive(Debug)]
pub struct DiffLine {
    pub line_number: usize,
    pub expected: Option<DrawCommand>,
    pub actual: Option<DrawCommand>,
    pub different: bool,
}

impl Display for DiffLine {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        writeln!(
            f,
            "line {}: expected: {:?}, actual: {:?}",
            self.line_number, self.expected, self.actual
        )
    }
}

pub struct VisualDiff {
    rendered_diff: String,
    is_different: bool,
}

impl VisualDiff {
    pub async fn new(diff: &DrawCommandDiff) -> Result<Self> {
        use crate::render::Renderer;

        async fn read_lines(renderer: &dyn Renderer) -> Vec<String> {
            let mut out = vec![];

            for i in 0..renderer.dimensions().1 {
                let line = renderer
                    .read_string(0, i, renderer.dimensions().0 - 1)
                    .await
                    .unwrap()
                    .trim_end()
                    .to_string();
                out.push(line);
            }

            while out.last().map(|s| s.is_empty()).unwrap_or(false) {
                out.pop();
            }

            out
        }

        let mut expected_renderer = crate::render::MemoryRenderer::new(128, 128);
        let mut actual_renderer = crate::render::MemoryRenderer::new(128, 128);

        expected_renderer
            .render(&[(0, diff.expected.clone())])
            .await?;
        actual_renderer.render(&[(0, diff.actual.clone())]).await?;

        let expected_lines = read_lines(&expected_renderer).await;
        let actual_lines = read_lines(&actual_renderer).await;

        let expected_text = expected_lines.join("\n");
        let actual_text = actual_lines.join("\n");

        let mut rendered_diff = String::from("");
        for i in 0..actual_lines.len() {
            use std::fmt::Write;

            if i >= expected_lines.len() {
                write!(
                    &mut rendered_diff,
                    "{}{}{}",
                    makeup_ansi::Ansi::Sgr(vec![makeup_ansi::SgrParameter::HexBackgroundColour(
                        0xFF0000
                    )]),
                    actual_lines[i],
                    makeup_ansi::Ansi::Sgr(vec![makeup_ansi::SgrParameter::Reset]),
                )?;
            } else {
                let mut expected_chars = expected_lines[i].chars();
                let mut actual_chars = actual_lines[i].chars();

                // for each character in the actual line, find each range of characters
                // that is different
                // store them in a Vec<(start, end)>
                let mut different_ranges = vec![];
                let mut start = 0;
                let mut end = 0;
                let mut different = false;
                loop {
                    let expected = expected_chars.next();
                    let actual = actual_chars.next();

                    if expected.is_none() && actual.is_none() {
                        break;
                    }

                    if expected != actual {
                        if !different {
                            start = end;
                            different = true;
                        }
                    } else if different {
                        different_ranges.push((start, end));
                        different = false;
                    }

                    end += 1;
                }

                if different {
                    different_ranges.push((start, end));
                }

                // for each range, mark red
                let actual_chars: Vec<char> = actual_lines[i].chars().collect();
                let mut last_position = 0;
                for range in different_ranges {
                    if range.0 >= actual_chars.len() {
                        // If the range exists outside of the actual line, then
                        // we need to render red past the end of the line but
                        // without any actual text
                        let padding = " ".repeat(range.1 - range.0);
                        let up_to_range: String =
                            actual_chars[0..actual_chars.len()].iter().collect();
                        last_position = actual_chars.len();
                        write!(
                            &mut rendered_diff,
                            "{reset}{up_to_range}{red}{padding}{reset}",
                            red = makeup_ansi::Ansi::Sgr(vec![
                                makeup_ansi::SgrParameter::HexBackgroundColour(0xFF0000)
                            ]),
                            reset = makeup_ansi::Ansi::Sgr(vec![makeup_ansi::SgrParameter::Reset]),
                        )?;
                    } else {
                        let up_to_range: String =
                            actual_chars[last_position..range.0].iter().collect();
                        last_position = std::cmp::min(range.1, actual_chars.len());

                        let padding = if last_position < range.1 {
                            " ".repeat(range.1 - last_position)
                        } else {
                            String::new()
                        };

                        let range: String = actual_chars[range.0..last_position].iter().collect();

                        write!(
                            &mut rendered_diff,
                            "{reset}{up_to_range}{red}{range}{padding}{reset}",
                            reset = makeup_ansi::Ansi::Sgr(vec![makeup_ansi::SgrParameter::Reset]),
                            red = makeup_ansi::Ansi::Sgr(vec![
                                makeup_ansi::SgrParameter::HexBackgroundColour(0xFF0000)
                            ]),
                        )?;
                    }
                }

                let up_to_range: String = actual_chars[last_position..].iter().collect();
                write!(
                    &mut rendered_diff,
                    "{}{}",
                    makeup_ansi::Ansi::Sgr(vec![makeup_ansi::SgrParameter::Reset]),
                    up_to_range,
                )?;
            }
            writeln!(
                &mut rendered_diff,
                "{}",
                makeup_ansi::Ansi::Sgr(vec![makeup_ansi::SgrParameter::Reset])
            )?;
        }

        let rendered_diff = rendered_diff.trim_end();

        let data = indoc::formatdoc!(
            "test ui did not match expected output!!!

            visual diff:

            ----------------

            expected:

            {expected_text}

            ----------------

            actual:

            {actual_text}

            ----------------

            diff:

            {rendered_diff}

            ----------------
            ",
        );

        Ok(Self {
            rendered_diff: data,
            is_different: expected_text != actual_text,
        })
    }

    pub async fn render(&self) -> Result<()> {
        if self.is_different {
            let mut data = EchoText::<()>::new(&self.rendered_diff);

            let ui = {
                use crate::input::TerminalInput;
                use crate::render::TerminalRenderer;
                use crate::MUI;

                let renderer = TerminalRenderer::new();
                let input = TerminalInput::new().await?;

                MUI::new(&mut data, Box::new(renderer), input)?
            };
            ui.render_once().await?;
        }

        Ok(())
    }

    pub fn is_different(&self) -> bool {
        self.is_different
    }
}

#[cfg(test)]
mod tests {
    use async_trait::async_trait;
    use eyre::Result;

    use crate::component::{DrawCommandBatch, Key, MakeupUpdate, RenderContext};
    use crate::test::{assert_renders_many, static_text};
    use crate::{Component, Dimensions, DrawCommand};

    #[derive(Debug)]
    struct LinesComponent {
        #[allow(dead_code)]
        state: (),
        key: Key,
    }

    #[async_trait]
    impl Component for LinesComponent {
        type Message = ();

        fn children(&self) -> Option<Vec<&Box<dyn Component<Message = Self::Message>>>> {
            None
        }

        fn children_mut(
            &mut self,
        ) -> Option<Vec<&mut Box<dyn Component<Message = Self::Message>>>> {
            None
        }

        async fn update(&mut self, _ctx: &mut MakeupUpdate<Self>) -> Result<()> {
            Ok(())
        }

        async fn render(&self, _ctx: &RenderContext) -> Result<DrawCommandBatch> {
            Ok((
                self.key,
                vec![
                    DrawCommand::TextUnderCursor("line 1    \n".into()),
                    DrawCommand::TextUnderCursor("lime 2\n".into()),
                    DrawCommand::TextUnderCursor("line 3\n".into()),
                    DrawCommand::TextUnderCursor("line 4\n".into()),
                    DrawCommand::TextUnderCursor("line 5\n".into()),
                ],
            ))
        }

        fn key(&self) -> Key {
            self.key
        }

        fn dimensions(&self) -> Result<Option<Dimensions>> {
            unimplemented!()
        }
    }

    #[tokio::test]
    #[should_panic]
    async fn test_diff_works() {
        async fn __do_test() -> Result<()> {
            let mut root = LinesComponent {
                state: (),
                key: crate::component::generate_key(),
            };

            assert_renders_many!(
                vec![
                    static_text!("line 1\n"),
                    static_text!("line 2\n"),
                    static_text!("line 3\n"),
                ],
                root
            );

            Ok(())
        }

        __do_test().await.unwrap();
    }
}