ratatui 0.30.0

A library that's all about cooking up terminal user interfaces
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
use std::error::Error;

use ratatui::backend::TestBackend;
use ratatui::layout::Rect;
use ratatui::widgets::{Block, Paragraph, Widget};
use ratatui::{Terminal, TerminalOptions, Viewport};

#[test]
fn swap_buffer_clears_prev_buffer() {
    let backend = TestBackend::new(100, 50);
    let mut terminal = Terminal::new(backend).unwrap();
    terminal
        .current_buffer_mut()
        .set_string(0, 0, "Hello", ratatui::style::Style::reset());
    assert_eq!(terminal.current_buffer_mut().content()[0].symbol(), "H");
    terminal.swap_buffers();
    assert_eq!(terminal.current_buffer_mut().content()[0].symbol(), " ");
}

#[test]
fn terminal_draw_returns_the_completed_frame() -> Result<(), Box<dyn Error>> {
    let backend = TestBackend::new(10, 10);
    let mut terminal = Terminal::new(backend)?;
    let frame = terminal.draw(|f| {
        let paragraph = Paragraph::new("Test");
        f.render_widget(paragraph, f.area());
    })?;
    assert_eq!(frame.buffer[(0, 0)].symbol(), "T");
    assert_eq!(frame.area, Rect::new(0, 0, 10, 10));
    terminal.backend_mut().resize(8, 8);
    let frame = terminal.draw(|f| {
        let paragraph = Paragraph::new("test");
        f.render_widget(paragraph, f.area());
    })?;
    assert_eq!(frame.buffer[(0, 0)].symbol(), "t");
    assert_eq!(frame.area, Rect::new(0, 0, 8, 8));
    Ok(())
}

#[test]
fn terminal_draw_increments_frame_count() -> Result<(), Box<dyn Error>> {
    let backend = TestBackend::new(10, 10);
    let mut terminal = Terminal::new(backend)?;
    let frame = terminal.draw(|f| {
        assert_eq!(f.count(), 0);
        let paragraph = Paragraph::new("Test");
        f.render_widget(paragraph, f.area());
    })?;
    assert_eq!(frame.count, 0);
    let frame = terminal.draw(|f| {
        assert_eq!(f.count(), 1);
        let paragraph = Paragraph::new("test");
        f.render_widget(paragraph, f.area());
    })?;
    assert_eq!(frame.count, 1);
    let frame = terminal.draw(|f| {
        assert_eq!(f.count(), 2);
        let paragraph = Paragraph::new("test");
        f.render_widget(paragraph, f.area());
    })?;
    assert_eq!(frame.count, 2);
    Ok(())
}

#[test]
fn terminal_insert_before_moves_viewport() -> Result<(), Box<dyn Error>> {
    // When we have a terminal with 5 lines, and a single line viewport, if we insert a
    // number of lines less than the `terminal height - viewport height` it should move
    // viewport down to accommodate the new lines.

    let backend = TestBackend::new(20, 5);
    let mut terminal = Terminal::with_options(
        backend,
        TerminalOptions {
            viewport: Viewport::Inline(1),
        },
    )?;

    // insert_before cannot guarantee the contents of the viewport remain unharmed
    // by potential scrolling as such it is necessary to call draw afterwards to
    // redraw the contents of the viewport over the newly designated area.
    terminal.insert_before(2, |buf| {
        Paragraph::new(vec![
            "------ Line 1 ------".into(),
            "------ Line 2 ------".into(),
        ])
        .render(buf.area, buf);
    })?;

    terminal.draw(|f| {
        let paragraph = Paragraph::new("[---- Viewport ----]");
        f.render_widget(paragraph, f.area());
    })?;

    terminal.backend().assert_buffer_lines([
        "------ Line 1 ------",
        "------ Line 2 ------",
        "[---- Viewport ----]",
        "                    ",
        "                    ",
    ]);
    terminal.backend().assert_scrollback_empty();

    Ok(())
}

