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
 825
 826
 827
 828
 829
 830
 831
 832
 833
 834
 835
 836
 837
 838
 839
 840
 841
 842
 843
 844
 845
 846
 847
 848
 849
 850
 851
 852
 853
 854
 855
 856
 857
 858
 859
 860
 861
 862
 863
 864
 865
 866
 867
 868
 869
 870
 871
 872
 873
 874
 875
 876
 877
 878
 879
 880
 881
 882
 883
 884
 885
 886
 887
 888
 889
 890
 891
 892
 893
 894
 895
 896
 897
 898
 899
 900
 901
 902
 903
 904
 905
 906
 907
 908
 909
 910
 911
 912
 913
 914
 915
 916
 917
 918
 919
 920
 921
 922
 923
 924
 925
 926
 927
 928
 929
 930
 931
 932
 933
 934
 935
 936
 937
 938
 939
 940
 941
 942
 943
 944
 945
 946
 947
 948
 949
 950
 951
 952
 953
 954
 955
 956
 957
 958
 959
 960
 961
 962
 963
 964
 965
 966
 967
 968
 969
 970
 971
 972
 973
 974
 975
 976
 977
 978
 979
 980
 981
 982
 983
 984
 985
 986
 987
 988
 989
 990
 991
 992
 993
 994
 995
 996
 997
 998
 999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
use std::convert::TryInto as _;
use unicode_width::UnicodeWidthChar as _;

const DEFAULT_MULTI_PARAMS: &[i64] = &[0];

#[derive(enumset::EnumSetType, Debug)]
enum Output {
    AudibleBell,
    VisualBell,
}

#[derive(enumset::EnumSetType, Debug)]
enum Mode {
    ApplicationKeypad,
    ApplicationCursor,
    HideCursor,
    AlternateScreen,
    BracketedPaste,
}

/// The xterm mouse handling mode currently in use.
#[derive(Copy, Clone, Debug, Eq, PartialEq)]
pub enum MouseProtocolMode {
    /// Mouse handling is disabled.
    None,

    /// Mouse button events should be reported on button press. Also known as
    /// X10 mouse mode.
    Press,

    /// Mouse button events should be reported on button press and release.
    /// Also known as VT200 mouse mode.
    PressRelease,

    // Highlight,
    /// Mouse button events should be reported on button press and release, as
    /// well as when the mouse moves between cells while a button is held
    /// down.
    ButtonMotion,

    /// Mouse button events should be reported on button press and release,
    /// and mouse motion events should be reported when the mouse moves
    /// between cells regardless of whether a button is held down or not.
    AnyMotion,
    // DecLocator,
}

impl Default for MouseProtocolMode {
    fn default() -> Self {
        Self::None
    }
}

/// The encoding to use for the enabled `MouseProtocolMode`.
#[derive(Copy, Clone, Debug, Eq, PartialEq)]
pub enum MouseProtocolEncoding {
    /// Default single-printable-byte encoding.
    Default,

    /// UTF-8-based encoding.
    Utf8,

    /// SGR-like encoding.
    Sgr,
    // Urxvt,
}

impl Default for MouseProtocolEncoding {
    fn default() -> Self {
        Self::Default
    }
}

/// Represents the overall terminal state.
#[derive(Clone, Debug)]
pub struct Screen {
    grid: crate::grid::Grid,
    alternate_grid: crate::grid::Grid,

    attrs: crate::attrs::Attrs,
    saved_attrs: crate::attrs::Attrs,

    title: String,
    icon_name: String,

    outputs: enumset::EnumSet<Output>,
    modes: enumset::EnumSet<Mode>,
    mouse_protocol_mode: MouseProtocolMode,
    mouse_protocol_encoding: MouseProtocolEncoding,
}

