shpool-vterm 0.2.6

An in-memory terminal to support session restore in shpool.
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
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
// Copyright 2026 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

#[macro_use]
#[path = "support/mod.rs"]
mod support;

use shpool_vterm::{term, ContentRegion};

frag! {
    reverse_index_partial_screen { scrollback_lines: 100, width: 5, height: 10 }
    <= term::Raw::from("11"), term::Crlf::default(),
       term::Raw::from("22"),
       term::ControlCodes::cursor_position(1, 1),
       term::control_codes().reverse_index,
       term::Raw::from("XX")
    => ContentRegion::All =>
            reset_codes,
            term::Raw::from("XX"),
            term::Crlf::default(),
            term::Raw::from("11"),
            term::Crlf::default(),
            term::Raw::from("22"),
            term::ControlCodes::cursor_position(1, 3),
            term::control_codes().clear_attrs
}

frag! {
    insert_lines_partial_screen { scrollback_lines: 100, width: 5, height: 10 }
    <= term::Raw::from("11"), term::Crlf::default(),
       term::Raw::from("22"),
       term::ControlCodes::cursor_position(1, 1),
       term::ControlCodes::insert_lines(1),
       term::Raw::from("XX")
    => ContentRegion::All =>
            reset_codes,
            term::Raw::from("XX"),
            term::Crlf::default(),
            term::Raw::from("11"),
            term::Crlf::default(),
            term::Raw::from("22"),
            term::ControlCodes::cursor_position(1, 3),
            term::control_codes().clear_attrs
}

frag! {
    delete_lines_partial_screen { scrollback_lines: 100, width: 5, height: 10 }
    <= term::Raw::from("11"), term::Crlf::default(),
       term::Raw::from("22"), term::Crlf::default(),
       term::Raw::from("33"),
       term::ControlCodes::cursor_position(2, 1),
       term::ControlCodes::delete_lines(1)
    => ContentRegion::All =>
            reset_codes,
            term::Raw::from("11"),
            term::Crlf::default(),
            term::Raw::from("33"),
            term::Crlf::default(),
            term::ControlCodes::cursor_position(2, 1),
            term::control_codes().clear_attrs
}

// DCH (Delete Character) with the cursor to the right of the last cell that
// has actually been written.
//
// A line only stores cells out to the last one written; trailing blanks are
// implicit. That makes `min(cells.len(), col + n) - col` in delete_character
// underflow whenever the cursor is parked past the end of a short line, which
// CUF, CHA and CUP all manage without writing anything. The sibling ops
// (insert_character, erase_character) guard against it by clamping to width
// and padding up to col first.

// Control case: the cursor sits exactly on cells.len(), the rightmost
// position that cannot underflow. It anchors the expected output of the cases
// below, showing that DCH pads the line out to the full width even when it
// has nothing real to delete.
frag! {
    delete_character_at_end_of_line { scrollback_lines: 100, width: 5, height: 10 }
    <= term::Raw::from("ab"),
       term::ControlCodes::delete_character(3)
    => ContentRegion::All =>
            reset_codes,
            term::Raw::from("ab   "),
            term::ControlCodes::cursor_position(1, 3),
            term::control_codes().clear_attrs
}

frag! {
    delete_character_past_end_of_line_via_cursor_forward
        { scrollback_lines: 100, width: 5, height: 10 }
    <= term::Raw::from("ab"),
       term::ControlCodes::cursor_forward(2),
       term::ControlCodes::delete_character(3)
    => ContentRegion::All =>
            reset_codes,
            term::Raw::from("ab   "),
            term::ControlCodes::cursor_position(1, 5),
            term::control_codes().clear_attrs
}

frag! {
    delete_character_past_end_of_line_via_horizontal_absolute
        { scrollback_lines: 100, width: 5, height: 10 }
    <= term::Raw::from("ab"),
       term::ControlCodes::cursor_horizontal_absolute(5),
       term::ControlCodes::delete_character(3)
    => ContentRegion::All =>
            reset_codes,
            term::Raw::from("ab   "),
            term::ControlCodes::cursor_position(1, 5),
            term::control_codes().clear_attrs
}

