duat-term 0.10.0

A frontend for Duat for the terminal
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
use duat_core::{
    opts::PrintOpts,
    text::{Text, TextPart, TextPlace, TwoPoints},
};
use unicode_width::UnicodeWidthChar;

/// A struct representing a printed [`TextPlace`] on the screen
///
/// This position differs from a [`VPoint`] in the sense that it
/// represents three properties of a printed character:
///
/// - The x position in which it was printed;
/// - The amount of horizontal space it occupies;
/// - Wether this character is the first on the line (i.e. it wraps)
///
/// [`VPoint`]: crate::mode::VPoint
/// [`TextPlace`]: crate::text::TextPlace
#[derive(Debug, Clone, Copy)]
pub struct PrintedPlace {
    /// The horizontal position in which a character was printed
    pub x: u32,
    /// The horizontal space it occupied
    pub len: u32,
    /// Wether it is the first character in the line
    pub wrap: bool,
}

/// An [`Iterator`] that returns both an [`TextPlace`] and a
/// [`Caret`].
///
/// This function expects that `cap` has been validated, and that the
/// iterator starts in the visual start of the line.
#[inline(never)]
pub fn print_iter<'t>(
    text: &'t Text,
    points: TwoPoints,
    width: u32,
    opts: PrintOpts,
) -> impl Iterator<Item = (PrintedPlace, TextPlace<'t>)> + 't {
    let start_points = text.visual_line_start(points, 0);
    let cap = opts.wrap_width(width).unwrap_or(width);
    let max_indent = if opts.indent_wraps { cap } else { 0 };

    // Line construction variables.
    let (mut x, mut spacers) = (0, 0);
    let (mut indent, mut on_indent, mut wrapped_indent) = (0, true, 0);

    if start_points != points {
        for item in text
            .iter_fwd(start_points)
            .take_while(|item| item.points() < points)
        {
            let old_indent = indent;
            let indent = (&mut indent, &mut on_indent);
            let len = process_part(item, x, opts, indent, &mut spacers);

            x += len;

            if x > cap && opts.wrap_lines {
                spacers = 0;

                if let TextPart::Char('\t') = item.part {
                    let desired = old_indent + x - cap;
                    wrapped_indent = if desired < max_indent {
                        desired
                    } else {
                        x - cap
                    };
                    x = wrapped_indent;
                } else {
                    wrapped_indent = old_indent * (old_indent < max_indent) as u32;
                    x = wrapped_indent + len;
                }
            }
        }

        if x == cap && opts.wrap_lines {
            x = indent;
        }
    }

    inner_iter(
        text.iter_fwd(points),
        (x, spacers),
        (indent, on_indent, wrapped_indent),
        (cap, opts),
    )
}

#[inline(never)]
pub fn rev_print_iter<'t>(
    text: &'t Text,
    points: TwoPoints,
    cap: u32,
    opts: PrintOpts,
) -> impl Iterator<Item = (PrintedPlace, TextPlace<'t>)> + 't {
    let mut iter = text.iter_rev(points);

    let mut returns = Vec::new();
    let mut items = Vec::new();

    let mut discard_first_empty_wrap = {
        let mut went_through_non_empty = false;
        move |(mut place, item): (PrintedPlace, _)| {
            went_through_non_empty |= place.len > 0;
            place.wrap &= went_through_non_empty;
            (place, item)
        }
    };

    std::iter::from_fn(move || {
        if let Some(next) = returns.pop().map(&mut discard_first_empty_wrap) {
            Some(next)
        } else {
            let iter = loop {
                if let Some(item) = iter.next() {
                    items.push(item);
                    if let TextPart::Char('\n') = item.part {
                        let len = items.len();
                        if len > 1 {
                            break items.drain(..len - 1).rev();
                        }
                    }
                } else {
                    break items.drain(..).rev();
                }
            };

            returns.extend(inner_iter(iter, (0, 0), (0, true, 0), (cap, opts)));

            returns.pop().map(&mut discard_first_empty_wrap)
        }
    })
}

#[inline(always)]
fn inner_iter<'t, 'i>(
    iter: impl Iterator<Item = TextPlace<'t>>,
    (mut x, mut spacers): (u32, usize),
    (mut indent, mut on_indent, mut wrapped_indent): (u32, bool, u32),
    (cap, opts): (u32, PrintOpts),
) -> impl Iterator<Item = (PrintedPlace, TextPlace<'t>)>
where
    't: 'i,
{
    let max_indent = if opts.indent_wraps { cap } else { 0 };

    // Line return variables.
    let (mut line, mut leftover_nl) = (Vec::<(u32, TextPlace)>::new(), None);
    let (mut printed_x, mut i) = (0, 0);
    let mut iter = iter.peekable();
    let mut initial_printed_x = x;

    std::iter::from_fn(move || {
        loop {
            // Emptying the line, most next calls should come here.
            if let Some(&(len, item)) = line.get(i) {
                let caret = PrintedPlace { x: printed_x, len, wrap: i == 0 };
                i += 1;
                printed_x += len;
                break Some((caret, item));
            }

            line.clear();
            i = 0;

            // Emptying a leftover '\n', which may come after the end of a line.
            if let Some((x, item)) = leftover_nl.take() {
                break Some((PrintedPlace { x, len: 1, wrap: true }, item));
            }

            // Preparing the line itself.
            loop {
                let Some(&item) = iter.peek() else {
                    if line.is_empty() {
                        return None;
                    } else {
                        printed_x = initial_printed_x.max(wrapped_indent);
                        space_line(spacers, &mut line, cap, x);
                        break;
                    }
                };

                let mut next = || _ = unsafe { iter.next().unwrap_unchecked() };

                let old_indent = indent;
                let indent = (&mut indent, &mut on_indent);
                let len = process_part(item, x, opts, indent, &mut spacers);

                let must_wrap = (x >= cap || x + len > cap) && opts.wrap_lines;
                x += len;

                if item.part == TextPart::Char('\n') || must_wrap {
                    printed_x = initial_printed_x.max(wrapped_indent);
                    space_line(spacers, &mut line, cap, x);
                    spacers = 0;

                    let leftover = x.saturating_sub(cap);
                    wrapped_indent = match item.part {
                        TextPart::Char('\t') if leftover > 0 => {
                            line.push((len - leftover, item));
                            next();
                            if old_indent + leftover < max_indent && opts.indent_wraps {
                                old_indent + leftover
                            } else {
                                leftover
                            }
                        }
                        TextPart::Char('\n') => {
                            if len > 0 && must_wrap {
                                let position = old_indent
                                    * (old_indent < max_indent && opts.indent_wraps) as u32;
                                leftover_nl = Some((position, item))
                            } else {
                                line.push((len, item));
                            }
                            next();
                            0
                        }
                        _ => {
                            // At this point, the only logical course of action is to wrap on
                            // every character.
                            if cap == 0 && !line.iter().any(|(_, item)| item.part.is_char()) {
                                line.push((len, item));
                                next();
                            }

                            old_indent * (old_indent < max_indent && opts.indent_wraps) as u32
                        }
                    };

                    x = wrapped_indent;
                    break;
                } else {
                    line.push((len, item));
                    next();
                }
            }

            initial_printed_x = 0;
        }
    })
}

/// Wether the [`TwoPoints`] actually does start a wrapped line.
pub fn is_starting_points(text: &Text, points: TwoPoints, width: u32, opts: PrintOpts) -> bool {
    let start_points = text.visual_line_start(points, 0);
    let max_indent = if opts.indent_wraps { width } else { 0 };
    let cap = opts.wrap_width(width).unwrap_or(width);

    // Line construction variables.
    let (mut x, mut spacers) = (0, 0);
    let (mut indent, mut on_indent) = (0, true);

    if start_points == points {
        true
    } else if !opts.wrap_lines {
        false
    } else {
        let mut wrapped = true;

        for item in text
            .iter_fwd(start_points)
            .take_while(|item| item.points() <= points)
        {
            wrapped = false;

            let old_indent = indent;
            let indent = (&mut indent, &mut on_indent);
            let len = process_part(item, x, opts, indent, &mut spacers);

            x += len;
            if x > cap && opts.wrap_lines {
                wrapped = true;

                x = if let TextPart::Char('\t') = item.part {
                    let desired = old_indent + x - cap;
                    if desired < max_indent {
                        desired
                    } else {
                        x - cap
                    }
                } else {
                    old_indent * (old_indent < max_indent) as u32 + len
                };
            }
        }

        wrapped
    }
}

/// Returns an [`Iterator`] over the sequences of [`WordChars`].
#[inline(always)]
fn _words<'t, 'i>(
    iter: impl Iterator<Item = TextPlace<'t>> + Clone + 'i,
    (mut total_len, mut spacers): (u32, usize),
    (mut indent, mut on_indent, mut wrapped_indent): (u32, bool, u32),
    (cap, opts): (u32, PrintOpts),
) -> impl Iterator<Item = (PrintedPlace, TextPlace<'t>)> + Clone {
    let max_indent = if opts.indent_wraps { cap } else { 0 };

    // Line return variables.
    let (mut line, mut leftover_nl): (Vec<(u32, TextPlace)>, _) = (Vec::new(), None);
    let (mut x, mut i, mut first_char_was_printed) = (0, 0, false);

    // Line construction variables
    let mut iter = iter.peekable();
    let mut word = Vec::new();
    let (mut new_x, mut first_x) = (0, total_len * (total_len < cap) as u32);

    std::iter::from_fn(move || {
        loop {
            if let Some(&(len, item)) = line.get(i) {
                let wrap = !first_char_was_printed && item.part.is_char();
                if wrap {
                    x = new_x;
                }
                let caret = PrintedPlace { x, len, wrap };
                i += 1;
                x += len;
                first_char_was_printed |= item.part.is_char();
                break Some((caret, item));
            }

            line.clear();
            i = 0;
            first_char_was_printed = false;

            if let Some((x, item)) = leftover_nl.take() {
                break Some((PrintedPlace { x, len: 1, wrap: true }, item));
            }

            loop {
                let Some(&item) = iter.peek() else {
                    if line.is_empty() {
                        return None;
                    } else {
                        break;
                    }
                };

                let old_indent = indent;
                let indent = (&mut indent, &mut on_indent);
                let len = process_part(item, x, opts, indent, &mut spacers);

                total_len += len;

                if let TextPart::Char(char) = item.part
                    && ((total_len >= cap && opts.wrap_lines) || char == '\n')
                {
                    new_x = first_x + wrapped_indent;
                    space_line(spacers, &mut line, cap, total_len);
                    spacers = 0;

                    let leftover = total_len.saturating_sub(cap);
                    wrapped_indent = match char {
                        '\t' if leftover > 0 => {
                            line.push((len - leftover, item));
                            iter.next();
                            if old_indent + leftover < max_indent && opts.indent_wraps {
                                old_indent + leftover
                            } else {
                                leftover
                            }
                        }
                        '\n' => {
                            match (opts.print_new_line, total_len > cap) {
                                (true, true) => {
                                    let position = old_indent
                                        * (old_indent < max_indent && opts.indent_wraps) as u32;
                                    leftover_nl = Some((position, iter.next().unwrap()))
                                }
                                (true, false) => line.push((1, iter.next().unwrap())),
                                (false, _) => line.push((0, iter.next().unwrap())),
                            }
                            0
                        }
                        _ => old_indent * (old_indent < max_indent && opts.indent_wraps) as u32,
                    };

                    total_len = wrapped_indent;
                    break;
                } else {
                    word.push((len, item));
                }

                iter.next();
            }

            first_x = 0;
        }
    })
}

#[inline(always)]
pub fn len_from(char: char, start: u32, opts: PrintOpts) -> u32 {
    match char {
        '\t' => (opts.tabstop_spaces_at(start)).max(1),
        '\n' => 0,
        '\0'..='\u{1f}' => 2,
        '\u{80}'..='\u{9f}' => 4,
        char => UnicodeWidthChar::width(char).unwrap_or(0) as u32,
    }
}

fn process_part(
    item: TextPlace,
    x: u32,
    opts: PrintOpts,
    indent: (&mut u32, &mut bool),
    spacers: &mut usize,
) -> u32 {
    match item.part {
        TextPart::Char(char) => process_char(indent, x, char, opts),
        TextPart::Spacer => {
            *spacers += 1;
            0
        }
        _ => 0,
    }
}

#[inline(always)]
fn process_char(
    (indent, on_indent): (&mut u32, &mut bool),
    x: u32,
    char: char,
    opts: PrintOpts,
) -> u32 {
    match char {
        '\n' => {
            *on_indent = true;
            *indent = 0;
            opts.print_new_line as u32
        }
        char => {
            let len = len_from(char, x, opts);
            *on_indent &= char == ' ' || char == '\t';
            *indent += len * *on_indent as u32;
            len
        }
    }
}

/// Adds spacing to a line, and returns the initial amount of
/// space needed
pub fn space_line(spacers: usize, line: &mut [(u32, TextPlace)], cap: u32, total_len: u32) {
    if spacers == 0 {
        return;
    }

    let space = cap.saturating_sub(total_len) as usize;
    let mut enough_space = space;
    while !enough_space.is_multiple_of(spacers) {
        enough_space += 1;
    }
    let diff = enough_space - space;

    // Do it in reverse, so skipped Spacers won't change.
    for (i, (len, _)) in line
        .iter_mut()
        .rev()
        .filter(|(_, item)| matches!(item.part, TextPart::Spacer))
        .enumerate()
    {
        *len = ((enough_space / spacers) - (i < diff) as usize) as u32;
    }
}