r3bl_tui 0.3.6

TUI library inspired by Redux, Redux, Flexbox, CSS, editor components, etc
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
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
/*
 *   Copyright (c) 2022 R3BL LLC
 *   All rights reserved.
 *
 *   Licensed under the Apache License, Version 2.0 (the "License");
 *   you may not use this file except in compliance with the License.
 *   You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 *   Unless required by applicable law or agreed to in writing, software
 *   distributed under the License is distributed on an "AS IS" BASIS,
 *   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *   See the License for the specific language governing permissions and
 *   limitations under the License.
 */

use std::{
    fmt::{self, Debug},
    ops::{Deref, DerefMut},
};

use async_trait::async_trait;
use get_size::GetSize;
use r3bl_rs_utils_core::*;
use serde::{Deserialize, Serialize};

use crate::*;

/// Represents a grid of cells where the row/column index maps to the terminal screen. This works
/// regardless of the size of each cell. Cells can contain emoji who's display width is greater than
/// one. This complicates things since a "😃" takes up 2 display widths.
///
/// Let's say one cell has a "😃" in it. The cell's display width is 2. The cell's byte size is 4.
/// The next cell after it will have to contain nothing or void.
///
/// Why? This is because the col & row indices of the grid map to display col & row indices of the
/// terminal screen. By inserting a [PixelChar::Void] pixel char in the next cell, we signal the
/// rendering logic to skip it since it has already been painted. And this is different than a
/// [PixelChar::Spacer] which has to be painted!
#[derive(Clone, Serialize, Deserialize, PartialEq, Eq, Hash, GetSize)]
pub struct OffscreenBuffer {
    pub buffer: PixelCharLines,
    pub window_size: Size,
    pub my_pos: Position,
    pub my_fg_color: Option<TuiColor>,
    pub my_bg_color: Option<TuiColor>,
}

pub enum OffscreenBufferDiffResult {
    NotComparable,
    Comparable(PixelCharDiffChunks),
}

pub type PixelCharDiffChunks = List<DiffChunk>;
pub type DiffChunk = (Position, PixelChar);

mod offscreen_buffer_impl {
    use super::*;

    impl PixelCharDiffChunks {
        pub fn pretty_print(&self) -> String {
            let mut it = String::new();
            for (pos, pixel_char) in self.iter() {
                it.push_str(&format!("\t{:?}: {}\n", pos, pixel_char.pretty_print()));
            }
            it
        }
    }

    impl Deref for OffscreenBuffer {
        type Target = PixelCharLines;

        fn deref(&self) -> &Self::Target {
            &self.buffer
        }
    }

    impl DerefMut for OffscreenBuffer {
        fn deref_mut(&mut self) -> &mut Self::Target {
            &mut self.buffer
        }
    }

    impl Debug for OffscreenBuffer {
        fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
            write!(
                f,
                "window_size: {:?}, \n{}\n",
                self.window_size,
                self.pretty_print()
            )
        }
    }

    impl OffscreenBuffer {
        /// Checks for differences between self and other. Returns a list of positions and pixel
        /// chars if there are differences (from other).
        pub fn diff(&self, other: &Self) -> OffscreenBufferDiffResult {
            if self.window_size != other.window_size {
                return OffscreenBufferDiffResult::NotComparable;
            }

            let mut it = List::default();
            for (row, (self_row, other_row)) in
                self.buffer.iter().zip(other.buffer.iter()).enumerate()
            {
                for (col, (self_pixel_char, other_pixel_char)) in
                    self_row.iter().zip(other_row.iter()).enumerate()
                {
                    if self_pixel_char != other_pixel_char {
                        it.push((
                            position!(col_index: col, row_index: row),
                            other_pixel_char.clone(),
                        ));
                    }
                }
            }
            OffscreenBufferDiffResult::Comparable(it)
        }

        /// Create a new buffer and fill it with empty chars.
        pub fn new_with_capacity_initialized(window_size: Size) -> Self {
            Self {
                buffer: PixelCharLines::new_with_capacity_initialized(window_size),
                window_size,
                my_pos: Default::default(),
                my_fg_color: None,
                my_bg_color: None,
            }
        }

        // Make sure each line is full of empty chars.
        pub fn clear(&mut self) {
            self.buffer = PixelCharLines::new_with_capacity_initialized(self.window_size);
        }

        pub fn pretty_print(&self) -> String {
            let mut lines = vec![];
            for row_index in 0..ch!(@to_usize self.window_size.row_count) {
                if let Some(row) = self.buffer.get(row_index) {
                    lines.push({
                        let row_index_text = format!("row_index: {row_index}");
                        let row_index_text = style_error(&row_index_text).to_string();
                        let row_text =
                            format!("{}\n{}", row_index_text, row.pretty_print());
                        row_text
                    });
                }
            }
            lines.join("\n")
        }
    }
}

#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq, Hash, GetSize)]
pub struct PixelCharLines {
    pub lines: Vec<PixelCharLine>,
}

