urushi 0.1.1

Composable styling, components, and terminal rendering for Rust CLIs
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
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
//! The height phase: how many rows each node occupies, once every width is
//! settled.
//!
//! Height is the result of wrapping, so it is defined only after a width
//! exists. That is why it is a phase of its own rather than a second half of
//! the [`width`](super::width) one, and why it may look back at the widths that
//! phase decided but never revise them.
//!
//! It runs in two steps, and the split is what keeps a line from being fitted
//! twice:
//!
//! - [`fit`] fits every text leaf to the width it was given, exactly once.
//!   Fitting depends on the width alone, so no height bound can change its
//!   result.
//! - [`heights`] hands height bounds down and counts rows. A `Column` whose
//!   area cannot hold its children asks each for its demand and then settles a
//!   short child at its assignment — the second visit recomputes numbers over
//!   the fitted lines and never fits them again.

use crate::text::{
    PrintableLines, PrintableText, StyledTextGrapheme, wrap_styled_lines, wrap_text,
};
use crate::{
    Align, BlockStyle, BlockTitle, Canvas, Key, Overflow, Sides, StyledText, TextStyle,
    VerticalAlign, Viewport,
};

use super::geometry::Constraint;
use super::sizing::{
    Claim, Kind, border_extent, degrade, distribute_constraints, height_axis, text_lines, vertical,
};
use super::width::{WidthNode, Widths};

/// One node with its width settled and its text already fitted to it.
#[derive(Debug)]
pub(super) struct Fitted<'a> {
    pub width: usize,
    height_kind: Kind,
    height_floor: usize,
    height_projection: bool,
    node: FittedNode<'a>,
}

#[derive(Debug)]
enum FittedNode<'a> {
    Text {
        lines: Vec<Vec<StyledTextGrapheme<'a>>>,
        align: Align,
        fill: TextStyle,
    },
    Block {
        style: &'a BlockStyle,
        title: Option<&'a BlockTitle>,
        anchor: Option<Key>,
        padding: Sides,
        margin: Sides,
        content_width: usize,
        child: Box<Fitted<'a>>,
    },
    Row(VerticalAlign, Vec<Fitted<'a>>),
    Column(Align, Vec<Fitted<'a>>),
    Grid {
        columns: Vec<usize>,
        rows: Vec<Vec<FittedCell<'a>>>,
    },
    Canvas(&'a Canvas, bool, super::canvas::CanvasRequirements),
    Viewport(&'a Viewport, bool, Box<Fitted<'a>>),
}

/// One cell of a grid, with its text already fitted to its column's width.
#[derive(Debug)]
pub(super) struct FittedCell<'a> {
    padding: Sides,
    align: Align,
    vertical_align: VerticalAlign,
    child: Fitted<'a>,
}

/// Fits every text leaf to its settled width.
///
/// This is the only place a line is ever wrapped or cut, and it happens once
/// per leaf: what a leaf produces depends on its width and its policy, both of
/// which the width phase already fixed.
pub(super) fn fit(widths: Widths<'_>) -> Fitted<'_> {
    let Widths {
        width,
        height_kind,
        height_floor,
        height_projection,
        node,
    } = widths;
    let node = match node {
        WidthNode::Text(text) => {
            let lines = fit_styled_text_lines(text.text, text.area, text.overflow);
            FittedNode::Text {
                lines,
                align: text.align,
                fill: text.fill,
            }
        }
        WidthNode::Block(block) => FittedNode::Block {
            style: block.style,
            title: block.title,
            anchor: block.anchor,
            padding: block.padding,
            margin: block.margin,
            content_width: block.content_width,
            child: Box::new(fit(*block.child)),
        },
        WidthNode::Row(align, children) => {
            FittedNode::Row(align, children.into_iter().map(fit).collect())
        }
        WidthNode::Column(align, children) => {
            FittedNode::Column(align, children.into_iter().map(fit).collect())
        }
        WidthNode::Grid(box_) => FittedNode::Grid {
            columns: box_.columns,
            rows: box_
                .rows
                .into_iter()
                .map(|row| {
                    row.into_iter()
                        .map(|cell| FittedCell {
                            padding: cell.padding,
                            align: cell.align,
                            vertical_align: cell.vertical_align,
                            child: fit(cell.child),
                        })
                        .collect()
                })
                .collect(),
        },
        WidthNode::Canvas(canvas, width_bounded, height) => {
            FittedNode::Canvas(canvas, width_bounded, height)
        }
        WidthNode::Viewport(viewport, width_bounded, child) => {
            FittedNode::Viewport(viewport, width_bounded, Box::new(fit(*child)))
        }
    };
    Fitted {
        width,
        height_kind,
        height_floor,
        height_projection,
        node,
    }
}

/// Fits plain text to a settled width under one overflow policy.
pub(crate) fn fit_text_lines(text: &str, width: Option<usize>, overflow: &Overflow) -> Vec<String> {
    let mut lines = match (width, overflow) {
        (Some(width), Overflow::Wrap) => wrap_text(PrintableLines::new(text), width),
        (Some(width), Overflow::Clip(marker)) => text_lines(text)
            .iter()
            .map(|line| clip_line(line, width, marker))
            .collect(),
        (None, _) => text.lines().map(str::to_owned).collect(),
    };
    if lines.is_empty() {
        lines.push(String::new());
    }
    lines
}

fn fit_styled_text_lines<'a>(
    text: &'a StyledText,
    width: Option<usize>,
    overflow: &'a Overflow,
) -> Vec<Vec<StyledTextGrapheme<'a>>> {
    let lines = text.lines();
    match (width, overflow) {
        (Some(width), Overflow::Wrap) => wrap_styled_lines(lines, width),
        (Some(width), Overflow::Clip(marker)) => lines
            .into_iter()
            .map(|line| clip_styled_line(line, width, marker))
            .collect(),
        (None, _) => lines,
    }
}

