gilt 2.2.0

Fast, beautiful terminal formatting for Rust — styles, tables, trees, syntax highlighting, progress bars, markdown.
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
//! Constrain widget -- limits the width of a renderable to a given number of characters.
//!

use std::cmp::min;
use std::fmt;

use crate::console::{Console, ConsoleOptions, Renderable, RenderableArc};
use crate::measure::Measurement;
use crate::segment::Segment;

// ---------------------------------------------------------------------------
// Constrain
// ---------------------------------------------------------------------------

/// A widget that constrains the width of its content to a given number of
/// characters.
///
/// When `width` is `Some(w)`, the content is rendered with a maximum width of
/// `min(w, options.max_width)`.  When `width` is `None`, the content passes
/// through unmodified.
#[derive(Clone)]
pub struct Constrain {
    /// The content to constrain (any renderable widget).
    pub renderable: RenderableArc,
    /// Maximum width in characters. `None` means no constraint is applied.
    pub width: Option<usize>,
}

// Manual Debug — RenderableArc (Arc<dyn Renderable + Send + Sync>) doesn't
// implement Debug, so we print a placeholder for the renderable field.
impl std::fmt::Debug for Constrain {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("Constrain")
            .field("renderable", &"<renderable>")
            .field("width", &self.width)
            .finish()
    }
}

impl Constrain {
    /// Create a new `Constrain` widget.
    ///
    /// `width` defaults to `Some(80)` following the Python implementation.
    pub fn new(renderable: impl Renderable + Send + Sync + 'static, width: Option<usize>) -> Self {
        Constrain {
            renderable: std::sync::Arc::new(renderable),
            width,
        }
    }

    /// Builder method to set the width.
    #[must_use]
    pub fn with_width(mut self, width: usize) -> Self {
        self.width = Some(width);
        self
    }

    /// Measure the minimum and maximum width requirements of the constrained
    /// content.
    ///
    /// If `width` is `Some(w)`, the options are constrained to that width
    /// before measuring.  The resulting measurement is then clamped to the
    /// constrained width.
    pub fn measure(&self, console: &Console, options: &ConsoleOptions) -> Measurement {
        if let Some(w) = self.width {
            let constrained = options.update_width(w);
            self.renderable
                .gilt_measure(console, &constrained)
                .with_maximum(constrained.max_width)
                // belt-and-suspenders: gilt_measure may return a width > constrained.max_width
                // for some Renderable types; clamp again to options.max_width as the final gate.
                .with_maximum(options.max_width)
        } else {
            self.renderable
                .gilt_measure(console, options)
                .with_maximum(options.max_width)
        }
    }
}

impl Renderable for Constrain {
    fn gilt_measure(&self, console: &Console, options: &ConsoleOptions) -> Measurement {
        self.measure(console, options)
    }

    fn gilt_console(&self, console: &Console, options: &ConsoleOptions) -> Vec<Segment> {
        match self.width {
            None => self.renderable.as_ref().gilt_console(console, options),
            Some(w) => {
                let constrained_width = min(w, options.max_width);
                let child_options = options.update_width(constrained_width);
                self.renderable
                    .as_ref()
                    .gilt_console(console, &child_options)
            }
        }
    }
}

// ---------------------------------------------------------------------------
// Display
// ---------------------------------------------------------------------------

impl fmt::Display for Constrain {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        let w = f.width().unwrap_or(80);
        let mut console = Console::builder()
            .width(w)
            .force_terminal(true)
            .no_color(true)
            .build();
        console.begin_capture();
        console.print(self);
        let output = console.end_capture();
        write!(f, "{}", output.trim_end_matches('\n'))
    }
}

// ---------------------------------------------------------------------------
// Tests
// ---------------------------------------------------------------------------

#[cfg(test)]
mod tests {
    use super::*;
    use crate::style::Style;
    use crate::text::Text;

    fn make_console(width: usize) -> Console {
        Console::builder()
            .width(width)
            .force_terminal(true)
            .no_color(true)
            .markup(false)
            .build()
    }

    fn segments_to_text(segments: &[Segment]) -> String {
        segments.iter().map(|s| s.text.as_str()).collect()
    }

    // -- Construction -------------------------------------------------------

    #[test]
    fn test_default_construction() {
        // Updated: field is now RenderableArc (Text still works via Renderable impl)
        let text = Text::new("Hello, world!", Style::null());
        let c = Constrain::new(text.clone(), Some(80));
        assert_eq!(c.width, Some(80));
        // Cannot call .plain() on RenderableArc — verify width instead
    }

    #[test]
    fn test_none_width_construction() {
        let text = Text::new("Hello", Style::null());
        let c = Constrain::new(text, None);
        assert_eq!(c.width, None);
    }