impl Screen {
    pub(crate) fn new(size: crate::grid::Size) -> Self {
        Self {
            grid: crate::grid::Grid::new(size),
            alternate_grid: crate::grid::Grid::new(size),

            attrs: crate::attrs::Attrs::default(),
            saved_attrs: crate::attrs::Attrs::default(),

            title: String::default(),
            icon_name: String::default(),

            outputs: enumset::EnumSet::default(),
            modes: enumset::EnumSet::default(),
            mouse_protocol_mode: MouseProtocolMode::default(),
            mouse_protocol_encoding: MouseProtocolEncoding::default(),
        }
    }

    /// Resizes the terminal.
    pub fn set_size(&mut self, rows: u16, cols: u16) {
        self.grid.set_size(crate::grid::Size { rows, cols });
        self.alternate_grid
            .set_size(crate::grid::Size { rows, cols });
    }

    /// Returns the current size of the terminal.
    ///
    /// The return value will be (rows, cols).
    pub fn size(&self) -> (u16, u16) {
        let size = self.grid().size();
        (size.rows, size.cols)
    }

    /// Returns the text contents of the terminal.
    ///
    /// This will not include any formatting information, and will be in plain
    /// text format.
    pub fn contents(&self) -> String {
        self.grid().contents()
    }

    /// Returns the text contents of the terminal by row, restricted to the
    /// given subset of columns.
    ///
    /// This will not include any formatting information, and will be in plain
    /// text format.
    ///
    /// Newlines will not be included.
    pub fn rows(
        &self,
        start: u16,
        width: u16,
    ) -> impl Iterator<Item = String> + '_ {
        self.grid()
            .rows()
            .map(move |row| row.contents(start, width))
    }

    /// Returns the formatted contents of the terminal.
    ///
    /// Formatting information will be included inline as terminal escape
    /// codes. The result will be suitable for feeding directly to a raw
    /// terminal parser, and will result in the same visual output. Internal
    /// terminal modes (such as application keypad mode or alternate screen
    /// mode) will not be included here, but modes that affect the visible
    /// output (such as hidden cursor mode) will.
    pub fn contents_formatted(&self) -> Vec<u8> {
        let mut grid_contents = if self.hide_cursor() {
            b"\x1b[?25l"
        } else {
            b"\x1b[?25h"
        }
        .to_vec();
        grid_contents.append(&mut self.grid().contents_formatted());
        grid_contents
    }

    /// Returns the formatted contents of the terminal by row, restricted to
    /// the given subset of columns.
    ///
    /// Formatting information will be included inline as terminal escape
    /// codes. The result will be suitable for feeding directly to a raw
    /// terminal parser, and will result in the same visual output. Internal
    /// terminal modes (such as application keypad mode or alternate screen
    /// mode) will not be included here.
    ///
    /// CRLF at the end of lines will not be included.
    pub fn rows_formatted(
        &self,
        start: u16,
        width: u16,
    ) -> impl Iterator<Item = Vec<u8>> + '_ {
        self.grid().rows().map(move |row| {
            let (contents, ..) = row.contents_formatted(
                start,
                width,
                crate::attrs::Attrs::default(),
            );
            contents
        })
    }

    /// Returns a terminal byte stream sufficient to turn the screen described
    /// by `prev` into the screen described by `self`.
    ///
    /// The result of rendering `prev.contents_formatted()` followed by
    /// `self.contents_diff(prev)` should be equivalent to the result of
    /// rendering `self.contents_formatted()`. This is primarily useful when
    /// you already have a terminal parser whose state is described by `prev`,
    /// since the diff will likely require less memory and cause less
    /// flickering than redrawing the entire screen contents.
    pub fn contents_diff(&self, prev: &Self) -> Vec<u8> {
        let mut grid_contents = vec![];
        if self.hide_cursor() != prev.hide_cursor() {
            grid_contents.extend(if self.hide_cursor() {
                b"\x1b[?25l"
            } else {
                b"\x1b[?25h"
            });
        }
        grid_contents.append(&mut self.grid().contents_diff(prev.grid()));
        grid_contents
    }

    /// Returns a sequence of terminal byte streams sufficient to turn the
    /// subset of each row from `prev` (as described by `start` and `width`)
    /// into the corresponding row subset in `self`.
    ///
    /// You must handle the initial row positioning yourself - each row diff
    /// expects to start out positioned at the start of that row. Internal
    /// terminal modes (such as application keypad mode or alternate screen
    /// mode) will not be included here.
    pub fn rows_diff<'a>(
        &'a self,
        prev: &'a Self,
        start: u16,
        width: u16,
    ) -> impl Iterator<Item = Vec<u8>> + 'a {
        self.grid().rows().zip(prev.grid().rows()).map(
            move |(row, prev_row)| {
                let (contents, ..) = row.contents_diff(
                    prev_row,
                    start,
                    width,
                    crate::attrs::Attrs::default(),
                );
                contents
            },
        )
    }

    /// Returns the `Cell` object at the given location in the terminal, if it
    /// exists.
    pub fn cell(&self, row: u16, col: u16) -> Option<&crate::cell::Cell> {
        self.grid().cell(crate::grid::Pos { row, col })
    }

    /// Returns the current cursor position of the terminal.
    ///
    /// The return value will be (row, col).
    pub fn cursor_position(&self) -> (u16, u16) {
        let pos = self.grid().pos();
        (pos.row, pos.col)
    }

    /// Returns the terminal's window title.
    pub fn title(&self) -> &str {
        &self.title
    }

    /// Returns the terminal's icon name.
    pub fn icon_name(&self) -> &str {
        &self.icon_name
    }

    /// Returns whether an audible bell has occurred since the last time this
    /// method was called.
    pub fn check_audible_bell(&mut self) -> bool {
        self.check_output(Output::AudibleBell)
    }

    /// Returns whether an visual bell has occurred since the last time this
    /// method was called.
    pub fn check_visual_bell(&mut self) -> bool {
        self.check_output(Output::VisualBell)
    }

    /// Returns whether the terminal should be in application keypad mode.
    pub fn application_keypad(&self) -> bool {
        self.mode(Mode::ApplicationKeypad)
    }

    /// Returns whether the terminal should be in application cursor mode.
    pub fn application_cursor(&self) -> bool {
        self.mode(Mode::ApplicationCursor)
    }

    /// Returns whether the terminal should be in hide cursor mode.
    pub fn hide_cursor(&self) -> bool {
        self.mode(Mode::HideCursor)
    }

    /// Returns whether the terminal should be in bracketed paste mode.
    pub fn bracketed_paste(&self) -> bool {
        self.mode(Mode::BracketedPaste)
    }

    /// Returns the currently active `MouseProtocolMode`
    pub fn mouse_protocol_mode(&self) -> MouseProtocolMode {
        self.mouse_protocol_mode
    }

    /// Returns the currently active `MouseProtocolEncoding`
    pub fn mouse_protocol_encoding(&self) -> MouseProtocolEncoding {
        self.mouse_protocol_encoding
    }

    fn grid(&self) -> &crate::grid::Grid {
        if self.mode(Mode::AlternateScreen) {
            &self.alternate_grid
        } else {
            &self.grid
        }
    }

    fn grid_mut(&mut self) -> &mut crate::grid::Grid {
        if self.mode(Mode::AlternateScreen) {
            &mut self.alternate_grid
        } else {
            &mut self.grid
        }
    }

    fn row(&self, pos: crate::grid::Pos) -> Option<&crate::row::Row> {
        self.grid().row(pos)
    }

    fn cell_mut(
        &mut self,
        pos: crate::grid::Pos,
    ) -> Option<&mut crate::cell::Cell> {
        self.grid_mut().cell_mut(pos)
    }

    fn current_cell_mut(&mut self) -> &mut crate::cell::Cell {
        self.grid_mut().current_cell_mut()
    }

    fn enter_alternate_grid(&mut self) {
        self.set_mode(Mode::AlternateScreen);
    }

    fn exit_alternate_grid(&mut self) {
        self.clear_mode(Mode::AlternateScreen);
    }

    fn save_cursor(&mut self) {
        self.grid_mut().save_cursor();
        self.saved_attrs = self.attrs;
    }

    fn restore_cursor(&mut self) {
        self.grid_mut().restore_cursor();
        self.attrs = self.saved_attrs;
    }

    fn set_output(&mut self, output: Output) {
        self.outputs.insert(output);
    }

    fn clear_output(&mut self, output: Output) {
        self.outputs.remove(output);
    }

    fn check_output(&mut self, output: Output) -> bool {
        let ret = self.outputs.contains(output);
        self.clear_output(output);
        ret
    }

    fn set_mode(&mut self, mode: Mode) {
        self.modes.insert(mode);
    }

    fn clear_mode(&mut self, mode: Mode) {
        self.modes.remove(mode);
    }

    fn mode(&self, mode: Mode) -> bool {
        self.modes.contains(mode)
    }

    fn set_mouse_mode(&mut self, mode: MouseProtocolMode) {
        self.mouse_protocol_mode = mode;
    }

    fn clear_mouse_mode(&mut self, mode: MouseProtocolMode) {
        if self.mouse_protocol_mode == mode {
            self.mouse_protocol_mode = MouseProtocolMode::default();
        }
    }

    fn set_mouse_encoding(&mut self, encoding: MouseProtocolEncoding) {
        self.mouse_protocol_encoding = encoding;
    }

    fn clear_mouse_encoding(&mut self, encoding: MouseProtocolEncoding) {
        if self.mouse_protocol_encoding == encoding {
            self.mouse_protocol_encoding = MouseProtocolEncoding::default();
        }
    }
}