#[test]
#[cfg(feature = "scrolling-regions")]
fn terminal_insert_before_moves_viewport_does_not_clobber() -> Result<(), Box<dyn Error>> {
    // This is like terminal_insert_before_moves_viewport, except it draws first before calling
    // insert_before, and doesn't draw again afterwards. When using scrolling regions, we
    // shouldn't clobber the viewport.

    let backend = TestBackend::new(20, 5);
    let mut terminal = Terminal::with_options(
        backend,
        TerminalOptions {
            viewport: Viewport::Inline(1),
        },
    )?;

    terminal.draw(|f| {
        let paragraph = Paragraph::new("[---- Viewport ----]");
        f.render_widget(paragraph, f.area());
    })?;

    terminal.insert_before(2, |buf| {
        Paragraph::new(vec![
            "------ Line 1 ------".into(),
            "------ Line 2 ------".into(),
        ])
        .render(buf.area, buf);
    })?;

    terminal.backend().assert_scrollback_empty();
    terminal.backend().assert_buffer_lines([
        "------ Line 1 ------",
        "------ Line 2 ------",
        "[---- Viewport ----]",
        "                    ",
        "                    ",
    ]);

    Ok(())
}

#[test]
fn terminal_insert_before_scrolls_on_large_input() -> Result<(), Box<dyn Error>> {
    // When we have a terminal with 5 lines, and a single line viewport, if we insert many
    // lines before the viewport (greater than `terminal height - viewport height`) it should
    // move the viewport down to the bottom of the terminal and scroll all lines above the viewport
    // until all have been added to the buffer.

    let backend = TestBackend::new(20, 5);
    let mut terminal = Terminal::with_options(
        backend,
        TerminalOptions {
            viewport: Viewport::Inline(1),
        },
    )?;

    terminal.insert_before(5, |buf| {
        Paragraph::new(vec![
            "------ Line 1 ------".into(),
            "------ Line 2 ------".into(),
            "------ Line 3 ------".into(),
            "------ Line 4 ------".into(),
            "------ Line 5 ------".into(),
        ])
        .render(buf.area, buf);
    })?;

    terminal.draw(|f| {
        let paragraph = Paragraph::new("[---- Viewport ----]");
        f.render_widget(paragraph, f.area());
    })?;

    terminal.backend().assert_buffer_lines([
        "------ Line 2 ------",
        "------ Line 3 ------",
        "------ Line 4 ------",
        "------ Line 5 ------",
        "[---- Viewport ----]",
    ]);
    terminal
        .backend()
        .assert_scrollback_lines(["------ Line 1 ------"]);

    Ok(())
}

#[test]
#[cfg(feature = "scrolling-regions")]
fn terminal_insert_before_scrolls_on_large_input_does_not_clobber() -> Result<(), Box<dyn Error>> {
    // This is like terminal_insert_scrolls_on_large_input, except it draws first before calling
    // insert_before, and doesn't draw again afterwards. When using scrolling regions, we
    // shouldn't clobber the viewport.

    let backend = TestBackend::new(20, 5);
    let mut terminal = Terminal::with_options(
        backend,
        TerminalOptions {
            viewport: Viewport::Inline(1),
        },
    )?;

    terminal.draw(|f| {
        let paragraph = Paragraph::new("[---- Viewport ----]");
        f.render_widget(paragraph, f.area());
    })?;

    terminal.insert_before(5, |buf| {
        Paragraph::new(vec![
            "------ Line 1 ------".into(),
            "------ Line 2 ------".into(),
            "------ Line 3 ------".into(),
            "------ Line 4 ------".into(),
            "------ Line 5 ------".into(),
        ])
        .render(buf.area, buf);
    })?;

    terminal
        .backend()
        .assert_scrollback_lines(["------ Line 1 ------"]);
    terminal.backend().assert_buffer_lines([
        "------ Line 2 ------",
        "------ Line 3 ------",
        "------ Line 4 ------",
        "------ Line 5 ------",
        "[---- Viewport ----]",
    ]);

    Ok(())
}

