Windows Terminal breaks when the script hits the end of visible terminal window #19538

Closed
opened 2026-01-31 06:46:33 +00:00 by claunia · 5 comments
Owner

Originally created by @farag2 on GitHub (Mar 14, 2023).

Windows Terminal version

1.16.10262.0

Windows build number

10.0.22621

Other Software

function ShowMenu
{
	[CmdletBinding()]
	param
	(
		[Parameter(Mandatory = $true)]
		[array]
		$Menu
	)

	$minY = [Console]::CursorTop
	$y = [Math]::Max([Math]::Min(1, $Menu.Count), 0)

	do
	{
		[Console]::CursorTop = $minY
		[Console]::CursorLeft = 0
		$i = 0
		foreach ($item in $Menu)
		{
			if ($i -ne $y)
			{
				Write-Information -MessageData ('  {1}  ' -f ($i+1), $item) -InformationAction Continue
			}
			else
			{
				Write-Information -MessageData ('[ {1} ]' -f ($i+1), $item) -InformationAction Continue
			}
			$i++
		}

		$k = [Console]::ReadKey()
		switch ($k.Key)
		{
			"UpArrow"
			{
				if ($y -gt 0)
				{
					$y--
				}
			}
			"DownArrow"
			{
				if ($y -lt ($Menu.Count - 1))
				{
					$y++
				}
			}
			"Enter"
			{
				return $Menu[$y]
			}
		}
	}
	while ($k.Key -notin ([ConsoleKey]::Escape, [ConsoleKey]::Enter))

}

# Get disks letters
ShowMenu -Menu @((Get-CimInstance -ClassName CIM_LogicalDisk | Where-Object -FilterScript {$_.DriveType -eq 3}).DeviceID | Sort-Object)

Steps to reproduce

If we run this snippet that just shows disks letters, after we hit the end of visible windows area we cannot use brackets to choose: disks letters overlap each other when I use arrow down/up.

The bug is not reproducable neither in bare powershell.exe (5.1), nor in pwsh.exe (7.3)

Recorded video with the bug

https://user-images.githubusercontent.com/10544660/225085472-550932ac-49f2-4804-8fdf-cf36fbf99867.mp4

Bare powershell.exe (5.1)

https://user-images.githubusercontent.com/10544660/225085862-3c359282-e58c-4b92-88e8-cd45389d2cb5.mp4

Expected Behavior

Regardless we hit the visible console window, we disks letters shoudn't overlap each other.

Actual Behavior

Disks letters overlap each other in the console

Originally created by @farag2 on GitHub (Mar 14, 2023). ### Windows Terminal version 1.16.10262.0 ### Windows build number 10.0.22621 ### Other Software ```powershell function ShowMenu { [CmdletBinding()] param ( [Parameter(Mandatory = $true)] [array] $Menu ) $minY = [Console]::CursorTop $y = [Math]::Max([Math]::Min(1, $Menu.Count), 0) do { [Console]::CursorTop = $minY [Console]::CursorLeft = 0 $i = 0 foreach ($item in $Menu) { if ($i -ne $y) { Write-Information -MessageData (' {1} ' -f ($i+1), $item) -InformationAction Continue } else { Write-Information -MessageData ('[ {1} ]' -f ($i+1), $item) -InformationAction Continue } $i++ } $k = [Console]::ReadKey() switch ($k.Key) { "UpArrow" { if ($y -gt 0) { $y-- } } "DownArrow" { if ($y -lt ($Menu.Count - 1)) { $y++ } } "Enter" { return $Menu[$y] } } } while ($k.Key -notin ([ConsoleKey]::Escape, [ConsoleKey]::Enter)) } # Get disks letters ShowMenu -Menu @((Get-CimInstance -ClassName CIM_LogicalDisk | Where-Object -FilterScript {$_.DriveType -eq 3}).DeviceID | Sort-Object) ``` ### Steps to reproduce If we run this snippet that just shows disks letters, after we hit the end of visible windows area we cannot use brackets to choose: disks letters overlap each other when I use arrow down/up. The bug is not reproducable neither in bare powershell.exe (5.1), nor in pwsh.exe (7.3) Recorded video with the bug https://user-images.githubusercontent.com/10544660/225085472-550932ac-49f2-4804-8fdf-cf36fbf99867.mp4 Bare powershell.exe (5.1) https://user-images.githubusercontent.com/10544660/225085862-3c359282-e58c-4b92-88e8-cd45389d2cb5.mp4 ### Expected Behavior Regardless we hit the visible console window, we disks letters shoudn't overlap each other. ### Actual Behavior Disks letters overlap each other in the console
claunia added the Needs-TriageIssue-BugNeeds-Attention labels 2026-01-31 06:46:33 +00:00
Author
Owner