impl Screen {
    fn text(&mut self, c: char) {
        let pos = self.grid().pos();
        if pos.col > 0 {
            let bgcolor = self.attrs.bgcolor;
            let prev_cell = self
                .cell_mut(crate::grid::Pos {
                    row: pos.row,
                    col: pos.col - 1,
                })
                .unwrap();
            if prev_cell.is_wide() {
                prev_cell.clear(bgcolor);
            }
        }

        let width = c.width().unwrap_or(0).try_into().unwrap();
        let attrs = self.attrs;

        self.grid_mut().col_wrap(width);
        let cell = self.current_cell_mut();

        if width == 0 {
            if pos.col > 0 {
                let prev_cell = self
                    .cell_mut(crate::grid::Pos {
                        row: pos.row,
                        col: pos.col - 1,
                    })
                    .unwrap();
                prev_cell.append(c);
            } else if pos.row > 0 {
                let prev_row = self
                    .row(crate::grid::Pos {
                        row: pos.row - 1,
                        col: 0,
                    })
                    .unwrap();
                if prev_row.wrapped() {
                    let prev_cell = self
                        .cell_mut(crate::grid::Pos {
                            row: pos.row - 1,
                            col: self.grid().size().cols - 1,
                        })
                        .unwrap();
                    prev_cell.append(c);
                }
            }
        } else {
            cell.set(c.to_string(), attrs);
            self.grid_mut().col_inc(1);
            if width > 1 {
                let bgcolor = self.attrs.bgcolor;
                let next_cell = self.current_cell_mut();
                next_cell.clear(bgcolor);
                self.grid_mut().col_inc(1);
            }
        }
    }

