aboutsummaryrefslogtreecommitdiffstats
path: root/src/panes/scroll.rs
blob: 98d1b6f49914d0c4a3980ce9d5abbe5027ed3129 (plain) (blame)
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
use std::collections::VecDeque;
use std::{
    cmp::max,
    fmt::{self, Debug, Formatter},
};

use crate::panes::terminal_character::{
    CharacterStyles, TerminalCharacter, EMPTY_TERMINAL_CHARACTER,
};

/*
 * Scroll
 *
 * holds the terminal buffer and controls the viewport (which part of it we see)
 * its functions include line-wrapping and tracking the location of the cursor
 *
 */

/*
 * CanonicalLine vs. WrappedFragment
 *
 * If the terminal had infinite width and we did not need to line wrap, the CanonicalLine would
 * be our only unit of line separation.
 * Each CanonicalLine has one or more WrappedFragments, which are re-calculated when the terminal is
 * resized, or when characters are added to the line
 *
 */

#[derive(Clone)]
pub struct CanonicalLine {
    pub wrapped_fragments: Vec<WrappedFragment>,
}

impl CanonicalLine {
    pub fn new() -> Self {
        CanonicalLine {
            wrapped_fragments: vec![WrappedFragment::new()],
        }
    }
    pub fn add_new_wrap(&mut self, terminal_character: TerminalCharacter) {
        let mut new_fragment = WrappedFragment::new();
        new_fragment.add_character(terminal_character, 0);
        self.wrapped_fragments.push(new_fragment);
    }
    pub fn change_width(&mut self, new_width: usize) {
        let characters = self.flattened_characters();
        let wrapped_fragments =
            CanonicalLine::fill_fragments_up_to_width(characters, new_width, None);
        self.wrapped_fragments = wrapped_fragments;
    }
    pub fn clear_after(&mut self, fragment_index: usize, column_index: usize) {
        let fragment_to_clear = self
            .wrapped_fragments
            .get_mut(fragment_index)
            .expect("fragment out of bounds");
        fragment_to_clear.clear_after_and_including(column_index);
        self.wrapped_fragments.truncate(fragment_index + 1);
    }
    pub fn replace_with_empty_chars(
        &mut self,
        fragment_index: usize,
        from_col: usize,
        count: usize,
        style_of_empty_space: CharacterStyles,
    ) {
        let mut characters_replaced = 0;
        let mut column_position_in_fragment = from_col;
        let mut current_fragment = fragment_index;
        let mut empty_space_character = EMPTY_TERMINAL_CHARACTER;
        empty_space_character.styles = style_of_empty_space;
        loop {
            if characters_replaced == count {
                break;
            }
            match self.wrapped_fragments.get_mut(current_fragment) {
                Some(fragment_to_clear) => {
                    let fragment_characters_count = fragment_to_clear.characters.len();
                    if fragment_characters_count >= column_position_in_fragment {
                        fragment_to_clear
                            .add_character(empty_space_character, column_position_in_fragment);
                        column_position_in_fragment += 1;
                        characters_replaced += 1;
                    } else {
                        current_fragment += 1;
                        column_position_in_fragment = 0;
                    }
                }
                None => {
                    // end of line, nothing more to clear
                    break;
                }
            }
        }
    }
    pub fn erase_chars(
        &mut self,
        fragment_index: usize,
        from_col: usize,
        count: usize,
        style_of_empty_space: CharacterStyles,
    ) {
        let mut empty_character = EMPTY_TERMINAL_CHARACTER;
        empty_character.styles = style_of_empty_space;
        let current_width = self.wrapped_fragments.get(0).unwrap().characters.len();
        let mut characters = self.flattened_characters();
        let absolute_position_of_character = fragment_index * current_width + from_col;
        for _ in 0..count {
            characters.remove(absolute_position_of_character);
        }
        let wrapped_fragments = CanonicalLine::fill_fragments_up_to_width(
            characters,
            current_width,
            Some(empty_character),
        );
        self.wrapped_fragments = wrapped_fragments;
    }
    pub fn replace_with_empty_chars_after_cursor(
        &mut self,
        fragment_index: usize,
        from_col: usize,
        total_columns: usize,
        style_of_empty_space: CharacterStyles,
    ) {
        let mut empty_char_character = EMPTY_TERMINAL_CHARACTER;
        empty_char_character.styles = style_of_empty_space;
        let current_fragment = self.wrapped_fragments.get_mut(fragment_index).unwrap();
        let fragment_characters_count = current_fragment.characters.len();

        for i in from_col..fragment_characters_count {
            current_fragment.add_character(empty_char_character, i);
        }

        for i in fragment_characters_count..total_columns {
            current_fragment.add_character(empty_char_character, i);
        }

        self.wrapped_fragments.truncate(fragment_index + 1);
    }
    pub fn replace_with_empty_chars_before_cursor(
        &mut self,
        fragment_index: usize,
        until_col: usize,
        style_of_empty_space: CharacterStyles,
    ) {
        let mut empty_char_character = EMPTY_TERMINAL_CHARACTER;
        empty_char_character.styles = style_of_empty_space;

        if fragment_index > 0 {
            for i in 0..(fragment_index - 1) {
                let fragment = self.wrapped_fragments.get_mut(i).unwrap();
                for i in 0..fragment.characters.len() {
                    fragment.add_character(empty_char_character, i);
                }
            }
        }
        let current_fragment = self.wrapped_fragments.get_mut(fragment_index).unwrap();
        for i in 0..until_col {
            current_fragment.add_character(empty_char_character, i);
        }
    }
    fn flattened_characters(&self) -> Vec<TerminalCharacter> {
        self.wrapped_fragments
            .iter()
            .fold(
                Vec::with_capacity(self.wrapped_fragments.len()),
                |mut characters, wrapped_fragment| {
                    characters.push(wrapped_fragment.characters.iter().copied());
                    characters
                },
            )
            .into_iter()
            .flatten()
            .collect()
    }
    fn fill_fragments_up_to_width(
        mut characters: Vec<TerminalCharacter>,
        width: usize,
        padding: Option<TerminalCharacter>,
    ) -> Vec<WrappedFragment> {
        let mut wrapped_fragments = vec![];
        while !characters.is_empty() {
            if characters.len() > width {
                wrapped_fragments.push(WrappedFragment::from_vec(
                    characters.drain(..width).collect(),
                ));
            } else {
                let mut last_fragment = WrappedFragment::from_vec(characters.drain(..).collect());
                let last_fragment_len = last_fragment.characters.len();
                if let Some(empty_char_character) = padding {
                    if last_fragment_len < width {
                        for _ in last_fragment_len..width {
                            last_fragment.characters.push(empty_char_character);
                        }
                    }
                }
                wrapped_fragments.push(last_fragment);
            }
        }
        if wrapped_fragments.is_empty() {
            wrapped_fragments.push(WrappedFragment::new());
        }
        wrapped_fragments
    }
}