frag! {
    delete_character_past_end_of_line_via_cursor_position
        { scrollback_lines: 100, width: 5, height: 10 }
    <= term::Raw::from("ab"),
       term::ControlCodes::cursor_position(1, 5),
       term::ControlCodes::delete_character(3)
    => ContentRegion::All =>
            reset_codes,
            term::Raw::from("ab   "),
            term::ControlCodes::cursor_position(1, 5),
            term::control_codes().clear_attrs
}

frag! {
    delete_character_past_end_of_line_second_row
        { scrollback_lines: 100, width: 5, height: 10 }
    <= term::Raw::from("ab"), term::Crlf::default(),
       term::Raw::from("cd"),
       term::ControlCodes::cursor_position(2, 5),
       term::ControlCodes::delete_character(3)
    => ContentRegion::All =>
            reset_codes,
            term::Raw::from("ab"),
            term::Crlf::default(),
            term::Raw::from("cd   "),
            term::ControlCodes::cursor_position(2, 5),
            term::control_codes().clear_attrs
}

//
// Zero width codepoints.
//

// Control case: the precomposed form of the same glyph, which takes the
// ordinary width-1 path today. It anchors the expected rendering of
// combining_mark_after_ascii below.
frag! {
    precomposed_char_needs_no_modifier { scrollback_lines: 100, width: 10, height: 10 }
    <= term::Raw::from("\u{e9}")
    => ContentRegion::All =>
            reset_codes,
            term::Raw::from("\u{e9}"),
            term::ControlCodes::cursor_position(1, 2),
            term::control_codes().clear_attrs
}

// 'e' followed by a combining acute accent. One cell, one column.
frag! {
    combining_mark_after_ascii { scrollback_lines: 100, width: 10, height: 10 }
    <= term::Raw::from("e\u{301}")
    => ContentRegion::All =>
            reset_codes,
            term::Raw::from("e\u{301}"),
            term::ControlCodes::cursor_position(1, 2),
            term::control_codes().clear_attrs
}

// VS16 requests the emoji presentation of the preceding symbol. We keep the
// base char's width here rather than promoting the cell to width 2: terminals
// disagree about the promotion, and getting the bytes back out intact is what
// session restore actually needs.
frag! {
    variation_selector_after_symbol { scrollback_lines: 100, width: 10, height: 10 }
    <= term::Raw::from("\u{2714}\u{fe0f}")
    => ContentRegion::All =>
            reset_codes,
            term::Raw::from("\u{2714}\u{fe0f}"),
            term::ControlCodes::cursor_position(1, 2),
            term::control_codes().clear_attrs
}

// A ZWJ sequence. The joiner attaches to the cell before it, and the emoji
// after the joiner starts a fresh width-2 cell, so the pair spans 4 columns.
frag! {
    zwj_emoji_sequence { scrollback_lines: 100, width: 10, height: 10 }
    <= term::Raw::from("\u{1f9d1}\u{200d}\u{1f4bb}")
    => ContentRegion::All =>
            reset_codes,
            term::Raw::from("\u{1f9d1}\u{200d}\u{1f4bb}"),
            term::ControlCodes::cursor_position(1, 5),
            term::control_codes().clear_attrs
}

// A combining mark with no cell to modify. There is nothing sensible to
// attach it to, so it gets dropped, but it must not take the session down.
frag! {
    combining_mark_with_no_preceding_cell { scrollback_lines: 100, width: 10, height: 10 }
    <= term::Raw::from("\u{301}")
    => ContentRegion::All =>
            reset_codes,
            term::ControlCodes::cursor_position(1, 1),
            term::control_codes().clear_attrs
}

// Same, but the cursor has moved to a fresh row. The mark must not reach back
// up to the last cell of the previous line.
frag! {
    combining_mark_at_start_of_line { scrollback_lines: 100, width: 10, height: 10 }
    <= term::Raw::from("ab"), term::Crlf::default(),
       term::Raw::from("\u{301}")
    => ContentRegion::All =>
            reset_codes,
            term::Raw::from("ab"),
            term::ControlCodes::cursor_position(2, 1),
            term::control_codes().clear_attrs
}

//
// Scroll region bottom past the last row of the screen.
//
// DECSTBM stores the requested bottom verbatim, so `CSI 1;5r` on a three row
// screen leaves a region extending past the grid. LF only scrolls once the
// cursor reaches the bottom of the region, so from the last row it walks the
// cursor off the screen instead. Real terminals clamp the bottom to the
// screen height, which is what these tests assert.
//
// These used to prime the buffer with an SU, back when SU moved the viewport
// rather than the grid, so that rows below the screen still resolved to lines
// and `bottom - cursor.row` underflowed in Scrollback::insert_lines and
// Scrollback::delete_lines. The viewport is gone, so the grid always starts at
// the bottom of the buffer and that underflow is no longer reachable.