    // control codes

    fn bel(&mut self) {
        self.set_output(Output::AudibleBell);
    }

    fn bs(&mut self) {
        // XXX is this correct? is backwards wrapping a thing?
        self.grid_mut().col_dec(1);
    }

    fn tab(&mut self) {
        self.grid_mut().col_tab();
    }

    fn lf(&mut self) {
        self.grid_mut().row_inc_scroll(1);
    }

    fn vt(&mut self) {
        self.lf();
    }

    fn ff(&mut self) {
        self.lf();
    }

    fn cr(&mut self) {
        self.grid_mut().col_set(0);
    }

    // escape codes

    // ESC 7
    fn decsc(&mut self) {
        self.save_cursor();
    }

    // ESC 8
    fn decrc(&mut self) {
        self.restore_cursor();
    }

    // ESC =
    fn deckpam(&mut self) {
        self.set_mode(Mode::ApplicationKeypad);
    }

    // ESC >
    fn deckpnm(&mut self) {
        self.clear_mode(Mode::ApplicationKeypad);
    }

    // ESC M
    fn ri(&mut self) {
        self.grid_mut().row_dec_scroll(1);
    }

    // ESC c
    fn ris(&mut self) {
        let outputs = self.outputs;
        let title = self.title.clone();
        let icon_name = self.icon_name.clone();

        *self = Self::new(self.grid().size());

        self.outputs = outputs;
        self.title = title;
        self.icon_name = icon_name;
    }

