anathema-widgets 0.2.11

Anathema widget base
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
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
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
use std::ops::{AddAssign, Deref};

use anathema_geometry::Size;
use anathema_store::tree::ValueId;
use anathema_value_resolver::ValueKind;
use unicode_width::{UnicodeWidthChar, UnicodeWidthStr};

use crate::WidgetId;

/// Word wrapping strategy
#[derive(Debug, Copy, Clone, PartialEq, Eq, Default)]
pub enum Wrap {
    /// Normal word wrapping. This will break text on hyphen and whitespace.
    /// Trailing whitespace is consumed if it would cause a line break.
    #[default]
    Normal,
    /// Insert a newline in the middle of any text
    WordBreak,
}

impl Wrap {
    /// Returns true if word wrapping is enabled (self == Self::Normal)
    pub fn is_word_wrap(&self) -> bool {
        matches!(self, Self::Normal)
    }
}

impl TryFrom<&ValueKind<'_>> for Wrap {
    type Error = ();

    fn try_from(value: &ValueKind<'_>) -> Result<Self, Self::Error> {
        let s = value.as_str().ok_or(())?;
        match s {
            "normal" => Ok(Wrap::Normal),
            "break" => Ok(Wrap::WordBreak),
            _ => Err(()),
        }
    }
}

impl From<Wrap> for ValueKind<'_> {
    fn from(value: Wrap) -> Self {
        let value = match value {
            Wrap::Normal => "normal",
            Wrap::WordBreak => "break",
        };

        ValueKind::Str(value.into())
    }
}

#[derive(Debug)]
pub(crate) struct LineWidth(usize);

impl LineWidth {
    pub(crate) const ZERO: Self = Self(0);

    // update the current value and return the old value
    pub(crate) fn swap(&mut self, mut new_value: usize) -> u16 {
        std::mem::swap(&mut self.0, &mut new_value);
        new_value as u16
    }
}

impl Default for LineWidth {
    fn default() -> Self {
        LineWidth::ZERO
    }
}

impl Deref for LineWidth {
    type Target = usize;

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

impl AddAssign<usize> for LineWidth {
    fn add_assign(&mut self, rhs: usize) {
        self.0 += rhs;
    }
}

/// The process result dictates whether it's possible to
/// fit more text or not.
#[derive(Debug, Copy, Clone, PartialEq)]
pub enum ProcessResult {
    /// Continue means it's possible to process more text
    Continue,
    /// Break means that there is no more room for text and
    /// further processing should be avoided
    Break,
}

#[derive(Debug, Copy, Clone)]
pub(crate) enum Entry {
    Newline,
    LineWidth(u16),
    Style(ValueId),
}

/// Represents a line containing the width and the segments
#[derive(Debug)]
pub struct Line<I> {
    pub width: u16,
    pub entries: I,
}

/// A line segment.
#[derive(Debug)]
pub enum Segment<'a> {
    /// Set the style
    SetStyle(ValueId),
    /// String slice
    Str(&'a str),
}

#[derive(Debug, Copy, Clone, PartialEq)]
pub(crate) enum LineEntry {
    Width(u16),
    Str(u32, u32),
    SetStyle(ValueId),
    Newline,
}

/// Perform text layout
/// ```
/// # use anathema_widgets::layout::text::*;
/// # use anathema_geometry::Size;
///
/// let mut text = Strings::new(Size::new(10, 10), Wrap::Normal);
/// text.add_str("he");
/// text.add_str("ll");
/// text.add_str("o world");
/// let size = text.finish();
///
/// let lines = text.lines();
/// ```
#[derive(Debug)]
pub struct Strings {
    layout: Vec<(u32, Entry)>,
    lines: Vec<LineEntry>,
    chomper: Chomper,
    frozen: bool,
    bytes: Vec<u8>,
    max: Size,
    size: Size,
    wrap: Wrap,
    // Byte index where the current line starts
    line: usize,
    current_width: LineWidth,
}

impl Strings {
    pub fn new(max: Size, wrap: Wrap) -> Self {
        Self {
            layout: vec![],
            lines: vec![],
            chomper: Chomper::Continuous(0),
            frozen: false,
            bytes: vec![],
            max,
            wrap,
            size: Size::new(0, 1),
            line: 0,
            current_width: LineWidth::ZERO,
        }
    }

    /// Layout another string slice.
    pub fn add_str(&mut self, s: &str) -> ProcessResult {
        if self.max.height == 0 || self.max.width == 0 {
            return ProcessResult::Break;
        }

        if self.frozen {
            return ProcessResult::Break;
        }

        for word in s.split_inclusive(char::is_whitespace) {
            self.bytes.extend(word.bytes());
            for c in word.chars() {
                if let res @ ProcessResult::Break = self.chomp(c) {
                    self.bytes.truncate(self.chomper.index());
                    self.freeze();
                    return res;
                }
            }
        }

        ProcessResult::Continue
    }