#[test]
fn terminal_insert_before_scrolls_on_many_inserts() -> Result<(), Box<dyn Error>> {
    // This test ensures similar behaviour to `terminal_insert_before_scrolls_on_large_input`
    // but covers a bug previously present whereby multiple small insertions
    // (less than `terminal height - viewport height`) would have disparate behaviour to one large
    // insertion. This was caused by an undocumented cap on the height to be inserted, which has now
    // been removed.

    let backend = TestBackend::new(20, 5);
    let mut terminal = Terminal::with_options(
        backend,
        TerminalOptions {
            viewport: Viewport::Inline(1),
        },
    )?;

    terminal.insert_before(1, |buf| {
        Paragraph::new(vec!["------ Line 1 ------".into()]).render(buf.area, buf);
    })?;

    terminal.insert_before(1, |buf| {
        Paragraph::new(vec!["------ Line 2 ------".into()]).render(buf.area, buf);
    })?;

    terminal.insert_before(1, |buf| {
        Paragraph::new(vec!["------ Line 3 ------".into()]).render(buf.area, buf);
    })?;

    terminal.insert_before(1, |buf| {
        Paragraph::new(vec!["------ Line 4 ------".into()]).render(buf.area, buf);
    })?;

    terminal.insert_before(1, |buf| {
        Paragraph::new(vec!["------ Line 5 ------".into()]).render(buf.area, buf);
    })?;

    terminal.draw(|f| {
        let paragraph = Paragraph::new("[---- Viewport ----]");
        f.render_widget(paragraph, f.area());
    })?;

    terminal.backend().assert_buffer_lines([
        "------ Line 2 ------",
        "------ Line 3 ------",
        "------ Line 4 ------",
        "------ Line 5 ------",
        "[---- Viewport ----]",
    ]);
    terminal
        .backend()
        .assert_scrollback_lines(["------ Line 1 ------"]);

    Ok(())
}

#[test]
#[cfg(feature = "scrolling-regions")]
fn terminal_insert_before_scrolls_on_many_inserts_does_not_clobber() -> Result<(), Box<dyn Error>> {
    // This is like terminal_insert_before_scrolls_on_many_inserts, except it draws first before
    // calling insert_before, and doesn't draw again afterwards. When using scrolling regions, we
    // shouldn't clobber the viewport.

    let backend = TestBackend::new(20, 5);
    let mut terminal = Terminal::with_options(
        backend,
        TerminalOptions {
            viewport: Viewport::Inline(1),
        },
    )?;

    terminal.draw(|f| {
        let paragraph = Paragraph::new("[---- Viewport ----]");
        f.render_widget(paragraph, f.area());
    })?;

    terminal.insert_before(1, |buf| {
        Paragraph::new(vec!["------ Line 1 ------".into()]).render(buf.area, buf);
    })?;

    terminal.insert_before(1, |buf| {
        Paragraph::new(vec!["------ Line 2 ------".into()]).render(buf.area, buf);
    })?;

    terminal.insert_before(1, |buf| {
        Paragraph::new(vec!["------ Line 3 ------".into()]).render(buf.area, buf);
    })?;

    terminal.insert_before(1, |buf| {
        Paragraph::new(vec!["------ Line 4 ------".into()]).render(buf.area, buf);
    })?;

    terminal.insert_before(1, |buf| {
        Paragraph::new(vec!["------ Line 5 ------".into()]).render(buf.area, buf);
    })?;

    terminal
        .backend()
        .assert_scrollback_lines(["------ Line 1 ------"]);
    terminal.backend().assert_buffer_lines([
        "------ Line 2 ------",
        "------ Line 3 ------",
        "------ Line 4 ------",
        "------ Line 5 ------",
        "[---- Viewport ----]",
    ]);

    Ok(())
}