    // ESC g
    fn vb(&mut self) {
        self.set_output(Output::VisualBell);
    }

    // csi codes

    // CSI @
    fn ich(&mut self, count: u16) {
        self.grid_mut().insert_cells(count);
    }

    // CSI A
    fn cuu(&mut self, offset: u16) {
        self.grid_mut().row_dec_clamp(offset);
    }

    // CSI B
    fn cud(&mut self, offset: u16) {
        self.grid_mut().row_inc_clamp(offset);
    }

    // CSI C
    fn cuf(&mut self, offset: u16) {
        self.grid_mut().col_inc_clamp(offset);
    }

    // CSI D
    fn cub(&mut self, offset: u16) {
        self.grid_mut().col_dec(offset);
    }

    // CSI G
    fn cha(&mut self, col: u16) {
        self.grid_mut().col_set(col - 1);
    }

    // CSI H
    fn cup(&mut self, (row, col): (u16, u16)) {
        self.grid_mut().set_pos(crate::grid::Pos {
            row: row - 1,
            col: col - 1,
        });
    }

    // CSI J
    fn ed(&mut self, mode: u16) {
        let bgcolor = self.attrs.bgcolor;
        match mode {
            0 => self.grid_mut().erase_all_forward(bgcolor),
            1 => self.grid_mut().erase_all_backward(bgcolor),
            2 => self.grid_mut().erase_all(bgcolor),
            _ => {}
        }
    }

    // CSI ? J
    fn decsed(&mut self, mode: u16) {
        self.ed(mode);
    }

    // CSI K
    fn el(&mut self, mode: u16) {
        let bgcolor = self.attrs.bgcolor;
        match mode {
            0 => self.grid_mut().erase_row_forward(bgcolor),
            1 => self.grid_mut().erase_row_backward(bgcolor),
            2 => self.grid_mut().erase_row(bgcolor),
            _ => {}
        }
    }

    // CSI ? K
    fn decsel(&mut self, mode: u16) {
        self.el(mode);
    }

    // CSI L
    fn il(&mut self, count: u16) {
        self.grid_mut().insert_lines(count);
    }

    // CSI M
    fn dl(&mut self, count: u16) {
        self.grid_mut().delete_lines(count);
    }

    // CSI P
    fn dch(&mut self, count: u16) {
        self.grid_mut().delete_cells(count);
    }

    // CSI S
    fn su(&mut self, count: u16) {
        self.grid_mut().scroll_up(count);
    }

    // CSI T
    fn sd(&mut self, count: u16) {
        self.grid_mut().scroll_down(count);
    }

    // CSI X
    fn ech(&mut self, count: u16) {
        let bgcolor = self.attrs.bgcolor;
        self.grid_mut().erase_cells(count, bgcolor);
    }

    // CSI d
    fn vpa(&mut self, row: u16) {
        self.grid_mut().row_set(row - 1);
    }

    // CSI h
    fn sm(&mut self, _params: &[i64]) {
        // nothing, i think?
    }