// Control case: an in-range bottom, where LF scrolls and the cursor stays on
// the last row. Anchors the expected output of the three cases below.
frag! {
    scroll_region_bottom_at_last_row { scrollback_lines: 100, width: 5, height: 3 }
    <= term::Raw::from("aa"), term::Crlf::default(),
       term::Raw::from("bb"), term::Crlf::default(),
       term::Raw::from("cc"), term::Crlf::default(),
       term::Raw::from("dd"), term::Crlf::default(),
       term::Raw::from("ee"),
       term::ControlCodes::set_scroll_region(1, 3),
       term::ControlCodes::cursor_position(3, 1),
       term::Crlf::default(),
       term::Crlf::default(),
       term::control_codes().unset_scroll_region
    => ContentRegion::Screen =>
            reset_codes,
            term::Raw::from("ee"),
            term::Crlf::default(),
            term::Crlf::default(),
            term::ControlCodes::cursor_position(3, 1),
            term::control_codes().clear_attrs
}

// An out of range bottom must behave exactly like the control. Today nothing
// scrolls and the restore buffer ends with a CUP to row 5 of a three row
// screen, which is the mid screen cursor users see on reattach.
frag! {
    scroll_region_bottom_past_last_row { scrollback_lines: 100, width: 5, height: 3 }
    <= term::Raw::from("aa"), term::Crlf::default(),
       term::Raw::from("bb"), term::Crlf::default(),
       term::Raw::from("cc"), term::Crlf::default(),
       term::Raw::from("dd"), term::Crlf::default(),
       term::Raw::from("ee"),
       term::ControlCodes::set_scroll_region(1, 5),
       term::ControlCodes::cursor_position(3, 1),
       term::Crlf::default(),
       term::Crlf::default(),
       term::control_codes().unset_scroll_region
    => ContentRegion::Screen =>
            reset_codes,
            term::Raw::from("ee"),
            term::Crlf::default(),
            term::Crlf::default(),
            term::ControlCodes::cursor_position(3, 1),
            term::control_codes().clear_attrs
}

// IL with the cursor below the last row. Once the cursor is back on the
// screen the insert has nothing to push down, so the output matches the
// control.
frag! {
    insert_lines_with_cursor_past_last_row { scrollback_lines: 100, width: 5, height: 3 }
    <= term::Raw::from("aa"), term::Crlf::default(),
       term::Raw::from("bb"), term::Crlf::default(),
       term::Raw::from("cc"), term::Crlf::default(),
       term::Raw::from("dd"), term::Crlf::default(),
       term::Raw::from("ee"),
       term::ControlCodes::set_scroll_region(1, 5),
       term::ControlCodes::cursor_position(3, 1),
       term::Crlf::default(),
       term::Crlf::default(),
       term::control_codes().unset_scroll_region,
       term::ControlCodes::insert_lines(1)
    => ContentRegion::Screen =>
            reset_codes,
            term::Raw::from("ee"),
            term::Crlf::default(),
            term::Crlf::default(),
            term::ControlCodes::cursor_position(3, 1),
            term::control_codes().clear_attrs
}

// The same for DL, which has its own copy of the underflowing arithmetic.
frag! {
    delete_lines_with_cursor_past_last_row { scrollback_lines: 100, width: 5, height: 3 }
    <= term::Raw::from("aa"), term::Crlf::default(),
       term::Raw::from("bb"), term::Crlf::default(),
       term::Raw::from("cc"), term::Crlf::default(),
       term::Raw::from("dd"), term::Crlf::default(),
       term::Raw::from("ee"),
       term::ControlCodes::set_scroll_region(1, 5),
       term::ControlCodes::cursor_position(3, 1),
       term::Crlf::default(),
       term::Crlf::default(),
       term::control_codes().unset_scroll_region,
       term::ControlCodes::delete_lines(1)
    => ContentRegion::Screen =>
            reset_codes,
            term::Raw::from("ee"),
            term::Crlf::default(),
            term::Crlf::default(),
            term::ControlCodes::cursor_position(3, 1),
            term::control_codes().clear_attrs
}