    /// Access the laid out strings.
    /// See [`Strings`] for example.
    pub fn lines(&self) -> impl Iterator<Item = Line<impl Iterator<Item = Segment<'_>>>> {
        let lines = self.lines.split(|e| *e == LineEntry::Newline);

        lines.map(|entries| {
            let LineEntry::Width(width) = entries[0] else { unreachable!() };

            Line {
                width,
                entries: entries[1..].iter().map(|e| match e {
                    LineEntry::Str(from, to) => Segment::Str(
                        std::str::from_utf8(&self.bytes[*from as usize..*to as usize])
                            .expect("only strings written to the byte store"),
                    ),
                    LineEntry::SetStyle(style) => Segment::SetStyle(*style),
                    LineEntry::Width(_) | LineEntry::Newline => unreachable!("consumed already"),
                }),
            }
        })
    }

    pub fn set_style(&mut self, style: WidgetId) {
        let index = self.bytes.len();
        self.layout.push((index as u32, Entry::Style(style)));
    }

    /// Finalize the layout, converting entries to lines
    pub fn finish(&mut self) -> Size {
        self.frozen = true;
        self.layout.sort_by(|a, b| a.0.cmp(&b.0));

        let last_line = self.line(self.bytes.len());
        let last_line_width = last_line.width();
        self.layout
            .push((self.bytes.len() as u32, Entry::LineWidth(last_line_width as u16)));

        // Write the entries as lines
        let mut from = 0;
        for line in self.layout.split(|e| matches!(e.1, Entry::Newline)) {
            // Find the line width (always the last entry)
            let width = match line.last() {
                Some((_, Entry::LineWidth(w))) => *w,
                _ => unreachable!("the last entry is always the line width"),
            };

            self.lines.push(LineEntry::Width(width));

            for (i, entry) in line {
                // Don't bother adding a string entry for an empty string
                if from != *i {
                    self.lines.push(LineEntry::Str(from, *i));
                }

                from = *i;

                match entry {
                    Entry::Style(style) => self.lines.push(LineEntry::SetStyle(*style)),
                    Entry::LineWidth(_) => {}
                    Entry::Newline => unreachable!("consumed by the split"),
                }
            }
            self.lines.push(LineEntry::Newline);
        }

        self.lines.pop();

        self.update_width();
        if self.size.width == 0 {
            self.size = Size::ZERO;
        }
        self.size
    }

    fn freeze(&mut self) {
        self.frozen = true;
    }

    fn newline(&mut self) {
        self.size.height += 1;
        self.update_width();
        self.line = match self.chomper {
            Chomper::Continuous(idx) => {
                self.layout
                    .push((idx as u32, Entry::LineWidth(self.current_width.swap(0))));
                self.layout.push((idx as u32, Entry::Newline));
                idx
            }
            Chomper::WordBoundary {
                word_boundary,
                current_index,
            } => {
                let diff = self.line(current_index).width() - self.line(word_boundary).width();
                let width = *self.current_width - diff;
                self.layout.push((word_boundary as u32, Entry::LineWidth(width as u16)));
                self.layout.push((word_boundary as u32, Entry::Newline));
                let _ = self.current_width.swap(diff);
                self.chomper = Chomper::Continuous(current_index);
                word_boundary
            }
        };
    }

    fn line(&self, index: usize) -> &str {
        self.str(self.line, index)
    }

    fn str(&self, offset: usize, index: usize) -> &str {
        std::str::from_utf8(&self.bytes[offset..index]).expect("only valid strings here")
    }

    fn update_width(&mut self) {
        self.size.width = self.size.width.max(*self.current_width as u16);
    }

    fn chomp(&mut self, c: char) -> ProcessResult {
        let width = c.width().unwrap_or(0) as u16;

        // NOTE
        // Special case: the character is too wide to ever fit so it's removed,
        // e.g a character width of two with a max width of one.
        if width > self.max.width {
            for _ in 0..c.len_utf8() {
                self.bytes.pop();
            }
            return ProcessResult::Continue;
        }

        // NOTE
        // If newline characters are handled then pop the bytes and insert a newline
        if c == '\n' {
            self.bytes.pop();

            if self.size.height >= self.max.height {
                return ProcessResult::Break;
            }

            self.chomper.force_word_boundary();
            self.newline();
            return ProcessResult::Continue;
        }

        // NOTE
        // If the trailing whitespace should be removed, do so here
        while width + *self.current_width as u16 > self.max.width {
            if c.is_whitespace() {
                // 1. Make this the next word boundary
                // 2. Insert a newline here
                // 3. Remove the bytes representing this whitespace

                for _ in 0..c.len_utf8() {
                    self.bytes.pop();
                }

                self.chomper.force_word_boundary();
                self.newline();

                return ProcessResult::Continue;
            }

            if self.size.height >= self.max.height {
                return ProcessResult::Break;
            }

            self.newline();
        }

        self.chomper.chomp(c, self.wrap);
        self.current_width += width as usize;

        ProcessResult::Continue
    }
}