@zadjii-msft commented on GitHub (Mar 14, 2023):

Can you test something for me? In bare powershell, can you fill up the entire buffer, then try to repro the bug? Something like a gci -Recurse should fill it up pretty quickly. Just make sure that there's enough output so that the scrollbar is all the way at the bottom of the console window.

Once the buffer's full, try reproing this again/?

My hunch is that this never worked at the bottom of the buffer, and as far as the Terminal is concerned, the bottom of the viewport is the bottom of the buffer.

@zadjii-msft commented on GitHub (Mar 14, 2023): Can you test something for me? In bare powershell, can you fill up the _entire_ buffer, then try to repro the bug? Something like a `gci -Recurse` should fill it up pretty quickly. Just make sure that there's enough output so that the scrollbar is all the way at the bottom of the console window. Once the buffer's full, try reproing this again/? My hunch is that this never worked at the bottom of the _buffer_, and as far as the Terminal is concerned, the bottom of the viewport _is_ the bottom of the buffer.
Author
Owner

@farag2 commented on GitHub (Mar 14, 2023):

@zadjii-msft, did what you asked. It works in bare powershell.exe (5.1).

https://user-images.githubusercontent.com/10544660/225117607-5b36d273-2068-49b0-832c-47400b8819a2.mp4

@farag2 commented on GitHub (Mar 14, 2023): @zadjii-msft, did what you asked. It works in bare powershell.exe (5.1). https://user-images.githubusercontent.com/10544660/225117607-5b36d273-2068-49b0-832c-47400b8819a2.mp4
Author
Owner

@lhecker commented on GitHub (Mar 15, 2023):

@farag2 No, try filling your entire scrollback buffer. Your scrollbar shows that there's still some space left. Here's a faster way than gci -Recurse to do so:

[Console]::CursorTop = [Console]::BufferHeight - 1

I agree with @zadjii-msft here: This has never worked reliably, and if you try the above it should show that. The problem is that once the buffer is full, writing more text will scroll the text upwards, but cursor positions will remain relative to the scrollback buffer! They don't get scrolled up with the content.

@lhecker commented on GitHub (Mar 15, 2023): @farag2 No, try filling your _entire_ scrollback buffer. Your scrollbar shows that there's still some space left. Here's a faster way than `gci -Recurse` to do so: ```pwsh [Console]::CursorTop = [Console]::BufferHeight - 1 ``` I agree with @zadjii-msft here: This has never worked reliably, and if you try the above it should show that. The problem is that once the buffer is full, writing more text will scroll the text upwards, but cursor positions will remain relative to the scrollback buffer! They don't get scrolled up with the content.
Author
Owner

@farag2 commented on GitHub (Mar 15, 2023):

Yeah, I got it. It really occurs in powershell.exe too. I just wanted to know whether this is a Terminal bug only. You're right.

This code fixes the bug. Thank you guys! 🐱

[System.Console]::BufferHeight += $Menu.Count
$minY = [Console]::CursorTop
$y = [Math]::Max([Math]::Min($Default, $Menu.Count), 0)
@farag2 commented on GitHub (Mar 15, 2023): Yeah, I got it. It really occurs in powershell.exe too. I just wanted to know whether this is a Terminal bug only. You're right. This code fixes the bug. Thank you guys! :cat: ```powershell [System.Console]::BufferHeight += $Menu.Count $minY = [Console]::CursorTop $y = [Math]::Max([Math]::Min($Default, $Menu.Count), 0) ```
Author
Owner

@farag2 commented on GitHub (Mar 29, 2025):

Hello, @lhecker. Unfortunately, the issue has returned in 1.22.10731.0, and

[System.Console]::BufferHeight += $Menu.Count
$minY = [Console]::CursorTop
$y = [Math]::Max([Math]::Min($Default, $Menu.Count), 0)

no longer helps. Could you please find a solution. Thanks in advance.

@farag2 commented on GitHub (Mar 29, 2025): Hello, @lhecker. Unfortunately, the issue has returned in `1.22.10731.0`, and ```powershell [System.Console]::BufferHeight += $Menu.Count $minY = [Console]::CursorTop $y = [Math]::Max([Math]::Min($Default, $Menu.Count), 0) ``` no longer helps. Could you please find a solution. Thanks in advance.
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: starred/terminal#19538