impl Debug for CanonicalLine {
    fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
        for wrapped_fragment in &self.wrapped_fragments {
            writeln!(f, "{:?}", wrapped_fragment)?;
        }
        Ok(())
    }
}

#[derive(Clone)]
pub struct WrappedFragment {
    pub characters: Vec<TerminalCharacter>,
}

impl WrappedFragment {
    pub fn new() -> Self {
        WrappedFragment { characters: vec![] }
    }
    pub fn add_character(
        &mut self,
        terminal_character: TerminalCharacter,
        position_in_line: usize,
    ) {
        if position_in_line == self.characters.len() {
            self.characters.push(terminal_character);
        } else {
            // this is much more performant than remove/insert
            self.characters.push(terminal_character);
            self.characters.swap_remove(position_in_line);
        }
    }
    pub fn from_vec(characters: Vec<TerminalCharacter>) -> Self {
        WrappedFragment { characters }
    }
    pub fn clear_after_and_including(&mut self, character_index: usize) {
        self.characters.truncate(character_index);
    }
}

impl Debug for WrappedFragment {
    fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
        for character in &self.characters {
            write!(f, "{:?}", character)?;
        }
        Ok(())
    }
}

#[derive(Debug, Clone, Copy)]
pub struct CursorPosition {
    line_index: (usize, usize), // (canonical line index, fragment index in line)
    column_index: usize,        // 0 is the first character from the pane edge
}