// The alt screen keeps its grid in a buffer exactly one screen tall, so an out
// of range bottom indexes off the end of it rather than underflowing. The
// expected output here is what the in-range `CSI 1;3r` produces.
frag! {
    alt_screen_scroll_region_past_last_row { scrollback_lines: 100, width: 5, height: 3 }
    <= term::control_codes().enable_alt_screen,
       term::Raw::from("aa"), term::Crlf::default(),
       term::Raw::from("bb"), term::Crlf::default(),
       term::Raw::from("cc"),
       term::ControlCodes::set_scroll_region(1, 9),
       term::ControlCodes::scroll_up(1)
    => ContentRegion::All =>
            reset_codes,
            empty_scrollback,
            term::control_codes().enable_alt_screen,
            term::Raw::from("bb"),
            term::Crlf::default(),
            term::Raw::from("cc"),
            term::Crlf::default(),
            term::ControlCodes::set_scroll_region(1, 3),
            term::ControlCodes::cursor_position(3, 3),
            term::control_codes().clear_attrs
}

// Clamping the bottom can pull it up to or above the top, leaving a region
// that describes no rows at all. There is nothing to scroll, so it is dropped.
frag! {
    scroll_region_clamped_away { scrollback_lines: 100, width: 5, height: 3 }
    <= term::ControlCodes::set_scroll_region(5, 9)
    => ContentRegion::All =>
            reset_codes,
            term::ControlCodes::cursor_position(1, 1),
            term::control_codes().clear_attrs
}

// A shrinking resize strands a region that was in range when it was set, so
// the clamp has to be reapplied there too. shpool resizes the spool on every
// reattach, which is how a long lived session gets into this state.
#[test]
fn scroll_region_clamped_on_shrinking_resize() {
    use shpool_vterm::term::AsTermInput;

    let mut input = vec![];
    term::ControlCodes::set_scroll_region(1, 6).term_input_into(&mut input);

    let mut term = shpool_vterm::Term::new(100, shpool_vterm::Size { width: 5, height: 6 });
    term.process(input.as_slice());
    term.resize(shpool_vterm::Size { width: 5, height: 3 });

    let mut expected = vec![];
    crate::support::frag::reset_codes.term_input_into(&mut expected);
    term::ControlCodes::set_scroll_region(1, 3).term_input_into(&mut expected);
    term::ControlCodes::cursor_position(1, 1).term_input_into(&mut expected);
    term::control_codes().clear_attrs.term_input_into(&mut expected);

    assert_eq!(term.contents(ContentRegion::All), expected);
}

// Same, on the alt screen, where the stranded region also takes the session
// down on the next scroll.
#[test]
fn alt_screen_scroll_region_clamped_on_shrinking_resize() {
    use shpool_vterm::term::AsTermInput;

    let mut input = vec![];
    term::control_codes().enable_alt_screen.term_input_into(&mut input);
    term::ControlCodes::set_scroll_region(1, 6).term_input_into(&mut input);

    let mut term = shpool_vterm::Term::new(100, shpool_vterm::Size { width: 5, height: 6 });
    term.process(input.as_slice());
    term.resize(shpool_vterm::Size { width: 5, height: 3 });

    let mut scroll = vec![];
    term::ControlCodes::scroll_down(1).term_input_into(&mut scroll);
    term.process(scroll.as_slice());

    let mut expected = vec![];
    crate::support::frag::reset_codes.term_input_into(&mut expected);
    crate::support::frag::empty_scrollback.term_input_into(&mut expected);
    term::control_codes().enable_alt_screen.term_input_into(&mut expected);
    term::Crlf::default().term_input_into(&mut expected);
    term::Crlf::default().term_input_into(&mut expected);
    term::ControlCodes::set_scroll_region(1, 3).term_input_into(&mut expected);
    term::ControlCodes::cursor_position(1, 1).term_input_into(&mut expected);
    term::control_codes().clear_attrs.term_input_into(&mut expected);

    assert_eq!(term.contents(ContentRegion::All), expected);
}