fn clip_styled_line<'a>(
    line: Vec<StyledTextGrapheme<'a>>,
    width: usize,
    marker: &'a str,
) -> Vec<StyledTextGrapheme<'a>> {
    if line.iter().map(|grapheme| grapheme.width()).sum::<usize>() <= width {
        return line;
    }
    if width == 0 {
        return Vec::new();
    }
    let marker = PrintableText::new(marker);
    let marker_width = marker.width();
    let marker = (marker_width < width).then_some(marker);
    let budget = width - marker.map_or(0, PrintableText::width);

    let mut output = Vec::new();
    let mut consumed = 0;
    let mut omitted_style = None;
    for grapheme in line {
        if consumed + grapheme.width() > budget {
            omitted_style = Some(grapheme.style);
            break;
        }
        consumed += grapheme.width();
        output.push(grapheme);
    }
    if let (Some(marker), Some(style)) = (marker, omitted_style) {
        output.extend(
            marker
                .graphemes()
                .map(|grapheme| StyledTextGrapheme { grapheme, style }),
        );
    }
    output
}

/// Cuts one line to `width` cells between graphemes, ending it with `marker`.
///
/// The marker occupies cells of its own, so the text keeps the width less the
/// marker's own. A marker the box cannot hold is dropped: a silent cut is
/// closer to the request than a box filled with the marker alone.
fn clip_line(line: &PrintableText, width: usize, marker: &str) -> String {
    if line.width() <= width {
        return line.as_str().to_owned();
    }
    if width == 0 {
        return String::new();
    }
    let marker = PrintableText::new(marker);
    let marker = if marker.width() < width {
        marker
    } else {
        PrintableText::new("")
    };
    let budget = width - marker.width();

    let mut output = line.truncate(budget).as_str().to_owned();
    output.push_str(marker.as_str());
    output
}

/// One node with both of its sizes settled: the rectangle, before it exists.
#[derive(Debug)]
pub(super) struct Sized<'f> {
    pub width: usize,
    pub height: usize,
    pub node: SizedNode<'f>,
}