impl CursorPosition {
    pub fn new() -> Self {
        CursorPosition {
            line_index: (0, 0),
            column_index: 0,
        }
    }
    pub fn move_forward(&mut self, count: usize) {
        // TODO: panic if out of bounds?
        self.column_index += count;
    }
    pub fn move_backwards(&mut self, count: usize) {
        self.column_index = u32::overflowing_sub(self.column_index as u32, count as u32).0 as usize;
    }
    pub fn move_to_next_linewrap(&mut self) {
        self.line_index.1 += 1;
    }
    pub fn move_to_next_canonical_line(&mut self) {
        self.line_index.0 += 1;
    }
    pub fn _move_to_prev_canonical_line(&mut self) {
        self.line_index.0 -= 1;
    }
    pub fn move_to_beginning_of_linewrap(&mut self) {
        self.column_index = 0;
    }
    pub fn move_to_beginning_of_canonical_line(&mut self) {
        self.column_index = 0;
        self.line_index.1 = 0;
    }
    pub fn move_down_by_canonical_lines(&mut self, count: usize) {
        // this method does not verify that we will not overflow
        let current_canonical_line_position = self.line_index.0;
        self.line_index = (current_canonical_line_position + count, 0);
    }
    pub fn move_to_canonical_line(&mut self, index: usize) {
        self.line_index = (index, 0);
    }
    pub fn move_to_column(&mut self, col: usize) {
        self.column_index = col;
    }
    pub fn reset(&mut self) {
        self.column_index = 0;
        self.line_index = (0, 0);
    }
}

pub struct Scroll {
    canonical_lines: Vec<CanonicalLine>,
    cursor_position: CursorPosition,
    total_columns: usize,
    lines_in_view: usize,
    viewport_bottom_offset: Option<usize>,
    scroll_region: Option<(usize, usize)>, // start line, end line (if set, this is the area the will scroll)
    show_cursor: bool,
    alternative_buffer: Option<Vec<CanonicalLine>>,
    alternative_cursor_position: Option<CursorPosition>,
}