// Enabling origin mode homes the cursor, so it has to be restored before the
// cursor position, which is then relative to the top of the scroll region.
frag! {
    origin_mode_restored_before_cursor { scrollback_lines: 100, width: 5, height: 5 }
    <= term::Raw::from("11111"), term::Crlf::default(),
       term::Raw::from("22222"), term::Crlf::default(),
       term::Raw::from("33333"), term::Crlf::default(),
       term::Raw::from("44444"), term::Crlf::default(),
       term::Raw::from("55555"),
       term::ControlCodes::set_scroll_region(2, 4),
       term::control_codes().enable_scroll_region_origin_mode,
       term::ControlCodes::cursor_position(2, 3)
    => ContentRegion::All =>
            reset_codes,
            term::Raw::from("11111"),
            term::Crlf::default(),
            term::Raw::from("22222"),
            term::Crlf::default(),
            term::Raw::from("33333"),
            term::Crlf::default(),
            term::Raw::from("44444"),
            term::Crlf::default(),
            term::Raw::from("55555"),
            term::ControlCodes::set_scroll_region(2, 4),
            term::control_codes().enable_scroll_region_origin_mode,
            term::ControlCodes::cursor_position(2, 3),
            term::control_codes().clear_attrs
}

// A dump has to land correctly on a terminal in any state, so the prefix
// clears margins and origin mode before it homes the cursor and erases.
//
// Spelled out rather than built from `reset_codes` so that the helper cannot
// quietly track a regression here.
#[test]
fn dump_prefix_clears_scroll_region_and_origin_mode() {
    use shpool_vterm::term::AsTermInput;

    let mut expected = vec![];
    term::control_codes().end_link.term_input_into(&mut expected);
    term::control_codes().clear_attrs.term_input_into(&mut expected);
    term::control_codes().unset_scroll_region.term_input_into(&mut expected);
    term::control_codes().disable_scroll_region_origin_mode.term_input_into(&mut expected);
    term::ControlCodes::cursor_position(1, 1).term_input_into(&mut expected);
    term::control_codes().clear_screen.term_input_into(&mut expected);
    term::Raw::from("hi").term_input_into(&mut expected);
    term::ControlCodes::cursor_position(1, 3).term_input_into(&mut expected);
    term::control_codes().clear_attrs.term_input_into(&mut expected);

    crate::support::frag::round_trip_frag(
        b"hi",
        expected.as_slice(),
        100,
        shpool_vterm::Size { width: 5, height: 3 },
        ContentRegion::All,
    );
}

// Reflow rebuilds the scrollback out of logical lines, and a blank line is a
// logical line with nothing in it. Dropping those silently deletes rows out of
// the middle of the screen and slides everything below them up, while the
// cursor stays on the row it was already on.
//
// shpool resizes the spool on every reattach whether or not the size actually
// changed, so a resize that does nothing still has to leave the dump alone.
#[test]
fn blank_lines_survive_a_noop_resize() {
    use shpool_vterm::term::AsTermInput;

    let size = shpool_vterm::Size { width: 5, height: 8 };

    let mut input = vec![];
    term::Raw::from("aa").term_input_into(&mut input);
    term::Crlf::default().term_input_into(&mut input);
    term::Crlf::default().term_input_into(&mut input);
    term::Raw::from("bb").term_input_into(&mut input);
    term::Crlf::default().term_input_into(&mut input);
    term::Crlf::default().term_input_into(&mut input);
    term::Raw::from("cc").term_input_into(&mut input);

    let mut term = shpool_vterm::Term::new(100, size);
    term.process(input.as_slice());
    term.resize(size);

    let mut expected = vec![];
    crate::support::frag::reset_codes.term_input_into(&mut expected);
    term::Raw::from("aa").term_input_into(&mut expected);
    term::Crlf::default().term_input_into(&mut expected);
    term::Crlf::default().term_input_into(&mut expected);
    term::Raw::from("bb").term_input_into(&mut expected);
    term::Crlf::default().term_input_into(&mut expected);
    term::Crlf::default().term_input_into(&mut expected);
    term::Raw::from("cc").term_input_into(&mut expected);
    term::ControlCodes::cursor_position(5, 3).term_input_into(&mut expected);
    term::control_codes().clear_attrs.term_input_into(&mut expected);

    // Screen rather than All because that is the region shpool restores.
    assert_eq!(term.contents(ContentRegion::Screen), expected);
}