    #[test]
    fn test_builder_method() {
        let text = Text::new("Hello", Style::null());
        let c = Constrain::new(text, None).with_width(40);
        assert_eq!(c.width, Some(40));
    }

    // -- Passthrough (width = None) -----------------------------------------

    #[test]
    fn test_none_width_passthrough() {
        // Updated: field is now RenderableArc (Text still works via Renderable impl)
        let console = make_console(80);
        let opts = console.options();
        let text = Text::new("Hello, world!", Style::null());
        // Clone text before passing to Constrain::new since it moves the value
        let text_direct = text.clone();
        let c = Constrain::new(text, None);

        let constrained_segments = c.gilt_console(&console, &opts);
        let direct_segments = text_direct.gilt_console(&console, &opts);

        assert_eq!(
            segments_to_text(&constrained_segments),
            segments_to_text(&direct_segments),
        );
    }

    // -- Width smaller than content -----------------------------------------

    #[test]
    fn test_width_smaller_than_content() {
        let console = make_console(80);
        let opts = console.options();
        // "Hello, world!" is 13 chars; constrain to 5
        let text = Text::new("Hello, world!", Style::null());
        let c = Constrain::new(text, Some(5));

        let segments = c.gilt_console(&console, &opts);
        let output = segments_to_text(&segments);

        // Each line should be at most 5 cells wide (the text will wrap)
        for line in output.split('\n') {
            if !line.is_empty() {
                assert!(
                    crate::cells::cell_len(line) <= 5,
                    "Line '{}' exceeds constrained width of 5 (actual: {})",
                    line,
                    crate::cells::cell_len(line),
                );
            }
        }
    }

    // -- Width larger than content ------------------------------------------

    #[test]
    fn test_width_larger_than_content() {
        let console = make_console(80);
        let opts = console.options();
        let text = Text::new("Hi", Style::null());
        let c = Constrain::new(text.clone(), Some(40));

        let constrained_segments = c.gilt_console(&console, &opts);
        let direct_segments = text.gilt_console(&console, &opts);

        // With width larger than content, the rendering should be the same as
        // rendering into the full console width (since the content fits)
        assert_eq!(
            segments_to_text(&constrained_segments),
            segments_to_text(&direct_segments),
        );
    }

    // -- Width constrains to min(width, max_width) --------------------------

    #[test]
    fn test_width_min_of_width_and_max_width() {
        // Console width is 10, constrain width is 20.
        // Effective constraint should be min(20, 10) = 10.
        let console = make_console(10);
        let opts = console.options();
        let text = Text::new("ABCDEFGHIJKLMNOP", Style::null());

        let c = Constrain::new(text, Some(20));
        let segments = c.gilt_console(&console, &opts);
        let output = segments_to_text(&segments);

        for line in output.split('\n') {
            if !line.is_empty() {
                assert!(
                    crate::cells::cell_len(line) <= 10,
                    "Line '{}' exceeds effective width of 10 (actual: {})",
                    line,
                    crate::cells::cell_len(line),
                );
            }
        }
    }

    // -- Measure with width -------------------------------------------------

    #[test]
    fn test_measure_with_width() {
        let console = make_console(80);
        let opts = console.options();
        let text = Text::new("Hello, world!", Style::null());

        // Constrain to 5: measurement maximum should be at most 5
        let c = Constrain::new(text, Some(5));
        let m = c.measure(&console, &opts);
        assert!(m.maximum <= 5, "Expected max <= 5, got {}", m.maximum);
    }

    #[test]
    fn test_measure_without_width() {
        // Updated: field is now RenderableArc (Text still works via Renderable impl)
        // Constrain::measure now delegates to gilt_measure instead of Text::measure
        let console = make_console(80);
        let opts = console.options();
        let text = Text::new("Hello", Style::null());

        let c = Constrain::new(text.clone(), None);
        let m = c.measure(&console, &opts);

        // Without width constraint, measure should match text.gilt_measure()
        // clamped to options.max_width
        let text_m = text
            .gilt_measure(&console, &opts)
            .with_maximum(opts.max_width);
        assert_eq!(m.minimum, text_m.minimum);
        assert_eq!(m.maximum, text_m.maximum);
    }

    #[test]
    fn test_measure_width_larger_than_content() {
        let console = make_console(80);
        let opts = console.options();
        let text = Text::new("Hello", Style::null());

        // Constrain to 40 but text is only 5 wide
        let c = Constrain::new(text, Some(40));
        let m = c.measure(&console, &opts);

        assert_eq!(m.maximum, 5);
    }