#[test]
fn terminal_insert_before_large_viewport() -> Result<(), Box<dyn Error>> {
    // This test covers a bug previously present whereby doing an insert_before when the
    // viewport covered the entire screen would cause a panic.

    let backend = TestBackend::new(20, 3);
    let mut terminal = Terminal::with_options(
        backend,
        TerminalOptions {
            viewport: Viewport::Inline(3),
        },
    )?;

    terminal.insert_before(1, |buf| {
        Paragraph::new(vec!["------ Line 1 ------".into()]).render(buf.area, buf);
    })?;

    terminal.insert_before(3, |buf| {
        Paragraph::new(vec![
            "------ Line 2 ------".into(),
            "------ Line 3 ------".into(),
            "------ Line 4 ------".into(),
        ])
        .render(buf.area, buf);
    })?;

    terminal.insert_before(7, |buf| {
        Paragraph::new(vec![
            "------ Line 5 ------".into(),
            "------ Line 6 ------".into(),
            "------ Line 7 ------".into(),
            "------ Line 8 ------".into(),
            "------ Line 9 ------".into(),
            "----- Line 10 ------".into(),
            "----- Line 11 ------".into(),
        ])
        .render(buf.area, buf);
    })?;

    terminal.draw(|f| {
        let paragraph = Paragraph::new("Viewport")
            .centered()
            .block(Block::bordered());
        f.render_widget(paragraph, f.area());
    })?;

    terminal.backend().assert_buffer_lines([
        "┌──────────────────┐",
        "│     Viewport     │",
        "└──────────────────┘",
    ]);
    terminal.backend().assert_scrollback_lines([
        "------ Line 1 ------",
        "------ Line 2 ------",
        "------ Line 3 ------",
        "------ Line 4 ------",
        "------ Line 5 ------",
        "------ Line 6 ------",
        "------ Line 7 ------",
        "------ Line 8 ------",
        "------ Line 9 ------",
        "----- Line 10 ------",
        "----- Line 11 ------",
    ]);

    Ok(())
}

#[test]
#[cfg(feature = "scrolling-regions")]
fn terminal_insert_before_large_viewport_does_not_clobber() -> Result<(), Box<dyn Error>> {
    // This is like terminal_insert_before_large_viewport, except it draws first before calling
    // insert_before, and doesn't draw again afterwards. When using scrolling regions, we shouldn't
    // clobber the viewport.

    let backend = TestBackend::new(20, 3);
    let mut terminal = Terminal::with_options(
        backend,
        TerminalOptions {
            viewport: Viewport::Inline(3),
        },
    )?;

    terminal.draw(|f| {
        let paragraph = Paragraph::new("Viewport")
            .centered()
            .block(Block::bordered());
        f.render_widget(paragraph, f.area());
    })?;

    terminal.insert_before(1, |buf| {
        Paragraph::new(vec!["------ Line 1 ------".into()]).render(buf.area, buf);
    })?;

    terminal.insert_before(3, |buf| {
        Paragraph::new(vec![
            "------ Line 2 ------".into(),
            "------ Line 3 ------".into(),
            "------ Line 4 ------".into(),
        ])
        .render(buf.area, buf);
    })?;

    terminal.insert_before(7, |buf| {
        Paragraph::new(vec![
            "------ Line 5 ------".into(),
            "------ Line 6 ------".into(),
            "------ Line 7 ------".into(),
            "------ Line 8 ------".into(),
            "------ Line 9 ------".into(),
            "----- Line 10 ------".into(),
            "----- Line 11 ------".into(),
        ])
        .render(buf.area, buf);
    })?;

    terminal.backend().assert_buffer_lines([
        "┌──────────────────┐",
        "│     Viewport     │",
        "└──────────────────┘",
    ]);
    terminal.backend().assert_scrollback_lines([
        "------ Line 1 ------",
        "------ Line 2 ------",
        "------ Line 3 ------",
        "------ Line 4 ------",
        "------ Line 5 ------",
        "------ Line 6 ------",
        "------ Line 7 ------",
        "------ Line 8 ------",
        "------ Line 9 ------",
        "----- Line 10 ------",
        "----- Line 11 ------",
    ]);

    Ok(())
}