mod pixel_char_lines_impl {
    use super::*;

    impl Deref for PixelCharLines {
        type Target = Vec<PixelCharLine>;
        fn deref(&self) -> &Self::Target {
            &self.lines
        }
    }

    impl DerefMut for PixelCharLines {
        fn deref_mut(&mut self) -> &mut Self::Target {
            &mut self.lines
        }
    }

    impl PixelCharLines {
        pub fn new_with_capacity_initialized(window_size: Size) -> Self {
            let window_height = ch!(@to_usize window_size.row_count);
            let window_width = ch!(@to_usize window_size.col_count);
            Self {
                lines: vec![
                    PixelCharLine::new_with_capacity_initialized(window_width);
                    window_height
                ],
            }
        }
    }
}

#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq, Hash, GetSize)]
pub struct PixelCharLine {
    pub pixel_chars: Vec<PixelChar>,
}

mod pixel_char_line_impl {
    use super::*;

    // This represents a single row on the screen (i.e. a line of text).
    impl PixelCharLine {
        pub fn pretty_print(&self) -> String {
            let mut it = vec![];
            let mut void_indices: Vec<usize> = vec![];
            let mut spacer_indices: Vec<usize> = vec![];
            let mut void_count: Vec<String> = vec![];
            let mut spacer_count: Vec<String> = vec![];

            // Pretty print only so many chars per line (depending on the terminal width in which
            // log.fish is run).
            const MAX_PIXEL_CHARS_PER_LINE: usize = 6;
            let mut char_count = 0;

            // Loop: for each PixelChar in a line (pixel_chars_lines[row_index]).
            for (col_index, pixel_char) in self.iter().enumerate() {
                match pixel_char {
                    PixelChar::Void => {
                        void_count.push(col_index.to_string());
                        void_indices.push(col_index);
                    }
                    PixelChar::Spacer => {
                        spacer_count.push(col_index.to_string());
                        spacer_indices.push(col_index);
                    }
                    _ => {}
                }

                let index_txt = format!("{col_index:03}");
                let pixel_char_txt = pixel_char.pretty_print();
                let index_msg =
                    format!("{}{}", style_dim_underline(&index_txt), pixel_char_txt);
                it.push(index_msg);

                // Add \n every MAX_CHARS_PER_LINE characters.
                char_count += 1;
                if char_count >= MAX_PIXEL_CHARS_PER_LINE {
                    char_count = 0;
                    it.push("\n".to_string());
                }
            }

            // Pretty print the spacers & voids (of any of either or both).
            {
                let mut void_spacer_output = vec![];

                if !void_count.is_empty() {
                    void_spacer_output.push(format!(
                        "void [ {} ]",
                        PixelCharLine::pretty_print_index_values(&void_indices)
                    ));
                }

                if !spacer_count.is_empty() {
                    match void_spacer_output.is_empty() {
                        true => {
                            void_spacer_output.push(format!(
                                "spacer [ {} ]",
                                PixelCharLine::pretty_print_index_values(&spacer_indices)
                            ));
                        }
                        false => {
                            void_spacer_output.push(format!(
                                ", spacer [ {} ]",
                                PixelCharLine::pretty_print_index_values(&spacer_indices)
                            ));
                        }
                    }
                }

                it.push(void_spacer_output.join(" | "));
            }

            it.join("")
        }

        pub fn pretty_print_index_values(values: &[usize]) -> String {
            // Track state thru loop iteration.
            let mut current_range: Vec<usize> = vec![];
            let mut it: Vec<String> = vec![];

            mod helpers {
                pub enum Peek {
                    NextItemContinuesRange,
                    NextItemDoesNotContinueRange,
                }

                pub fn peek_does_next_item_continues_range(
                    values: &[usize],
                    index: usize,
                ) -> Peek {
                    if values.get(index + 1).is_none() {
                        return Peek::NextItemDoesNotContinueRange;
                    }
                    if values[index + 1] == values[index] + 1 {
                        Peek::NextItemContinuesRange
                    } else {
                        Peek::NextItemDoesNotContinueRange
                    }
                }

                pub enum CurrentRange {
                    DoesNotExist,
                    Exists,
                }

                pub fn does_current_range_exist(
                    current_range: &Vec<usize>,
                ) -> CurrentRange {
                    match current_range.is_empty() {
                        true => CurrentRange::DoesNotExist,
                        false => CurrentRange::Exists,
                    }
                }
            }

            // Main loop.
            pub use helpers::*;
            for (i, value) in values.iter().enumerate() {
                match (
                    peek_does_next_item_continues_range(values, i),
                    does_current_range_exist(&current_range),
                ) {
                    (Peek::NextItemContinuesRange, CurrentRange::DoesNotExist) => {
                        current_range.push(*value); // Start new current range.
                    }
                    (Peek::NextItemDoesNotContinueRange, CurrentRange::DoesNotExist) => {
                        it.push(format!("{value}"));
                    }
                    // The next value continues the current range.
                    (Peek::NextItemContinuesRange, CurrentRange::Exists) => {
                        current_range.push(*value);
                    }
                    // The next value does not continue the current range.
                    (Peek::NextItemDoesNotContinueRange, CurrentRange::Exists) => {
                        current_range.push(*value);
                        it.push(format!(
                            "{}-{}",
                            current_range[0],
                            current_range[current_range.len() - 1]
                        ));
                        current_range.clear();
                    }
                }
            }

            it.join(", ")
        }