// The same, for a resize that actually reflows. The widths are picked so that
// "abcdef" occupies two grid lines either side of the resize, which keeps the
// row count stable and isolates the blank line from cursor re-anchoring.
#[test]
fn blank_lines_survive_a_reflowing_resize() {
    use shpool_vterm::term::AsTermInput;

    let mut input = vec![];
    term::Raw::from("abcdef").term_input_into(&mut input);
    term::Crlf::default().term_input_into(&mut input);
    term::Crlf::default().term_input_into(&mut input);
    term::Raw::from("gh").term_input_into(&mut input);

    let mut term = shpool_vterm::Term::new(100, shpool_vterm::Size { width: 4, height: 8 });
    term.process(input.as_slice());
    term.resize(shpool_vterm::Size { width: 3, height: 8 });

    let mut expected = vec![];
    crate::support::frag::reset_codes.term_input_into(&mut expected);
    term::Raw::from("abc").term_input_into(&mut expected);
    term::Crlf::default().term_input_into(&mut expected);
    term::Raw::from("def").term_input_into(&mut expected);
    term::Crlf::default().term_input_into(&mut expected);
    term::Crlf::default().term_input_into(&mut expected);
    term::Raw::from("gh").term_input_into(&mut expected);
    term::ControlCodes::cursor_position(4, 3).term_input_into(&mut expected);
    term::control_codes().clear_attrs.term_input_into(&mut expected);

    assert_eq!(term.contents(ContentRegion::Screen), expected);
}

// The other side of the same coin: a logical line that ends exactly on the
// width boundary fills its last grid line completely and must not pick up a
// trailing blank one. Reflow cannot simply emit a row for every logical line
// it starts.
#[test]
fn full_width_line_does_not_gain_a_blank_on_reflow() {
    use shpool_vterm::term::AsTermInput;

    let size = shpool_vterm::Size { width: 3, height: 5 };

    let mut input = vec![];
    term::Raw::from("abc").term_input_into(&mut input);
    term::Crlf::default().term_input_into(&mut input);
    term::Raw::from("d").term_input_into(&mut input);

    let mut term = shpool_vterm::Term::new(100, size);
    term.process(input.as_slice());
    term.resize(size);

    let mut expected = vec![];
    crate::support::frag::reset_codes.term_input_into(&mut expected);
    term::Raw::from("abc").term_input_into(&mut expected);
    term::Crlf::default().term_input_into(&mut expected);
    term::Raw::from("d").term_input_into(&mut expected);
    term::ControlCodes::cursor_position(2, 2).term_input_into(&mut expected);
    term::control_codes().clear_attrs.term_input_into(&mut expected);

    assert_eq!(term.contents(ContentRegion::Screen), expected);
}

// The row a line occupies is derived, not stored: it falls out of the buffer
// length and the height. `cursor.row` is an absolute row, so a resize that
// moves every line has to move the cursor with them. Clamping to the new size
// is not enough, because the cursor can stay in range while the line it was
// sitting on slides out from under it.
//
// Here the buffer is longer than the screen, so it is anchored to the bottom
// and growing the height pulls scrollback down into view. The cursor is on the
// last line and has to stay there.
#[test]
fn cursor_follows_its_row_when_the_height_grows() {
    use shpool_vterm::term::AsTermInput;

    let mut input = vec![];
    for row in ["11", "22", "33", "44"] {
        term::Raw::from(row).term_input_into(&mut input);
        term::Crlf::default().term_input_into(&mut input);
    }
    term::Raw::from("55").term_input_into(&mut input);

    let mut term = shpool_vterm::Term::new(100, shpool_vterm::Size { width: 5, height: 3 });
    term.process(input.as_slice());
    term.resize(shpool_vterm::Size { width: 5, height: 5 });

    let mut expected = vec![];
    crate::support::frag::reset_codes.term_input_into(&mut expected);
    term::Raw::from("11").term_input_into(&mut expected);
    term::Crlf::default().term_input_into(&mut expected);
    term::Raw::from("22").term_input_into(&mut expected);
    term::Crlf::default().term_input_into(&mut expected);
    term::Raw::from("33").term_input_into(&mut expected);
    term::Crlf::default().term_input_into(&mut expected);
    term::Raw::from("44").term_input_into(&mut expected);
    term::Crlf::default().term_input_into(&mut expected);
    term::Raw::from("55").term_input_into(&mut expected);
    term::ControlCodes::cursor_position(5, 3).term_input_into(&mut expected);
    term::control_codes().clear_attrs.term_input_into(&mut expected);

    assert_eq!(term.contents(ContentRegion::Screen), expected);
}