    #[test]
    fn test_measure_width_smaller_than_console() {
        let console = make_console(80);
        let opts = console.options();
        let text = Text::new("Hello, world! This is a long sentence.", Style::null());

        let c = Constrain::new(text, Some(10));
        let m = c.measure(&console, &opts);

        assert!(m.maximum <= 10, "Expected max <= 10, got {}", m.maximum,);
    }

    // -- Styled content -----------------------------------------------------

    #[test]
    fn test_styled_content_preserved() {
        let console = make_console(80);
        let opts = console.options();
        let text = Text::styled("Bold text", "bold");

        let c = Constrain::new(text, Some(40));
        let segments = c.gilt_console(&console, &opts);

        // The styled content should still carry its style through
        let has_styled = segments
            .iter()
            .any(|s| s.text.contains("Bold text") && s.style.is_some());
        assert!(has_styled, "Expected styled segment in output");
    }

    // -- Clone and Debug derive checks --------------------------------------

    #[test]
    fn test_clone() {
        // Updated: field is now RenderableArc (Text still works via Renderable impl)
        let text = Text::new("Hello", Style::null());
        let c = Constrain::new(text, Some(40));
        let cloned = c.clone();
        assert_eq!(cloned.width, c.width);
        // Cannot call .plain() on RenderableArc — verify width is correct
    }

    #[test]
    fn test_debug() {
        let text = Text::new("Hello", Style::null());
        let c = Constrain::new(text, Some(40));
        let debug = format!("{:?}", c);
        assert!(debug.contains("Constrain"));
        assert!(debug.contains("40"));
    }

    // -- Edge cases ---------------------------------------------------------

    #[test]
    fn test_zero_width() {
        let console = make_console(80);
        let opts = console.options();
        let text = Text::new("Hello", Style::null());
        let c = Constrain::new(text, Some(0));
        let segments = c.gilt_console(&console, &opts);
        let output = segments_to_text(&segments);

        // With width 0, all content lines should be empty
        for line in output.split('\n') {
            assert!(
                crate::cells::cell_len(line) == 0,
                "Expected empty line, got '{}'",
                line,
            );
        }
    }

    #[test]
    fn test_empty_text() {
        let console = make_console(80);
        let opts = console.options();
        let text = Text::new("", Style::null());
        let c = Constrain::new(text, Some(40));
        let segments = c.gilt_console(&console, &opts);
        let output = segments_to_text(&segments);
        // Empty text should produce only the end segment (newline)
        assert!(output.trim().is_empty());
    }

    #[test]
    fn test_width_equal_to_content() {
        let console = make_console(80);
        let opts = console.options();
        let text = Text::new("Hello", Style::null());
        let c = Constrain::new(text, Some(5));
        let segments = c.gilt_console(&console, &opts);
        let output = segments_to_text(&segments);

        // Content exactly fits the constraint -- should not wrap
        let content_lines: Vec<&str> = output.split('\n').filter(|l| !l.is_empty()).collect();
        assert_eq!(content_lines.len(), 1);
        assert_eq!(content_lines[0], "Hello");
    }

    // -- gilt_measure override -----------------------------------------------

    #[test]
    fn constrain_gilt_measure_matches_standalone() {
        let console = make_console(80);
        let opts = console.options();
        let text = Text::new("Hello, world!", Style::null());
        let c = Constrain::new(text, Some(5));
        let m_standalone = c.measure(&console, &opts);
        let m_trait = c.gilt_measure(&console, &opts);
        assert_eq!(
            m_trait, m_standalone,
            "Constrain::gilt_measure must delegate to Constrain::measure"
        );
    }

    #[test]
    fn constrain_gilt_measure_no_width_matches_standalone() {
        let console = make_console(80);
        let opts = console.options();
        let text = Text::new("Hello", Style::null());
        let c = Constrain::new(text, None);
        let m_standalone = c.measure(&console, &opts);
        let m_trait = c.gilt_measure(&console, &opts);
        assert_eq!(
            m_trait, m_standalone,
            "Constrain::gilt_measure (no width) must delegate to Constrain::measure"
        );
    }

    // -- Task 4.5: RenderableArc constructor tests ---------------------------

    #[test]
    // Updated: constructors now accept impl Renderable + Send + Sync + 'static (Text still works via Renderable impl)
    fn constrain_new_text_still_compiles() {
        let _ = Constrain::new(Text::new("x", Style::null()), None);
    }

    #[test]
    // Updated: constructors now accept impl Renderable + Send + Sync + 'static (Panel works too)
    fn constrain_new_panel_compiles() {
        let p = crate::panel::Panel::new(Text::new("x", Style::null()));
        let _ = Constrain::new(p, None);
    }
}