microsoft / microsoft/terminal

Weird behavior when clearing scrollback

Open
#16,378 3 comments 0 reactions 0 assignees View on GitHub
Area-VT Issue-Bug Needs-Repro Product-Terminal
Dominant language
C++
Stars
105k
Forks
9.6k
Avg merge
3d 17h
Merged PRs (30d)
29

Description

### Windows Terminal version

1.18.2822.0

### Windows build number

10.0.19045.3693

### Other Software

WSL: 2.0.9.0
Kernel: 5.15.133.1-1
zsh: 5.9

I use a plugin manager called zsh4humans, and it provides some commands for keeping the prompt at the bottom of the terminal. This is the command that clears the scrollback, called `z4h-clear-screen-hard-bottom`:

```sh
[[ -v _z4h_tty_fd ]] || return

builtin echoti civis >&$_z4h_tty_fd
builtin print -rnu $_z4h_tty_fd -- $'\e[H\e[2J'"${(pl:$((LINES-1))::\n:)}"
builtin zle -I
builtin zle -R
builtin print -rnu $_z4h_tty_fd -- $'\e[3J'
-z4h-cursor-show
```

This is how the variable `_z4h_tty_fd` is set:
```sh
if [[ -w $TTY ]]; then
typeset -gi _z4h_tty_fd
sysopen -o cloexec -rwu _z4h_tty_fd -- $TTY || return
typeset -gri _z4h_tty_fd
elif [[ -w /dev/tty ]]; then
typeset -gi _z4h_tty_fd
if sysopen -o cloexec -rwu _z4h_tty_fd -- /dev/tty 2>/dev/null; then
typeset -gri _z4h_tty_fd
else
unset _z4h_tty_fd
fi
fi
```

And this is the `-z4h-cursor-show` function:
```sh
function -z4h-cursor-show() {
local cnorm=${${terminfo[cnorm]-}:/*$'\e[?25h'(|'\e'*)/$'\e[?25h'}
[[ -t 1 ]] && print -rn -- $cnorm || print -rnu $_z4h_tty_fd -- $cnorm
}
```

### Steps to reproduce

- Open Windows Terminal and run WSL (tested with Ubuntu and ArchWSL);
- Install zsh4humans;
- Edit `.zshrc`:
- Delete the line `zstyle ':z4h:' start-tmux no`
- Add these lines at the end of the file:
```sh
zstyle ':z4h:' prompt-at-bottom yes
z4h bindkey z4h-clear-screen-hard-bottom Ctrl+L
```
- Run `exec zsh` or `source ~/.zshrc` to apply the new changes;
- Press Ctrl-L.

### Expected Behavior

The `z4h-clear-screen-hard-bottom` command is meant to clear the scrollback and push the prompt to the bottom of the terminal. [Here](https://asciinema.org/a/372068) is an example of how it should work.

### Actual Behavior

It seems that in Windows Terminal, for some reason, the prompt itself is also cleared:

https://github.com/microsoft/terminal/assets/31555284/ed7348a3-d94c-4f7e-be57-065b5fb66be8

I opened an issue in the official zsh4humans repository, and the owner asked me to try to reproduce this issue in other terminal emulators. I tried it with Cmder, ConEmu and Hyper, and none of them share this issue.

Does anyone know what could be causing this?

Contributor guide

Open the contributing guide

Research direction

Start by reproducing the Ctrl-L flow with z4h-clear-screen-hard-bottom in Windows Terminal running WSL, paying attention to the ESC sequences \e[H\e[2J and \e[3J sent by the command. The issue names no repository file or test; done means the scrollback is cleared and the prompt remains visible at the bottom, matching the expected recording.

Written by the indexing model from the issue text.

Assessment

Tech stack
zsh
Domain
cli, operating-systems
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
42/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.