        /// Create a new row with the given width and fill it with the empty chars.
        pub fn new_with_capacity_initialized(window_width: usize) -> Self {
            Self {
                pixel_chars: vec![PixelChar::Spacer; window_width],
            }
        }
    }
    impl Deref for PixelCharLine {
        type Target = Vec<PixelChar>;
        fn deref(&self) -> &Self::Target {
            &self.pixel_chars
        }
    }

    impl DerefMut for PixelCharLine {
        fn deref_mut(&mut self) -> &mut Self::Target {
            &mut self.pixel_chars
        }
    }
}

#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq, Hash, GetSize)]
pub enum PixelChar {
    Void,
    Spacer,
    PlainText {
        content: GraphemeClusterSegment,
        maybe_style: Option<Style>,
    },
}

const EMPTY_CHAR: char = '';
const VOID_CHAR: char = '';

mod pixel_char_impl {
    use super::*;

    impl Default for PixelChar {
        fn default() -> Self {
            Self::Spacer
        }
    }

    impl PixelChar {
        pub fn pretty_print(&self) -> String {
            fn truncate(s: &str, max_chars: usize) -> &str {
                match s.char_indices().nth(max_chars) {
                    None => s,
                    Some((idx, _)) => &s[..idx],
                }
            }

            let width = 16;

            let it = match self {
                PixelChar::Void => {
                    format!(" V {VOID_CHAR:░^width$}")
                }
                PixelChar::Spacer => {
                    format!(" S {EMPTY_CHAR:░^width$}")
                }
                PixelChar::PlainText {
                    content: character,
                    maybe_style,
                } => {
                    let output = match maybe_style {
                        // Content + style.
                        Some(style) => {
                            format!("'{}'→{}", character.string, style.pretty_print())
                        }
                        // Content, no style.
                        _ => format!("'{}'", character.string),
                    };
                    let trunc_output = truncate(&output, width);
                    format!(" {} {trunc_output: ^width$}", style_primary("P"))
                }
            };

            it
        }
    }
}

#[async_trait]
pub trait OffscreenBufferPaint {
    async fn render(&mut self, offscreen_buffer: &OffscreenBuffer) -> RenderOps;

    async fn render_diff(&mut self, diff_chunks: &PixelCharDiffChunks) -> RenderOps;

    async fn paint(
        &mut self,
        render_ops: RenderOps,
        flush_kind: FlushKind,
        shared_global_data: &SharedGlobalData,
    );

    async fn paint_diff(
        &mut self,
        render_ops: RenderOps,
        shared_global_data: &SharedGlobalData,
    );
}

#[cfg(test)]
mod tests {
    use r3bl_rs_utils_macro::style;

    use super::*;

    #[test]
    fn test_offscreen_buffer_construction() {
        let window_size = size! { col_count: 10, row_count: 2};
        let my_offscreen_buffer =
            OffscreenBuffer::new_with_capacity_initialized(window_size);
        assert_eq2!(my_offscreen_buffer.buffer.len(), 2);
        assert_eq2!(my_offscreen_buffer.buffer[0].len(), 10);
        assert_eq2!(my_offscreen_buffer.buffer[1].len(), 10);
        for line in my_offscreen_buffer.buffer.iter() {
            for pixel_char in line.iter() {
                assert_eq2!(pixel_char, &PixelChar::Spacer);
            }
        }
        // println!("my_offscreen_buffer: \n{:#?}", my_offscreen_buffer);
    }

    #[test]
    fn test_offscreen_buffer_re_init() {
        let window_size = size! { col_count: 10, row_count: 2};
        let mut my_offscreen_buffer =
            OffscreenBuffer::new_with_capacity_initialized(window_size);
        my_offscreen_buffer.buffer[0][0] = PixelChar::PlainText {
            content: GraphemeClusterSegment::from("a"),
            maybe_style: Some(style! {color_bg: color!(@green) }),
        };
        my_offscreen_buffer.buffer[1][9] = PixelChar::PlainText {
            content: GraphemeClusterSegment::from("z"),
            maybe_style: Some(style! {color_bg: color!(@red) }),
        };
        // println!("my_offscreen_buffer: \n{:#?}", my_offscreen_buffer);
        my_offscreen_buffer.clear();
        for line in my_offscreen_buffer.buffer.iter() {
            for pixel_char in line.iter() {
                assert_eq2!(pixel_char, &PixelChar::Spacer);
            }
        }
        // println!("my_offscreen_buffer: \n{:#?}", my_offscreen_buffer);
    }
}