// The height is not the only input that moves lines around. Here the screen is
// anchored to the top and the height never changes, but narrowing splits
// "abcdef" across two rows, which pushes "gh" and the cursor on it down one.
#[test]
fn cursor_follows_its_row_when_reflow_adds_rows() {
    use shpool_vterm::term::AsTermInput;

    let mut input = vec![];
    term::Raw::from("abcdef").term_input_into(&mut input);
    term::Crlf::default().term_input_into(&mut input);
    term::Raw::from("gh").term_input_into(&mut input);

    let mut term = shpool_vterm::Term::new(100, shpool_vterm::Size { width: 6, height: 8 });
    term.process(input.as_slice());
    term.resize(shpool_vterm::Size { width: 3, height: 8 });

    let mut expected = vec![];
    crate::support::frag::reset_codes.term_input_into(&mut expected);
    term::Raw::from("abc").term_input_into(&mut expected);
    term::Crlf::default().term_input_into(&mut expected);
    term::Raw::from("def").term_input_into(&mut expected);
    term::Crlf::default().term_input_into(&mut expected);
    term::Raw::from("gh").term_input_into(&mut expected);
    term::ControlCodes::cursor_position(3, 3).term_input_into(&mut expected);
    term::control_codes().clear_attrs.term_input_into(&mut expected);

    assert_eq!(term.contents(ContentRegion::Screen), expected);
}

// Shrinking the height walks the bottom anchored screen the other way, and the
// clamp already lands the cursor in the right place. Pinned so that teaching
// resize to re-anchor does not overshoot the case it happens to get right.
#[test]
fn cursor_stays_on_the_bottom_row_when_the_height_shrinks() {
    use shpool_vterm::term::AsTermInput;

    let mut input = vec![];
    for row in ["11", "22", "33", "44"] {
        term::Raw::from(row).term_input_into(&mut input);
        term::Crlf::default().term_input_into(&mut input);
    }
    term::Raw::from("55").term_input_into(&mut input);

    let mut term = shpool_vterm::Term::new(100, shpool_vterm::Size { width: 5, height: 5 });
    term.process(input.as_slice());
    term.resize(shpool_vterm::Size { width: 5, height: 3 });

    let mut expected = vec![];
    crate::support::frag::reset_codes.term_input_into(&mut expected);
    term::Raw::from("33").term_input_into(&mut expected);
    term::Crlf::default().term_input_into(&mut expected);
    term::Raw::from("44").term_input_into(&mut expected);
    term::Crlf::default().term_input_into(&mut expected);
    term::Raw::from("55").term_input_into(&mut expected);
    term::ControlCodes::cursor_position(3, 3).term_input_into(&mut expected);
    term::control_codes().clear_attrs.term_input_into(&mut expected);

    assert_eq!(term.contents(ContentRegion::Screen), expected);
}

// DECSC stores a row too, so it has the same problem as the live cursor: a
// resize between the save and the restore moves the line out from under it.
// Save on "55", park the live cursor elsewhere so a fix cannot get this right
// by accident, grow the height, then restore and write.
#[test]
fn saved_cursor_follows_its_row_across_a_resize() {
    use shpool_vterm::term::AsTermInput;

    let mut input = vec![];
    for row in ["11", "22", "33", "44"] {
        term::Raw::from(row).term_input_into(&mut input);
        term::Crlf::default().term_input_into(&mut input);
    }
    term::Raw::from("55").term_input_into(&mut input);
    term::control_codes().save_cursor.term_input_into(&mut input);
    term::ControlCodes::cursor_position(1, 1).term_input_into(&mut input);

    let mut term = shpool_vterm::Term::new(100, shpool_vterm::Size { width: 5, height: 3 });
    term.process(input.as_slice());
    term.resize(shpool_vterm::Size { width: 5, height: 5 });

    let mut restore = vec![];
    term::control_codes().restore_cursor.term_input_into(&mut restore);
    term::Raw::from("X").term_input_into(&mut restore);
    term.process(restore.as_slice());

    let mut expected = vec![];
    crate::support::frag::reset_codes.term_input_into(&mut expected);
    term::Raw::from("11").term_input_into(&mut expected);
    term::Crlf::default().term_input_into(&mut expected);
    term::Raw::from("22").term_input_into(&mut expected);
    term::Crlf::default().term_input_into(&mut expected);
    term::Raw::from("33").term_input_into(&mut expected);
    term::Crlf::default().term_input_into(&mut expected);
    term::Raw::from("44").term_input_into(&mut expected);
    term::Crlf::default().term_input_into(&mut expected);
    term::Raw::from("55X").term_input_into(&mut expected);
    term::ControlCodes::cursor_position(5, 4).term_input_into(&mut expected);
    term::control_codes().clear_attrs.term_input_into(&mut expected);

    assert_eq!(term.contents(ContentRegion::Screen), expected);
}