    // CSI ? h
    fn decset(&mut self, params: &[i64]) {
        for param in params {
            match param {
                1 => self.set_mode(Mode::ApplicationCursor),
                6 => self.grid_mut().set_origin_mode(true),
                9 => self.set_mouse_mode(MouseProtocolMode::Press),
                25 => self.clear_mode(Mode::HideCursor),
                47 => self.enter_alternate_grid(),
                1000 => self.set_mouse_mode(MouseProtocolMode::PressRelease),
                1002 => self.set_mouse_mode(MouseProtocolMode::ButtonMotion),
                1003 => self.set_mouse_mode(MouseProtocolMode::AnyMotion),
                1005 => self.set_mouse_encoding(MouseProtocolEncoding::Utf8),
                1006 => self.set_mouse_encoding(MouseProtocolEncoding::Sgr),
                1049 => {
                    self.decsc();
                    self.alternate_grid.clear();
                    self.enter_alternate_grid();
                }
                2004 => self.set_mode(Mode::BracketedPaste),
                _ => {}
            }
        }
    }

    // CSI l
    fn rm(&mut self, _params: &[i64]) {
        // nothing, i think?
    }

    // CSI ? l
    fn decrst(&mut self, params: &[i64]) {
        for param in params {
            match param {
                1 => self.clear_mode(Mode::ApplicationCursor),
                6 => self.grid_mut().set_origin_mode(false),
                9 => self.clear_mouse_mode(MouseProtocolMode::Press),
                25 => self.set_mode(Mode::HideCursor),
                47 => {
                    self.exit_alternate_grid();
                }
                1000 => {
                    self.clear_mouse_mode(MouseProtocolMode::PressRelease)
                }
                1002 => {
                    self.clear_mouse_mode(MouseProtocolMode::ButtonMotion)
                }
                1003 => self.clear_mouse_mode(MouseProtocolMode::AnyMotion),
                1005 => {
                    self.clear_mouse_encoding(MouseProtocolEncoding::Utf8)
                }
                1006 => self.clear_mouse_encoding(MouseProtocolEncoding::Sgr),
                1049 => {
                    self.exit_alternate_grid();
                    self.decrc();
                }
                2004 => self.clear_mode(Mode::BracketedPaste),
                _ => {}
            }
        }
    }

    // CSI m
    fn sgr(&mut self, params: &[i64]) {
        let mut i = 0;

        macro_rules! next_param {
            () => {
                if i >= params.len() {
                    return;
                } else if let Some(n) = i64_to_u8(params[i]) {
                    i += 1;
                    n
                } else {
                    return;
                }
            };
        }

        loop {
            match next_param!() {
                0 => self.attrs = crate::attrs::Attrs::default(),
                1 => self.attrs.set_bold(true),
                3 => self.attrs.set_italic(true),
                4 => self.attrs.set_underline(true),
                7 => self.attrs.set_inverse(true),
                22 => self.attrs.set_bold(false),
                23 => self.attrs.set_italic(false),
                24 => self.attrs.set_underline(false),
                27 => self.attrs.set_inverse(false),
                n if n >= 30 && n <= 37 => {
                    self.attrs.fgcolor = crate::attrs::Color::Idx(n - 30);
                }
                38 => match next_param!() {
                    2 => {
                        let r = next_param!();
                        let g = next_param!();
                        let b = next_param!();
                        self.attrs.fgcolor =
                            crate::attrs::Color::Rgb(r, g, b);
                    }
                    5 => {
                        self.attrs.fgcolor =
                            crate::attrs::Color::Idx(next_param!());
                    }
                    _ => {}
                },
                39 => {
                    self.attrs.fgcolor = crate::attrs::Color::Default;
                }
                n if n >= 40 && n <= 47 => {
                    self.attrs.bgcolor = crate::attrs::Color::Idx(n - 40);
                }
                48 => match next_param!() {
                    2 => {
                        let r = next_param!();
                        let g = next_param!();
                        let b = next_param!();
                        self.attrs.bgcolor =
                            crate::attrs::Color::Rgb(r, g, b);
                    }
                    5 => {
                        self.attrs.bgcolor =
                            crate::attrs::Color::Idx(next_param!());
                    }
                    _ => {}
                },
                49 => {
                    self.attrs.bgcolor = crate::attrs::Color::Default;
                }
                n if n >= 90 && n <= 97 => {
                    self.attrs.fgcolor = crate::attrs::Color::Idx(n - 82);
                }
                n if n >= 100 && n <= 107 => {
                    self.attrs.bgcolor = crate::attrs::Color::Idx(n - 92);
                }
                _ => {}
            }
        }
    }