#[derive(Debug)]
pub(super) enum SizedNode<'f> {
    Text {
        lines: &'f [Vec<StyledTextGrapheme<'f>>],
        align: Align,
        fill: &'f TextStyle,
    },
    Block {
        style: &'f BlockStyle,
        title: Option<&'f BlockTitle>,
        anchor: Option<Key>,
        padding: Sides,
        margin: Sides,
        content_width: usize,
        /// The rows the box keeps, decided here so assembly only applies it.
        content_height: usize,
        child: Box<Sized<'f>>,
    },
    Row(VerticalAlign, Vec<Sized<'f>>),
    Column(Align, Vec<Sized<'f>>),
    Grid {
        /// One width per column, padding included.
        columns: &'f [usize],
        /// One height per row, the padding of its cells included.
        heights: Vec<usize>,
        rows: Vec<Vec<SizedCell<'f>>>,
    },
    Canvas {
        canvas: &'f Canvas,
        width_bounded: bool,
        height_bounded: bool,
    },
    Viewport {
        viewport: &'f Viewport,
        width_bounded: bool,
        height_bounded: bool,
        child: Box<Sized<'f>>,
    },
}

/// One cell of a grid, with both of its sizes settled.
#[derive(Debug)]
pub(super) struct SizedCell<'f> {
    /// Both axes of the cell's padding, degraded to what its column and its
    /// row could hold.
    pub padding: Sides,
    /// How the cell places itself in a column or a row wider than it is.
    pub align: Align,
    pub vertical_align: VerticalAlign,
    pub child: Sized<'f>,
}

/// Counts the rows every node occupies under `area`.
pub(super) fn heights<'f>(fitted: &'f Fitted<'_>, constraint: Constraint) -> Sized<'f> {
    match &fitted.node {
        FittedNode::Text { lines, align, fill } => Sized {
            width: fitted.width,
            // A text leaf has no height of its own to bound: it produced its
            // rows when it was fitted, and clipping them is a box's rule.
            height: lines.len(),
            node: SizedNode::Text {
                lines,
                align: *align,
                fill,
            },
        },
        FittedNode::Block {
            style,
            title,
            anchor,
            padding,
            margin,
            content_width,
            child,
        } => {
            let border = border_extent(style);
            let (mut padding, mut margin) = (*padding, *margin);

            // 1. Degrade the frame to the area — the vertical half of it; the
            //    width phase decided the other half.
            let down = degrade(
                constraint.cap,
                vertical(margin),
                border.height(),
                vertical(padding),
                0,
            );
            if down.margin {
                margin.top = 0;
                margin.bottom = 0;
            }
            if down.padding {
                padding.top = 0;
                padding.bottom = 0;
            }

            let axis = height_axis(
                style,
                border.height() + usize::from(padding.top) + usize::from(padding.bottom),
            );
            let box_constraint = Constraint {
                reference: constraint
                    .reference
                    .map(|area| area.saturating_sub(vertical(margin))),
                cap: constraint
                    .cap
                    .map(|area| area.saturating_sub(vertical(margin))),
            };

            // 2. The content, under what the box leaves it, and then the used
            //    height by the same clamp the width took — now that the rows
            //    are known, because the content is what decided how many.
            let local_bound =
                matches!(axis.length, Some(crate::Length::Cells(_))) || axis.max.is_some();
            let mut child_constraint = axis.content_constraint(box_constraint);
            if !local_bound && constraint.reference.is_none() {
                child_constraint.reference = None;
            }
            if !local_bound && constraint.cap.is_none() {
                child_constraint.cap = None;
            }
            let child = heights(child, child_constraint);
            let used = axis.used(box_constraint, child.height, 0);

            Sized {
                width: fitted.width,
                height: used + vertical(margin),
                node: SizedNode::Block {
                    style,
                    title: *title,
                    anchor: *anchor,
                    padding,
                    margin,
                    content_width: *content_width,
                    content_height: used - axis.frame,
                    child: Box::new(child),
                },
            }
        }
        FittedNode::Row(align, children) => {
            // The cross axis has nothing to divide: every child receives the
            // row's own height, and a `Fill` there stretches to it.
            let children: Vec<Sized<'f>> = children
                .iter()
                .map(|child| heights(child, constraint))
                .collect();
            Sized {
                width: fitted.width,
                height: children.iter().map(|child| child.height).max().unwrap_or(0),
                node: SizedNode::Row(*align, children),
            }
        }
        FittedNode::Column(align, children) => {
            let children = divide(children, constraint);
            Sized {
                width: fitted.width,
                height: children.iter().map(|child| child.height).sum(),
                node: SizedNode::Column(*align, children),
            }
        }
        FittedNode::Grid { columns, rows } => {
            let (heights, rows) = rows_of(rows, constraint);
            Sized {
                width: fitted.width,
                height: heights.iter().sum(),
                node: SizedNode::Grid {
                    columns,
                    heights,
                    rows,
                },
            }
        }
        FittedNode::Canvas(canvas, width_bounded, height) => Sized {
            width: fitted.width,
            height: constraint
                .reference
                .map(|area| area.max(height.floor()))
                .unwrap_or(height.demand()),
            node: SizedNode::Canvas {
                canvas,
                width_bounded: *width_bounded,
                height_bounded: constraint.reference.is_some(),
            },
        },
        FittedNode::Viewport(viewport, width_bounded, child) => {
            let vertical = viewport.vertical_projection().is_some();
            let extent = constraint.reference;
            let child_constraint = if vertical {
                Constraint {
                    reference: extent,
                    cap: None,
                }
            } else {
                constraint
            };
            let child = heights(child, child_constraint);
            Sized {
                width: fitted.width,
                height: if vertical {
                    extent.unwrap_or(child.height)
                } else {
                    child.height
                },
                node: SizedNode::Viewport {
                    viewport,
                    width_bounded: *width_bounded,
                    height_bounded: extent.is_some(),
                    child: Box::new(child),
                },
            }
        }
    }
}