impl Scroll {
    pub fn new(total_columns: usize, lines_in_view: usize) -> Self {
        let mut canonical_lines = vec![];
        canonical_lines.push(CanonicalLine::new());
        let cursor_position = CursorPosition::new();
        Scroll {
            canonical_lines: vec![CanonicalLine::new()], // The rest will be created by newlines explicitly
            total_columns,
            lines_in_view,
            cursor_position,
            viewport_bottom_offset: None,
            scroll_region: None,
            show_cursor: true,
            alternative_buffer: None,
            alternative_cursor_position: None,
        }
    }
    pub fn as_character_lines(&self) -> Vec<Vec<TerminalCharacter>> {
        let mut lines: VecDeque<Vec<TerminalCharacter>> = VecDeque::new(); // TODO: with capacity lines_from_end?
        let canonical_lines = self.canonical_lines.iter().rev();
        let mut lines_to_skip = self.viewport_bottom_offset.unwrap_or(0);
        'gather_lines: for current_canonical_line in canonical_lines {
            for wrapped_fragment in current_canonical_line.wrapped_fragments.iter().rev() {
                let mut line: Vec<TerminalCharacter> =
                    wrapped_fragment.characters.iter().copied().collect();
                if lines_to_skip > 0 {
                    lines_to_skip -= 1;
                } else {
                    // pad line if needed
                    line.resize(self.total_columns, EMPTY_TERMINAL_CHARACTER);
                    lines.push_front(line);
                }
                if lines.len() == self.lines_in_view {
                    break 'gather_lines;
                }
            }
        }
        if lines.len() < self.lines_in_view {
            // pad lines in case we don't have enough scrollback to fill the view
            let empty_line = vec![EMPTY_TERMINAL_CHARACTER; self.total_columns];

            for _ in lines.len()..self.lines_in_view {
                // pad lines in case we didn't have enough
                lines.push_back(empty_line.clone());
            }
        }
        Vec::from(lines)
    }
    pub fn add_character(&mut self, terminal_character: TerminalCharacter) {
        let (canonical_line_position, wrapped_fragment_index_in_line) =
            self.cursor_position.line_index;
        let cursor_position_in_line = self.cursor_position.column_index;
        let current_line = self
            .canonical_lines
            .get_mut(canonical_line_position)
            .expect("cursor out of bounds");
        let current_wrapped_fragment = current_line
            .wrapped_fragments
            .get_mut(wrapped_fragment_index_in_line)
            .expect("cursor out of bounds");

        if cursor_position_in_line < self.total_columns {
            current_wrapped_fragment.add_character(terminal_character, cursor_position_in_line);
            self.cursor_position.move_forward(1);
        } else {
            current_line.add_new_wrap(terminal_character);
            self.cursor_position.move_to_next_linewrap();
            self.cursor_position.move_to_beginning_of_linewrap();
            self.cursor_position.move_forward(1);
        }
    }
    pub fn show_cursor(&mut self) {
        self.show_cursor = true;
    }
    pub fn hide_cursor(&mut self) {
        self.show_cursor = false;
    }
    pub fn add_canonical_line(&mut self) {
        let current_canonical_line_index = self.cursor_position.line_index.0;
        if let Some((scroll_region_top, scroll_region_bottom)) =
            self.scroll_region_absolute_indices()
        {
            if current_canonical_line_index == scroll_region_bottom {
                // end of scroll region
                // when we have a scroll region set and we're at its bottom
                // we need to delete its first line, thus shifting all lines in it upwards
                // then we add an empty line at its end which will be filled by the application
                // controlling the scroll region (presumably filled by whatever comes next in the
                // scroll buffer, but that's not something we control)
                self.canonical_lines.remove(scroll_region_top);
                self.canonical_lines
                    .insert(scroll_region_bottom, CanonicalLine::new());
                return;
            }
        }

        use std::cmp::Ordering;
        match current_canonical_line_index.cmp(&(self.canonical_lines.len() - 1)) {
            Ordering::Equal => {
                self.canonical_lines.push(CanonicalLine::new());
                self.cursor_position.move_to_next_canonical_line();
                self.cursor_position.move_to_beginning_of_canonical_line();
            }
            Ordering::Less => {
                self.cursor_position.move_to_next_canonical_line();
                self.cursor_position.move_to_beginning_of_canonical_line();
            }
            _ => panic!("cursor out of bounds, cannot add_canonical_line"),
        }
    }
    pub fn cursor_coordinates_on_screen(&self) -> Option<(usize, usize)> {
        // (x, y)
        if !self.show_cursor {
            return None;
        }
        let (canonical_line_cursor_position, line_wrap_cursor_position) =
            self.cursor_position.line_index;
        let x = self.cursor_position.column_index;
        let mut y = 0;
        let indices_and_canonical_lines = self.canonical_lines.iter().enumerate().rev();
        for (current_index, current_line) in indices_and_canonical_lines {
            if current_index == canonical_line_cursor_position {
                y += current_line.wrapped_fragments.len() - line_wrap_cursor_position;
                break;
            } else {
                y += current_line.wrapped_fragments.len();
            }
        }
        let total_lines = self
            .canonical_lines
            .iter()
            .fold(0, |total_lines, current_line| {
                total_lines + current_line.wrapped_fragments.len()
            }); // TODO: is this performant enough? should it be cached or kept track of?
        let y = if total_lines < self.lines_in_view {
            total_lines - y
        } else if y > self.lines_in_view {
            self.lines_in_view
        } else {
            self.lines_in_view - y
        };
        Some((x, y))
    }
    pub fn move_cursor_forward(&mut self, count: usize) {
        let (current_canonical_line_index, current_line_wrap_position) =
            self.cursor_position.line_index;
        let current_cursor_column_position = self.cursor_position.column_index;
        let current_canonical_line = self
            .canonical_lines
            .get_mut(current_canonical_line_index)
            .expect("cursor out of bounds");
        let current_fragment = current_canonical_line
            .wrapped_fragments
            .get_mut(current_line_wrap_position)
            .expect("cursor out of bounds");
        let move_count = if current_cursor_column_position + count > self.total_columns {
            // move to last column in the current line wrap
            self.total_columns - current_cursor_column_position
        } else {
            count
        };

        current_fragment.characters.resize(
            max(
                current_fragment.characters.len(),
                current_cursor_column_position + move_count,
            ),
            EMPTY_TERMINAL_CHARACTER,
        );

        self.cursor_position.move_forward(move_count);
    }
    pub fn move_cursor_back(&mut self, count: usize) {
        let current_cursor_column_position = self.cursor_position.column_index;
        if current_cursor_column_position < count {
            self.cursor_position.move_to_beginning_of_linewrap();
        } else {
            self.cursor_position.move_backwards(count);
        }
    }
    pub fn move_cursor_to_beginning_of_linewrap(&mut self) {
        self.cursor_position.move_to_beginning_of_linewrap();
    }
    pub fn _move_cursor_to_beginning_of_canonical_line(&mut self) {
        self.cursor_position.move_to_beginning_of_canonical_line();
    }
    pub fn move_cursor_backwards(&mut self, count: usize) {
        self.cursor_position.move_backwards(count);
    }
    pub fn move_cursor_up(&mut self, count: usize) {
        for _ in 0..count {
            let (current_canonical_line_index, current_line_wrap_index) =
                self.cursor_position.line_index;
            if current_line_wrap_index > 0 {
                // go up one line-wrap
                self.cursor_position.line_index.1 -= 1;
            } else if current_canonical_line_index > 0 {
                // go up to previous canonical line
                self.cursor_position.line_index.0 -= 1;
                let current_canonical_line = self
                    .canonical_lines
                    .get(self.cursor_position.line_index.0)
                    .unwrap();
                let wraps_in_current_line = current_canonical_line.wrapped_fragments.len();
                // make sure to only go up to the last wrap of the previous line
                self.cursor_position.line_index.1 = wraps_in_current_line - 1;
            }
        }
    }
    pub fn move_cursor_down(&mut self, count: usize) {
        let current_canonical_line = self.cursor_position.line_index.0;
        let max_count = (self.canonical_lines.len() - 1) - current_canonical_line;
        let count = std::cmp::min(count, max_count);
        self.cursor_position.move_down_by_canonical_lines(count);
    }
    pub fn change_size(&mut self, columns: usize, lines: usize) {
        for canonical_line in self.canonical_lines.iter_mut() {
            canonical_line.change_width(columns);
        }
        let cursor_line = self
            .canonical_lines
            .get(self.cursor_position.line_index.0)
            .expect("cursor out of bounds");
        if cursor_line.wrapped_fragments.len() <= self.cursor_position.line_index.1 {
            self.cursor_position.line_index.1 = cursor_line.wrapped_fragments.len() - 1;
        }
        self.lines_in_view = lines;
        self.total_columns = columns;
    }
    pub fn clear_canonical_line_right_of_cursor(&mut self, style_of_empty_space: CharacterStyles) {
        let (current_canonical_line_index, current_line_wrap_position) =
            self.cursor_position.line_index;
        let current_cursor_column_position = self.cursor_position.column_index;
        let current_canonical_line = self
            .canonical_lines
            .get_mut(current_canonical_line_index)
            .expect("cursor out of bounds");
        current_canonical_line.replace_with_empty_chars_after_cursor(
            current_line_wrap_position,
            current_cursor_column_position,
            self.total_columns,
            style_of_empty_space,
        );
    }
    pub fn clear_canonical_line_left_of_cursor(&mut self, style_of_empty_space: CharacterStyles) {
        let (current_canonical_line_index, current_line_wrap_position) =
            self.cursor_position.line_index;
        let current_cursor_column_position = self.cursor_position.column_index;
        let current_canonical_line = self
            .canonical_lines
            .get_mut(current_canonical_line_index)
            .expect("cursor out of bounds");
        current_canonical_line.replace_with_empty_chars_before_cursor(
            current_line_wrap_position,
            current_cursor_column_position,
            style_of_empty_space,
        );
    }
    pub fn clear_all_after_cursor(&mut self) {
        let (current_canonical_line_index, current_line_wrap_position) =
            self.cursor_position.line_index;
        let current_cursor_column_position = self.cursor_position.column_index;
        let current_canonical_line = self
            .canonical_lines
            .get_mut(current_canonical_line_index)
            .expect("cursor out of bounds");
        current_canonical_line
            .clear_after(current_line_wrap_position, current_cursor_column_position);
        self.canonical_lines
            .truncate(current_canonical_line_index + 1);
    }
    pub fn replace_with_empty_chars(
        &mut self,
        count: usize,
        style_of_empty_space: CharacterStyles,
    ) {
        let (current_canonical_line_index, current_line_wrap_position) =
            self.cursor_position.line_index;
        let current_cursor_column_position = self.cursor_position.column_index;
        let current_canonical_line = self
            .canonical_lines
            .get_mut(current_canonical_line_index)
            .expect("cursor out of bounds");
        current_canonical_line.replace_with_empty_chars(
            current_line_wrap_position,
            current_cursor_column_position,
            count,
            style_of_empty_space,
        );
    }
    pub fn erase_characters(&mut self, count: usize, style_of_empty_space: CharacterStyles) {
        let (current_canonical_line_index, current_line_wrap_position) =
            self.cursor_position.line_index;
        let current_cursor_column_position = self.cursor_position.column_index;
        let current_canonical_line = self
            .canonical_lines
            .get_mut(current_canonical_line_index)
            .expect("cursor out of bounds");
        current_canonical_line.erase_chars(
            current_line_wrap_position,
            current_cursor_column_position,
            count,
            style_of_empty_space,
        );
    }
    pub fn clear_all(&mut self) {
        self.canonical_lines.clear();
        self.canonical_lines.push(CanonicalLine::new());
        self.cursor_position.reset();
    }
    pub fn move_cursor_to(&mut self, line: usize, col: usize) {
        let line_on_screen = if self.canonical_lines.len() > self.lines_in_view {
            line + (self.canonical_lines.len() - self.lines_in_view)
        } else {
            line
        };
        if self.canonical_lines.len() > line_on_screen {
            self.cursor_position.move_to_canonical_line(line_on_screen);
        } else {
            for _ in self.canonical_lines.len()..=line_on_screen {
                self.canonical_lines.push(CanonicalLine::new());
            }
            self.cursor_position.move_to_canonical_line(line_on_screen);
        }
        let (current_canonical_line_index, current_line_wrap_position) =
            self.cursor_position.line_index;
        let current_canonical_line = self
            .canonical_lines
            .get_mut(current_canonical_line_index)
            .expect("cursor out of bounds");
        let current_fragment = current_canonical_line
            .wrapped_fragments
            .get_mut(current_line_wrap_position)
            .expect("cursor out of bounds");

        current_fragment.characters.resize(
            max(current_fragment.characters.len(), col),
            EMPTY_TERMINAL_CHARACTER,
        );
        self.cursor_position.move_to_column(col);
    }
    pub fn move_cursor_to_column(&mut self, col: usize) {
        let current_canonical_line = self.cursor_position.line_index.0;
        self.move_cursor_to(current_canonical_line, col);
    }
    pub fn move_cursor_to_line(&mut self, line: usize) {
        let current_column = self.cursor_position.column_index;
        self.move_cursor_to(line, current_column);
    }
    pub fn move_current_buffer_to_alternative_buffer(&mut self) {
        self.alternative_buffer = Some(self.canonical_lines.drain(..).collect());
        self.alternative_cursor_position = Some(self.cursor_position);
        self.clear_all();
    }
    pub fn override_current_buffer_with_alternative_buffer(&mut self) {
        if let Some(alternative_buffer) = self.alternative_buffer.as_mut() {
            self.canonical_lines = alternative_buffer.drain(..).collect();
        }
        if let Some(alternative_cursor_position) = self.alternative_cursor_position {
            self.cursor_position = alternative_cursor_position;
        }
        self.alternative_buffer = None;
        self.alternative_cursor_position = None;
    }
    pub fn set_scroll_region(&mut self, top_line: usize, bottom_line: usize) {
        self.scroll_region = Some((top_line, bottom_line));
    }
    pub fn clear_scroll_region(&mut self) {
        self.scroll_region = None;
    }
    fn scroll_region_absolute_indices(&mut self) -> Option<(usize, usize)> {
        self.scroll_region?;
        if self.canonical_lines.len() > self.lines_in_view {
            let absolute_top = self.canonical_lines.len() - 1 - self.lines_in_view;
            let absolute_bottom = self.canonical_lines.len() - 1;
            Some((absolute_top, absolute_bottom))
        } else {
            Some((self.scroll_region.unwrap().0, self.scroll_region.unwrap().1))
        }
    }
    fn scroll_region_absolute_indices_or_screen_edges(&mut self) -> (usize, usize) {
        match self.scroll_region {
            Some(_scroll_region) => self.scroll_region_absolute_indices().unwrap(),
            None => {
                // indices of screen top and bottom edge
                // TODO: what if we don't have enough lines?
                // let absolute_top = self.canonical_lines.len() - 1 - self.lines_in_view;
                let absolute_top = self.canonical_lines.len() - self.lines_in_view;
                let absolute_bottom = self.canonical_lines.len() - 1;
                (absolute_top, absolute_bottom)
            }
        }
    }
    pub fn delete_lines_in_scroll_region(&mut self, count: usize) {
        if let Some((scroll_region_top, scroll_region_bottom)) =
            self.scroll_region_absolute_indices()
        {
            let current_canonical_line_index = self.cursor_position.line_index.0;
            if current_canonical_line_index >= scroll_region_top
                && current_canonical_line_index <= scroll_region_bottom
            {
                // when deleting lines inside the scroll region, we must make sure it stays the
                // same size (and that other lines below it aren't shifted inside it)
                // so we delete the current line(s) and add an empty line at the end of the scroll
                // region
                for _ in 0..count {
                    self.canonical_lines.remove(current_canonical_line_index);
                    self.canonical_lines
                        .insert(scroll_region_bottom, CanonicalLine::new());
                }
            }
        }
    }
    pub fn add_empty_lines_in_scroll_region(&mut self, count: usize) {
        if let Some((scroll_region_top, scroll_region_bottom)) =
            self.scroll_region_absolute_indices()
        {
            let current_canonical_line_index = self.cursor_position.line_index.0;
            if current_canonical_line_index >= scroll_region_top
                && current_canonical_line_index <= scroll_region_bottom
            {
                // when adding empty lines inside the scroll region, we must make sure it stays the
                // same size and that lines don't "leak" outside of it
                // so we add an empty line where the cursor currently is, and delete the last line
                // of the scroll region
                for _ in 0..count {
                    self.canonical_lines.remove(scroll_region_bottom);
                    self.canonical_lines
                        .insert(current_canonical_line_index, CanonicalLine::new());
                }
            }
        }
    }
    pub fn move_cursor_up_in_scroll_region(&mut self, count: usize) {
        let (scroll_region_top, scroll_region_bottom) =
            self.scroll_region_absolute_indices_or_screen_edges();
        for _ in 0..count {
            let current_canonical_line_index = self.cursor_position.line_index.0;
            if current_canonical_line_index == scroll_region_top {
                // if we're at the top line, we create a new line and remove the last line that
                // would otherwise overflow
                self.canonical_lines.remove(scroll_region_bottom);
                self.canonical_lines
                    .insert(current_canonical_line_index, CanonicalLine::new());
            } else if current_canonical_line_index > scroll_region_top
                && current_canonical_line_index <= scroll_region_bottom
            {
                self.move_cursor_up(count);
            }
        }
    }
    /// [scroll_up](https://github.com/alacritty/alacritty/blob/ec42b42ce601808070462111c0c28edb0e89babb/alacritty_terminal/src/grid/mod.rs#L261)
    /// This function takes the first line of the scroll region and moves it to the bottom (count times)
    pub fn rotate_scroll_region_up(&mut self, count: usize) {
        if let Some((_, scroll_region_bottom)) = self.scroll_region_absolute_indices() {
            if self.show_cursor {
                let scroll_region_bottom_index = scroll_region_bottom - 1;
                self.cursor_position
                    .move_to_canonical_line(scroll_region_bottom_index);

                let new_empty_lines = vec![CanonicalLine::new(); count];
                self.canonical_lines.splice(
                    scroll_region_bottom_index..scroll_region_bottom_index + 1,
                    new_empty_lines,
                );

                self.cursor_position
                    .move_to_canonical_line(scroll_region_bottom_index + count);
            }
        }
    }
    /// [scroll_down](https://github.com/alacritty/alacritty/blob/ec42b42ce601808070462111c0c28edb0e89babb/alacritty_terminal/src/grid/mod.rs#L221)
    /// This function takes the last line of the scroll region and moves it to the top (count times)
    pub fn rotate_scroll_region_down(&mut self, count: usize) {
        if let Some((scroll_region_top, _)) = self.scroll_region_absolute_indices() {
            if self.show_cursor {
                let scroll_region_top_index = scroll_region_top - 1;
                self.cursor_position
                    .move_to_canonical_line(scroll_region_top_index);

                let new_empty_lines = vec![CanonicalLine::new(); count];
                self.canonical_lines.splice(
                    scroll_region_top_index..scroll_region_top_index,
                    new_empty_lines,
                );

                self.cursor_position
                    .move_to_canonical_line(scroll_region_top_index + count);
            }
        }
    }
    pub fn move_viewport_up(&mut self, count: usize) {
        if let Some(current_offset) = self.viewport_bottom_offset.as_mut() {
            *current_offset += count;
        } else {
            self.viewport_bottom_offset = Some(count);
        }
    }
    pub fn move_viewport_down(&mut self, count: usize) {
        if let Some(current_offset) = self.viewport_bottom_offset.as_mut() {
            if *current_offset > count {
                *current_offset -= count;
            } else {
                self.viewport_bottom_offset = None;
            }
        }
    }
    pub fn reset_viewport(&mut self) {
        self.viewport_bottom_offset = None;
    }
}

impl Debug for Scroll {
    fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
        for line in &self.canonical_lines {
            writeln!(f, "{:?}", line)?;
        }
        Ok(())
    }
}