    // CSI r
    fn decstbm(&mut self, (top, bottom): (u16, u16)) {
        self.grid_mut().set_scroll_region(top - 1, bottom - 1);
    }

    // osc codes

    fn osc0(&mut self, s: &[u8]) {
        self.osc1(s);
        self.osc2(s);
    }

    fn osc1(&mut self, s: &[u8]) {
        if let Ok(s) = std::str::from_utf8(s) {
            self.icon_name = s.to_string();
        }
    }

    fn osc2(&mut self, s: &[u8]) {
        if let Ok(s) = std::str::from_utf8(s) {
            self.title = s.to_string();
        }
    }
}

impl vte::Perform for Screen {
    fn print(&mut self, c: char) {
        self.text(c)
    }

    fn execute(&mut self, b: u8) {
        match b {
            7 => self.bel(),
            8 => self.bs(),
            9 => self.tab(),
            10 => self.lf(),
            11 => self.vt(),
            12 => self.ff(),
            13 => self.cr(),
            _ => {
                log::warn!("unhandled control character: {}", b);
            }
        }
    }

    fn esc_dispatch(
        &mut self,
        _params: &[i64],
        intermediates: &[u8],
        _ignore: bool,
        b: u8,
    ) {
        match intermediates.get(0) {
            None => match b {
                b'7' => self.decsc(),
                b'8' => self.decrc(),
                b'=' => self.deckpam(),
                b'>' => self.deckpnm(),
                b'M' => self.ri(),
                b'c' => self.ris(),
                b'g' => self.vb(),
                _ => {
                    log::warn!("unhandled escape code: ESC {}", b);
                }
            },
            Some(i) => {
                log::warn!("unhandled escape code: ESC {} {}", i, b);
            }
        }
    }

    fn csi_dispatch(
        &mut self,
        params: &[i64],
        intermediates: &[u8],
        _ignore: bool,
        c: char,
    ) {
        match intermediates.get(0) {
            None => match c {
                '@' => self.ich(canonicalize_params_1(params, 1)),
                'A' => self.cuu(canonicalize_params_1(params, 1)),
                'B' => self.cud(canonicalize_params_1(params, 1)),
                'C' => self.cuf(canonicalize_params_1(params, 1)),
                'D' => self.cub(canonicalize_params_1(params, 1)),
                'G' => self.cha(canonicalize_params_1(params, 1)),
                'H' => self.cup(canonicalize_params_2(params, 1, 1)),
                'J' => self.ed(canonicalize_params_1(params, 0)),
                'K' => self.el(canonicalize_params_1(params, 0)),
                'L' => self.il(canonicalize_params_1(params, 1)),
                'M' => self.dl(canonicalize_params_1(params, 1)),
                'P' => self.dch(canonicalize_params_1(params, 1)),
                'S' => self.su(canonicalize_params_1(params, 1)),
                'T' => self.sd(canonicalize_params_1(params, 1)),
                'X' => self.ech(canonicalize_params_1(params, 1)),
                'd' => self.vpa(canonicalize_params_1(params, 1)),
                'h' => self.sm(canonicalize_params_multi(params)),
                'l' => self.rm(canonicalize_params_multi(params)),
                'm' => self.sgr(canonicalize_params_multi(params)),
                'r' => self.decstbm(canonicalize_params_decstbm(
                    params,
                    self.grid().size(),
                )),
                _ => {
                    if log::log_enabled!(log::Level::Warn) {
                        log::warn!(
                            "unhandled csi sequence: CSI {} {}",
                            param_str(params),
                            c
                        )
                    }
                }
            },
            Some(b'?') => match c {
                'J' => self.decsed(canonicalize_params_1(params, 0)),
                'K' => self.decsel(canonicalize_params_1(params, 0)),
                'h' => self.decset(canonicalize_params_multi(params)),
                'l' => self.decrst(canonicalize_params_multi(params)),
                _ => {
                    if log::log_enabled!(log::Level::Warn) {
                        log::warn!(
                            "unhandled csi sequence: CSI ? {} {}",
                            param_str(params),
                            c
                        )
                    }
                }
            },
            Some(i) => {
                if log::log_enabled!(log::Level::Warn) {
                    log::warn!(
                        "unhandled csi sequence: CSI {} {} {}",
                        i,
                        param_str(params),
                        c
                    )
                }
            }
        }
    }