impl Default for Strings {
    fn default() -> Self {
        Self::new(Size::ZERO, Wrap::default())
    }
}

// TODO: move this into string2
#[derive(Debug)]
pub(crate) enum Chomper {
    Continuous(usize),
    WordBoundary { word_boundary: usize, current_index: usize },
}

impl Chomper {
    pub(crate) fn index(&self) -> usize {
        match self {
            Chomper::Continuous(current_index) | Chomper::WordBoundary { current_index, .. } => *current_index,
        }
    }

    pub(crate) fn force_word_boundary(&mut self) {
        if let Chomper::WordBoundary {
            word_boundary,
            current_index,
        } = self
        {
            *word_boundary = *current_index;
        }
    }

    pub(crate) fn chomp(&mut self, c: char, wrap: Wrap) {
        let c_len = c.len_utf8();

        if c.is_whitespace() && wrap.is_word_wrap() {
            match self {
                Chomper::Continuous(idx) | Chomper::WordBoundary { current_index: idx, .. } => {
                    let new_index = *idx + c_len;
                    *self = Self::WordBoundary {
                        word_boundary: new_index,
                        current_index: new_index,
                    };
                    return;
                }
            }
        }

        match self {
            Self::Continuous(idx) => *idx += c_len,
            Self::WordBoundary { current_index, .. } => *current_index += c_len,
        }
    }
}

impl Default for Chomper {
    fn default() -> Self {
        Chomper::Continuous(0)
    }
}

#[cfg(test)]
mod test {
    use super::*;

    fn test_layout(max: Size, input: &[&str], expected: &str, wrap: Wrap) {
        let mut strings = Strings::new(max, wrap);

        for i in input {
            if let ProcessResult::Break = strings.add_str(i) {
                break;
            }
        }

        let _size = strings.finish();

        let lines = strings.lines();

        let mut output = String::new();
        for line in lines {
            for e in line.entries {
                match e {
                    Segment::SetStyle(_) => todo!(),
                    Segment::Str(s) => output.push_str(s),
                }
            }
            output.push('\n');
        }
        output.pop();

        assert_eq!(&output, expected);
    }

    #[test]
    fn word_wrapping_layout() {
        let inputs: &[(&[&str], &str)] = &[
            (&["a\nb\nc"], "a\nb\nc"),
            (&[" 12", "345 12", "345 "], " \n12345\n12345\n"),
            (&[" 12", "345 12", "345 "], " \n12345\n12345\n"),
            (&[" 🐇🐇🐇", "🐇🐇 12", "345 "], " \n🐇🐇\n🐇🐇\n🐇 \n12345\n"),
            (&["1", "23", "45 12", "345 "], "12345\n12345\n"),
            (&["12345 abcde "], "12345\nabcde\n"),
            (&["onereallylongword"], "onere\nallyl\nongwo\nrd"),
            (&["ahello do the"], "ahell\no do \nthe"),
            (&["hello do the"], "hello\ndo \nthe"),
        ];

        for (input, expected) in inputs {
            test_layout(Size::new(5, 10), input, expected, Wrap::Normal);
        }
    }

    #[test]
    fn outliers() {
        let inputs: &[(&[&str], &str)] = &[
            (&["🐇"], ""),
            (&["\n"], "\n"),
            (&["\n\n\n"], "\n\n\n"),
            (&["abc"], "a\nb\nc"),
        ];

        for (input, expected) in inputs {
            test_layout(Size::new(1, 10), input, expected, Wrap::Normal);
        }
    }

    #[test]
    fn layout_size() {
        let inputs: &[(&[&str], &str)] = &[(&["123456789"], "123\n456")];

        for (input, expected) in inputs {
            test_layout(Size::new(3, 2), input, expected, Wrap::Normal);
        }
    }

    #[test]
    fn word_breaking_layout() {
        let inputs: &[(&[&str], &str)] = &[(&["123 4567"], "123 4\n567")];

        for (input, expected) in inputs {
            test_layout(Size::new(5, 3), input, expected, Wrap::WordBreak);
        }
    }

    #[test]
    fn freeze_layout() {
        let mut strings = Strings::new(Size::new(100, 10), Wrap::Normal);

        assert_eq!(strings.add_str("abc"), ProcessResult::Continue);
        strings.freeze();
        assert_eq!(strings.add_str("abc"), ProcessResult::Break);
    }

    #[test]
    fn limited_space() {
        test_layout(Size::new(58, 0), &["meh"], "", Wrap::Normal);
    }

    #[test]
    fn wrap_from_attribute() {
        let mut attributes = anathema_value_resolver::Attributes::empty();

        attributes.set("break", Wrap::WordBreak);
        let word_break = attributes.get_as::<Wrap>("break").unwrap();
        assert_eq!(word_break, Wrap::WordBreak);

        attributes.set("normal", Wrap::Normal);
        let word_break = attributes.get_as::<Wrap>("normal").unwrap();
        assert_eq!(word_break, Wrap::Normal);
    }
}