/// Divides a grid's height among its rows and settles every cell in one.
///
/// A row demands the greatest height among its cells, as a `Row` does, and the
/// rows then divide the grid's height by the rule a `Column` applies to its
/// children — the same claim, the same shrink, the same second visit for a row
/// assigned less than it asked for. A row wider in rows than its assignment is
/// what a cell could not give up, and it grows the grid rather than being cut,
/// exactly as an over-wide cell grows its column.
fn rows_of<'f>(
    rows: &'f [Vec<FittedCell<'_>>],
    constraint: Constraint,
) -> (Vec<usize>, Vec<Vec<SizedCell<'f>>>) {
    let asked: Vec<Vec<Sized<'f>>> = rows
        .iter()
        .map(|row| {
            row.iter()
                .map(|cell| heights(&cell.child, Constraint::unbounded()))
                .collect()
        })
        .collect();
    let claims: Vec<Claim> = rows
        .iter()
        .zip(&asked)
        .map(|(row, asked)| Claim {
            kind: if row.iter().any(|cell| cell.child.height_projection) {
                Kind::fill(1)
            } else {
                Kind::Auto
            },
            demand: row
                .iter()
                .zip(asked)
                .map(|(cell, sized)| sized.height + vertical(cell.padding))
                .max()
                .unwrap_or(0),
            floor: row
                .iter()
                .map(|cell| cell.child.height_floor + vertical(cell.padding))
                .max()
                .unwrap_or(0),
        })
        .collect();
    let shares = distribute_constraints(constraint.reference, constraint.cap, &claims);

    let mut settled_heights = shares.clone();
    let mut settled = Vec::with_capacity(rows.len());
    for ((index, row), asked) in rows.iter().enumerate().zip(asked) {
        let share = shares[index];
        let mut cells = Vec::with_capacity(row.len());
        for (cell, asked) in row.iter().zip(asked) {
            let mut padding = cell.padding;
            let down = degrade(
                Some(share),
                0,
                0,
                vertical(padding),
                cell.child.height_floor,
            );
            if down.padding {
                padding.top = 0;
                padding.bottom = 0;
            }
            let inner = share.saturating_sub(vertical(padding));
            let child = if constraint.cap.is_none() && asked.height <= inner {
                asked
            } else {
                heights(&cell.child, Constraint::established(inner))
            };
            settled_heights[index] = settled_heights[index].max(child.height + vertical(padding));
            cells.push(SizedCell {
                padding,
                align: cell.align,
                vertical_align: cell.vertical_align,
                child,
            });
        }
        settled.push(cells);
    }
    (settled_heights, settled)
}