    fn osc_dispatch(&mut self, params: &[&[u8]]) {
        match (params.get(0), params.get(1)) {
            (Some(&b"0"), Some(s)) => self.osc0(s),
            (Some(&b"1"), Some(s)) => self.osc1(s),
            (Some(&b"2"), Some(s)) => self.osc2(s),
            _ => {
                if log::log_enabled!(log::Level::Warn) {
                    log::warn!(
                        "unhandled osc sequence: OSC {}",
                        osc_param_str(params),
                    )
                }
            }
        }
    }

    fn hook(&mut self, params: &[i64], intermediates: &[u8], _ignore: bool) {
        if log::log_enabled!(log::Level::Warn) {
            // TODO: include the final byte here (it seems to be a bug that
            // the vte parser doesn't currently pass it to this method)
            match intermediates.get(0) {
                None => log::warn!(
                    "unhandled dcs sequence: DCS {}",
                    param_str(params),
                ),
                Some(i) => log::warn!(
                    "unhandled dcs sequence: DCS {} {}",
                    i,
                    param_str(params),
                ),
            }
        }
    }
    fn put(&mut self, _: u8) {}
    fn unhook(&mut self) {}
}

fn canonicalize_params_1(params: &[i64], default: u16) -> u16 {
    let first = params.get(0).copied().unwrap_or(0);
    if first == 0 {
        default
    } else {
        i64_to_u16(first)
    }
}

fn canonicalize_params_2(
    params: &[i64],
    default1: u16,
    default2: u16,
) -> (u16, u16) {
    let first = params.get(0).copied().unwrap_or(0);
    let first = if first == 0 {
        default1
    } else {
        i64_to_u16(first)
    };

    let second = params.get(1).copied().unwrap_or(0);
    let second = if second == 0 {
        default2
    } else {
        i64_to_u16(second)
    };

    (first, second)
}

fn canonicalize_params_multi(params: &[i64]) -> &[i64] {
    if params.is_empty() {
        DEFAULT_MULTI_PARAMS
    } else {
        params
    }
}

fn canonicalize_params_decstbm(
    params: &[i64],
    size: crate::grid::Size,
) -> (u16, u16) {
    let top = params.get(0).copied().unwrap_or(0);
    let top = if top == 0 { 1 } else { i64_to_u16(top) };

    let bottom = params.get(1).copied().unwrap_or(0);
    let bottom = if bottom == 0 {
        size.rows
    } else {
        i64_to_u16(bottom)
    };

    (top, bottom)
}

fn i64_to_u16(i: i64) -> u16 {
    if i < 0 {
        0
    } else if i > i64::from(u16::max_value()) {
        u16::max_value()
    } else {
        i.try_into().unwrap()
    }
}

fn i64_to_u8(i: i64) -> Option<u8> {
    if i < 0 || i > i64::from(u8::max_value()) {
        None
    } else {
        Some(i.try_into().unwrap())
    }
}

fn param_str(params: &[i64]) -> String {
    let strs: Vec<_> = params
        .iter()
        .map(std::string::ToString::to_string)
        .collect();
    strs.join(" ; ")
}

fn osc_param_str(params: &[&[u8]]) -> String {
    let strs: Vec<_> = params
        .iter()
        .map(|b| format!("\"{}\"", std::string::String::from_utf8_lossy(*b)))
        .collect();
    strs.join(" ; ")
}