// SU (CSI S) moves the content toward the top of the screen and opens blank
// rows at the bottom. It is what terminfo calls `indn`, and it is what a pager
// emits to advance a page. The cursor does not move, so it ends up on a
// different line than the one it started on.
//
// These assert against the screen rather than the whole buffer, so they say
// nothing about where a scrolled off line is kept. That is up to the
// implementation.
frag! {
    scroll_up_moves_content_toward_the_top { scrollback_lines: 100, width: 5, height: 3 }
    <= term::Raw::from("aa"), term::Crlf::default(),
       term::Raw::from("bb"), term::Crlf::default(),
       term::Raw::from("cc"),
       term::ControlCodes::scroll_up(1)
    => ContentRegion::Screen =>
            reset_codes,
            term::Raw::from("bb"),
            term::Crlf::default(),
            term::Raw::from("cc"),
            term::Crlf::default(),
            term::ControlCodes::cursor_position(3, 3),
            term::control_codes().clear_attrs
}

// SD (CSI T) is the mirror image: content moves toward the bottom, blank rows
// open at the top, and whatever falls off the bottom is gone. This is `rin`.
frag! {
    scroll_down_moves_content_toward_the_bottom { scrollback_lines: 100, width: 5, height: 3 }
    <= term::Raw::from("aa"), term::Crlf::default(),
       term::Raw::from("bb"), term::Crlf::default(),
       term::Raw::from("cc"),
       term::ControlCodes::scroll_down(1)
    => ContentRegion::Screen =>
            reset_codes,
            term::Crlf::default(),
            term::Raw::from("aa"),
            term::Crlf::default(),
            term::Raw::from("bb"),
            term::ControlCodes::cursor_position(3, 3),
            term::control_codes().clear_attrs
}

// SU edits the grid, so a later write has to leave it alone. This is what the
// viewport implementation got wrong: it looked right until the next printed
// character snapped the window back to the bottom of the buffer.
frag! {
    scroll_up_is_not_undone_by_the_next_write { scrollback_lines: 100, width: 5, height: 3 }
    <= term::Raw::from("aa"), term::Crlf::default(),
       term::Raw::from("bb"), term::Crlf::default(),
       term::Raw::from("cc"),
       term::ControlCodes::scroll_up(1),
       term::ControlCodes::cursor_position(1, 1),
       term::Raw::from("X")
    => ContentRegion::Screen =>
            reset_codes,
            term::Raw::from("Xb"),
            term::Crlf::default(),
            term::Raw::from("cc"),
            term::Crlf::default(),
            term::ControlCodes::cursor_position(1, 2),
            term::control_codes().clear_attrs
}

// With DECSTBM set, SU scrolls the region and nothing else. The rows above and
// below it stay put.
frag! {
    scroll_up_only_touches_the_scroll_region { scrollback_lines: 100, width: 5, height: 5 }
    <= term::Raw::from("11"), term::Crlf::default(),
       term::Raw::from("22"), term::Crlf::default(),
       term::Raw::from("33"), term::Crlf::default(),
       term::Raw::from("44"), term::Crlf::default(),
       term::Raw::from("55"),
       term::ControlCodes::set_scroll_region(2, 4),
       term::ControlCodes::cursor_position(1, 1),
       term::ControlCodes::scroll_up(1)
    => ContentRegion::All =>
            reset_codes,
            term::Raw::from("11"),
            term::Crlf::default(),
            term::Raw::from("33"),
            term::Crlf::default(),
            term::Raw::from("44"),
            term::Crlf::default(),
            term::Crlf::default(),
            term::Raw::from("55"),
            term::ControlCodes::set_scroll_region(2, 4),
            term::ControlCodes::cursor_position(1, 1),
            term::control_codes().clear_attrs
}