Repository: zellij-org/zellij (language: rust)

Issue:
Flakey scroll history after scrolling up inside less
In https://github.com/zellij-org/zellij/issues/797 I mentioned I also sometimes see some artifacts when scrolling up in zellij.
The linked issue was resolved, but as of zellij 0.38.2, I still see some artifacts. This makes me believe there is another, different problem.

In the mean time, I found a way to reproduce my issue. It seems to involve the pager `less`, but with a specific configuration so that the last paged content stays on the screen when less is exited with `q` (not using alternate screen of the terminal that would clear the text on exit). I found that git does this when paging, but probably there is a way of invoking `less` directly with the correct flags.

The specific steps that reproduce the bug on my machine:
- enter a git repo with enough commits that you can fill your screen with
- `PAGER=less git log --oneline`
- scroll down inside `less` by pressing `Page-down` a couple of times
- scroll up inside `less` by pressing `Page-up` or multiple times `arrow-up`
- **optional**: Exit `less` by pressing `q` (the last paged content must stay on the screen), continue working in the terminal
- scroll up inside zellij with the mouse wheel
- Observe garbled scrollback somewhere in the `less` output

Alacritty handles this scenario gracefully.

Screenshot before scrolling up:
![image](https://github.com/zellij-org/zellij/assets/15750438/39eb03b3-76d0-4d95-8eef-61cf0011bf7f)

After:
![image](https://github.com/zellij-org/zellij/assets/15750438/bf6df8d0-cf48-4b87-b234-c93298acabca)




Discussion / hints:
Instead of scrolling up inside zellij with the mouse wheel (last step), I found it also effective to enter and exit vim to produce the garbled scrollback.

Fix patch:
diff --git a/zellij-server/src/panes/grid.rs b/zellij-server/src/panes/grid.rs
index 498f0d6080..a3eda6cbcb 100644
--- a/zellij-server/src/panes/grid.rs
+++ b/zellij-server/src/panes/grid.rs
@@ -1525,7 +1525,9 @@ impl Grid {
                 if scroll_region_bottom < self.viewport.len() {
                     self.viewport.remove(scroll_region_bottom);
                 }
-                self.viewport.insert(current_line_index, Row::new()); // TODO: .canonical() ?
+
+                self.viewport
+                    .insert(current_line_index, Row::new().canonical());
             } else if current_line_index > scroll_region_top
                 && current_line_index <= scroll_region_bottom
             {


Test patch:
diff --git a/zellij-server/src/panes/unit/snapshots/zellij_server__panes__grid__grid_tests__git_diff_scrollup.snap b/zellij-server/src/panes/unit/snapshots/zellij_server__panes__grid__grid_tests__git_diff_scrollup.snap
index e4c5e29f07..4320b5b2c5 100644
--- a/zellij-server/src/panes/unit/snapshots/zellij_server__panes__grid__grid_tests__git_diff_scrollup.snap
+++ b/zellij-server/src/panes/unit/snapshots/zellij_server__panes__grid__grid_tests__git_diff_scrollup.snap
@@ -1,11 +1,10 @@
 ---
 source: zellij-server/src/panes/./unit/grid_tests.rs
 expression: "format!(\"{:?}\", grid)"
-
 ---
-00 (W): 
-01 (W): src/terminal_pane/scroll.rs
-02 (W): ───────────────────────────────────────────────────────────────────────────────────────────────────────────────────
+00 (C): 
+01 (C): src/terminal_pane/scroll.rs
+02 (C): ───────────────────────────────────────────────────────────────────────────────────────────────────────────────────
 03 (C): 
 04 (C): ────────────────────────────────────────────────┐
 05 (C): use crate::terminal_pane::terminal_character::{ │
diff --git a/zellij-server/src/panes/unit/snapshots/zellij_server__panes__grid__grid_tests__htop_scrolling.snap b/zellij-server/src/panes/unit/snapshots/zellij_server__panes__grid__grid_tests__htop_scrolling.snap
index 5f6c323d71..90f5227b6f 100644
--- a/zellij-server/src/panes/unit/snapshots/zellij_server__panes__grid__grid_tests__htop_scrolling.snap
+++ b/zellij-server/src/panes/unit/snapshots/zellij_server__panes__grid__grid_tests__htop_scrolling.snap
@@ -1,7 +1,6 @@
 ---
 source: zellij-server/src/panes/./unit/grid_tests.rs
 expression: "format!(\"{:?}\", grid)"
-
 ---
 00 (C):                                                                                                                     
 01 (C):   1  [||||||||||||||||||||||||||||||||||||||||||100.0%]   Tasks: 79, 382 thr; 1 running                             
@@ -12,7 +11,7 @@ expression: "format!(\"{:?}\", grid)"
 06 (C):   Swp[                                        0K/16.0G]                                                             
 07 (C):                                                                                                                     
 08 (C):     PID USER      PRI  NI  VIRT   RES   SHR S CPU% MEM%   TIME+  Command                                            
-09 (W):  123934 aram       20   0  8444  4384  3364 R 66.7  0.0  0:00.05 htop --delay=100000000000                          
+09 (C):  123934 aram       20   0  8444  4384  3364 R 66.7  0.0  0:00.05 htop --delay=100000000000                          
 10 (C):       1 root       20   0  171M 11616  8608 S  0.0  0.1  0:56.91 /sbin/init                                         
 11 (C):     268 root       20   0 93324 34340 33072 S  0.0  0.2  0:01.05 /usr/lib/systemd/systemd-journald                  
 12 (C):     276 root       20   0 32648 10192  7240 S  0.0  0.1  0:01.13 /usr/lib/systemd/systemd-udevd                     
diff --git a/zellij-server/src/panes/unit/snapshots/zellij_server__panes__grid__grid_tests__vttest2_6.snap b/zellij-server/src/panes/unit/snapshots/zellij_server__panes__grid__grid_tests__vttest2_6.snap
index 98cd7fac19..7d7a4ba666 100644
--- a/zellij-server/src/panes/unit/snapshots/zellij_server__panes__grid__grid_tests__vttest2_6.snap
+++ b/zellij-server/src/panes/unit/snapshots/zellij_server__panes__grid__grid_tests__vttest2_6.snap
@@ -3,19 +3,19 @@ source: zellij-server/src/panes/./unit/grid_tests.rs
 assertion_line: 305
 expression: "format!(\"{:?}\", grid)"
 ---
-00 (W): Push <RETURN>
-01 (W): Soft scroll down region [12..13] size 2 Line 29
-02 (W): Soft scroll down region [12..13] size 2 Line 28
-03 (W): Soft scroll down region [12..13] size 2 Line 27
-04 (W): Soft scroll down region [12..13] size 2 Line 26
-05 (W): Soft scroll down region [12..13] size 2 Line 25
-06 (W): Soft scroll down region [12..13] size 2 Line 24
-07 (W): Soft scroll down region [12..13] size 2 Line 23
-08 (W): Soft scroll down region [12..13] size 2 Line 22
-09 (W): Soft scroll down region [12..13] size 2 Line 21
-10 (W): Soft scroll down region [12..13] size 2 Line 20
-11 (W): Soft scroll down region [12..13] size 2 Line 19
-12 (W): Soft scroll down region [12..13] size 2 Line 18
+00 (C): Push <RETURN>
+01 (C): Soft scroll down region [12..13] size 2 Line 29
+02 (C): Soft scroll down region [12..13] size 2 Line 28
+03 (C): Soft scroll down region [12..13] size 2 Line 27
+04 (C): Soft scroll down region [12..13] size 2 Line 26
+05 (C): Soft scroll down region [12..13] size 2 Line 25
+06 (C): Soft scroll down region [12..13] size 2 Line 24
+07 (C): Soft scroll down region [12..13] size 2 Line 23
+08 (C): Soft scroll down region [12..13] size 2 Line 22
+09 (C): Soft scroll down region [12..13] size 2 Line 21
+10 (C): Soft scroll down region [12..13] size 2 Line 20
+11 (C): Soft scroll down region [12..13] size 2 Line 19
+12 (C): Soft scroll down region [12..13] size 2 Line 18
 13 (C): Soft scroll down region [12..13] size 2 Line 17                                                               
 14 (C): Soft scroll down region [12..13] size 2 Line 16                                                               
 15 (C): Soft scroll down region [12..13] size 2 Line 15                                                               
diff --git a/zellij-server/src/panes/unit/snapshots/zellij_server__panes__grid__grid_tests__vttest2_7.snap b/zellij-server/src/panes/unit/snapshots/zellij_server__panes__grid__grid_tests__vttest2_7.snap
index 734a9f4696..e4cf1a2262 100644
--- a/zellij-server/src/panes/unit/snapshots/zellij_server__panes__grid__grid_tests__vttest2_7.snap
+++ b/zellij-server/src/panes/unit/snapshots/zellij_server__panes__grid__grid_tests__vttest2_7.snap
@@ -3,19 +3,19 @@ source: zellij-server/src/panes/./unit/grid_tests.rs
 assertion_line: 327
 expression: "format!(\"{:?}\", grid)"
 ---
-00 (W): Push <RETURN>
-01 (W): Soft scroll down region [1..24] size 24 Line 29
-02 (W): Soft scroll down region [1..24] size 24 Line 28
-03 (W): Soft scroll down region [1..24] size 24 Line 27
-04 (W): Soft scroll down region [1..24] size 24 Line 26
-05 (W): Soft scroll down region [1..24] size 24 Line 25
-06 (W): Soft scroll down region [1..24] size 24 Line 24
-07 (W): Soft scroll down region [1..24] size 24 Line 23
-08 (W): Soft scroll down region [1..24] size 24 Line 22
-09 (W): Soft scroll down region [1..24] size 24 Line 21
-10 (W): Soft scroll down region [1..24] size 24 Line 20
-11 (W): Soft scroll down region [1..24] size 24 Line 19
-12 (W): Soft scroll down region [1..24] size 24 Line 18
+00 (C): Push <RETURN>
+01 (C): Soft scroll down region [1..24] size 24 Line 29
+02 (C): Soft scroll down region [1..24] size 24 Line 28
+03 (C): Soft scroll down region [1..24] size 24 Line 27
+04 (C): Soft scroll down region [1..24] size 24 Line 26
+05 (C): Soft scroll down region [1..24] size 24 Line 25
+06 (C): Soft scroll down region [1..24] size 24 Line 24
+07 (C): Soft scroll down region [1..24] size 24 Line 23
+08 (C): Soft scroll down region [1..24] size 24 Line 22
+09 (C): Soft scroll down region [1..24] size 24 Line 21
+10 (C): Soft scroll down region [1..24] size 24 Line 20
+11 (C): Soft scroll down region [1..24] size 24 Line 19
+12 (C): Soft scroll down region [1..24] size 24 Line 18
 13 (C): Soft scroll down region [1..24] size 24 Line 17                                                               
 14 (C): Soft scroll down region [1..24] size 24 Line 16                                                               
 15 (C): Soft scroll down region [1..24] size 24 Line 15                                                               
diff --git a/zellij-server/src/panes/unit/snapshots/zellij_server__panes__grid__grid_tests__vttest2_8.snap b/zellij-server/src/panes/unit/snapshots/zellij_server__panes__grid__grid_tests__vttest2_8.snap
index 6f313bec71..b186874356 100644
--- a/zellij-server/src/panes/unit/snapshots/zellij_server__panes__grid__grid_tests__vttest2_8.snap
+++ b/zellij-server/src/panes/unit/snapshots/zellij_server__panes__grid__grid_tests__vttest2_8.snap
@@ -3,19 +3,19 @@ source: zellij-server/src/panes/./unit/grid_tests.rs
 assertion_line: 349
 expression: "format!(\"{:?}\", grid)"
 ---
-00 (W): Push <RETURN>
-01 (W): Jump scroll down region [12..13] size 2 Line 29
-02 (W): Jump scroll down region [12..13] size 2 Line 28
-03 (W): Jump scroll down region [12..13] size 2 Line 27
-04 (W): Jump scroll down region [12..13] size 2 Line 26
-05 (W): Jump scroll down region [12..13] size 2 Line 25
-06 (W): Jump scroll down region [12..13] size 2 Line 24
-07 (W): Jump scroll down region [12..13] size 2 Line 23
-08 (W): Jump scroll down region [12..13] size 2 Line 22
-09 (W): Jump scroll down region [12..13] size 2 Line 21
-10 (W): Jump scroll down region [12..13] size 2 Line 20
-11 (W): Jump scroll down region [12..13] size 2 Line 19
-12 (W): Jump scroll down region [12..13] size 2 Line 18
+00 (C): Push <RETURN>
+01 (C): Jump scroll down region [12..13] size 2 Line 29
+02 (C): Jump scroll down region [12..13] size 2 Line 28
+03 (C): Jump scroll down region [12..13] size 2 Line 27
+04 (C): Jump scroll down region [12..13] size 2 Line 26
+05 (C): Jump scroll down region [12..13] size 2 Line 25
+06 (C): Jump scroll down region [12..13] size 2 Line 24
+07 (C): Jump scroll down region [12..13] size 2 Line 23
+08 (C): Jump scroll down region [12..13] size 2 Line 22
+09 (C): Jump scroll down region [12..13] size 2 Line 21
+10 (C): Jump scroll down region [12..13] size 2 Line 20
+11 (C): Jump scroll down region [12..13] size 2 Line 19
+12 (C): Jump scroll down region [12..13] size 2 Line 18
 13 (C): Jump scroll down region [12..13] size 2 Line 17                                                               
 14 (C): Jump scroll down region [12..13] size 2 Line 16                                                               
 15 (C): Jump scroll down region [12..13] size 2 Line 15                                                               
diff --git a/zellij-server/src/panes/unit/snapshots/zellij_server__panes__grid__grid_tests__vttest2_9.snap b/zellij-server/src/panes/unit/snapshots/zellij_server__panes__grid__grid_tests__vttest2_9.snap
index fed2d986df..eb18b942a7 100644
--- a/zellij-server/src/panes/unit/snapshots/zellij_server__panes__grid__grid_tests__vttest2_9.snap
+++ b/zellij-server/src/panes/unit/snapshots/zellij_server__panes__grid__grid_tests__vttest2_9.snap
@@ -3,19 +3,19 @@ source: zellij-server/src/panes/./unit/grid_tests.rs
 assertion_line: 371
 expression: "format!(\"{:?}\", grid)"
 ---
-00 (W): Push <RETURN>
-01 (W): Jump scroll down region [1..24] size 24 Line 29
-02 (W): Jump scroll down region [1..24] size 24 Line 28
-03 (W): Jump scroll down region [1..24] size 24 Line 27
-04 (W): Jump scroll down region [1..24] size 24 Line 26
-05 (W): Jump scroll down region [1..24] size 24 Line 25
-06 (W): Jump scroll down region [1..24] size 24 Line 24
-07 (W): Jump scroll down region [1..24] size 24 Line 23
-08 (W): Jump scroll down region [1..24] size 24 Line 22
-09 (W): Jump scroll down region [1..24] size 24 Line 21
-10 (W): Jump scroll down region [1..24] size 24 Line 20
-11 (W): Jump scroll down region [1..24] size 24 Line 19
-12 (W): Jump scroll down region [1..24] size 24 Line 18
+00 (C): Push <RETURN>
+01 (C): Jump scroll down region [1..24] size 24 Line 29
+02 (C): Jump scroll down region [1..24] size 24 Line 28
+03 (C): Jump scroll down region [1..24] size 24 Line 27
+04 (C): Jump scroll down region [1..24] size 24 Line 26
+05 (C): Jump scroll down region [1..24] size 24 Line 25
+06 (C): Jump scroll down region [1..24] size 24 Line 24
+07 (C): Jump scroll down region [1..24] size 24 Line 23
+08 (C): Jump scroll down region [1..24] size 24 Line 22
+09 (C): Jump scroll down region [1..24] size 24 Line 21
+10 (C): Jump scroll down region [1..24] size 24 Line 20
+11 (C): Jump scroll down region [1..24] size 24 Line 19
+12 (C): Jump scroll down region [1..24] size 24 Line 18
 13 (C): Jump scroll down region [1..24] size 24 Line 17                                                               
 14 (C): Jump scroll down region [1..24] size 24 Line 16                                                               
 15 (C): Jump scroll down region [1..24] size 24 Line 15