/// Divides `area` among a column's children and settles each at its share.
///
/// A height demand cannot be read off a style the way a width can: it depends
/// on how the content fitted at the cross-axis width, so the demand *is* a
/// height resolution. Because a resolution is a pure function of the node and
/// the area, that answer says nothing about a sibling — which is why the model
/// permits the question (`docs/design/layout-resolution.md`).
///
/// The answer is kept, so the ordinary case settles each child once. Only a
/// child a deficit assigns *less* than it asked for is settled again at that
/// assignment, which closes its frame at the smaller size instead of cutting
/// it. That second visit is arithmetic over lines that were fitted before this
/// function ran; nothing is fitted twice.
fn divide<'f>(children: &'f [Fitted<'_>], constraint: Constraint) -> Vec<Sized<'f>> {
    if constraint.reference.is_none() {
        return children
            .iter()
            .map(|child| heights(child, Constraint::unbounded()))
            .collect();
    }
    let asked: Vec<Option<Sized<'f>>> = children
        .iter()
        .map(|child| match child.height_kind {
            Kind::Fill(_) => None,
            _ => Some(heights(child, Constraint::unbounded())),
        })
        .collect();
    let claims: Vec<Claim> = children
        .iter()
        .zip(&asked)
        .map(|(child, sized)| Claim {
            kind: child.height_kind,
            demand: sized.as_ref().map_or(0, |sized| sized.height),
            floor: child.height_floor,
        })
        .collect();

    children
        .iter()
        .zip(asked)
        .zip(distribute_constraints(
            constraint.reference,
            constraint.cap,
            &claims,
        ))
        .map(|((child, asked), height)| match asked {
            Some(sized) if constraint.cap.is_none() && sized.height <= height => sized,
            _ => heights(child, Constraint::established(height)),
        })
        .collect()
}

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

    #[test]
    fn a_clip_keeps_the_marker_inside_the_width() {
        assert_eq!(clip_line(PrintableText::new("hello world"), 5, ""), "hello");
        assert_eq!(
            clip_line(PrintableText::new("hello world"), 5, "…"),
            "hell…"
        );
        assert_eq!(
            clip_line(PrintableText::new("hello world"), 5, "..."),
            "he..."
        );
        assert_eq!(
            clip_line(PrintableText::new("hello"), 9, "…"),
            "hello",
            "a line that fits is untouched"
        );
    }

    #[test]
    fn a_clip_never_splits_a_grapheme_and_measures_the_marker_in_cells() {
        assert_eq!(
            clip_line(PrintableText::new("日本語"), 5, ""),
            "日本",
            "the third would straddle"
        );
        assert_eq!(
            clip_line(PrintableText::new("日本語"), 4, "→"),
            "日→",
            "a wide marker costs two"
        );
        assert_eq!(
            clip_line(PrintableText::new("e\u{301}xyz"), 2, ""),
            "e\u{301}x"
        );
    }

    #[test]
    fn a_marker_that_cannot_fit_is_dropped() {
        assert_eq!(
            clip_line(PrintableText::new("hello"), 3, "..."),
            "hel",
            "no room for content"
        );
        assert_eq!(
            clip_line(PrintableText::new("hello"), 1, "…"),
            "h",
            "the marker fills the box"
        );
        assert_eq!(
            clip_line(PrintableText::new("hello"), 0, "…"),
            "",
            "nothing fits at all"
        );
    }
}