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

Closed
opened 2026-01-31 08:32:08 +00:00 by claunia · 12 comments
Owner

Originally created by @farag2 on GitHub (Mar 29, 2025).

Windows Terminal version

1.22.10731.0

Windows build number

10.0.26100.3476

Other Software

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

	# Checking whether current terminal is Windows Terminal
	if ($env:WT_SESSION)
	{
		# https://github.com/microsoft/terminal/issues/14992
		[System.Console]::BufferHeight += $Menu.Count # a workaround for < 1.22 release
	}
	$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.4)

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

The issue is fully similar to #14992, but the provided workaround has stopped working after Terminal 1.22 release.

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 29, 2025). ### Windows Terminal version 1.22.10731.0 ### Windows build number 10.0.26100.3476 ### Other Software ```powershell function ShowMenu { [CmdletBinding()] param ( [Parameter(Mandatory = $true)] [array] $Menu ) # Checking whether current terminal is Windows Terminal if ($env:WT_SESSION) { # https://github.com/microsoft/terminal/issues/14992 [System.Console]::BufferHeight += $Menu.Count # a workaround for < 1.22 release } $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.4) 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 The issue is fully similar to #14992, but the provided workaround has stopped working after Terminal 1.22 release. ### 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 08:32:08 +00:00
Author
Owner

@similar-issues-ai[bot] commented on GitHub (Mar 29, 2025):

We've found some similar issues:

  • #14992 , similarity score: 96%

If any of the above are duplicates, please consider closing this issue out and adding additional context in the original issue.

Note: You can give me feedback by 👍 or 👎 this comment.

@similar-issues-ai[bot] commented on GitHub (Mar 29, 2025): We've found some similar issues: - #14992 , similarity score: 96% If any of the above are duplicates, please consider closing this issue out and adding additional context in the original issue. > Note: You can give me feedback by 👍 or 👎 this comment.
Author
Owner

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

The issue is fully similar to https://github.com/microsoft/terminal/issues/14992, but the provided workaround has stopped working after Terminal 1.22 release.

@farag2 commented on GitHub (Mar 29, 2025): The issue is fully similar to https://github.com/microsoft/terminal/issues/14992, but the provided workaround has stopped working after Terminal 1.22 release.
Author
Owner

@farag2 commented on GitHub (Apr 1, 2025):

@lhecker

@farag2 commented on GitHub (Apr 1, 2025): @lhecker
Author
Owner

@DHowett commented on GitHub (Apr 2, 2025):

Thanks for the report.

		[System.Console]::BufferHeight += $Menu.Count # a workaround for < 1.22 release

If you remove the workaround for earlier versions, does it work?
Terminal was never expected to support buffers taller than the viewport size. It's no wonder it's acting up. 🙂

@DHowett commented on GitHub (Apr 2, 2025): Thanks for the report. > ``` > [System.Console]::BufferHeight += $Menu.Count # a workaround for < 1.22 release > ``` If you remove the workaround for earlier versions, does it work? Terminal was never expected to support buffers taller than the viewport size. It's no wonder it's acting up. 🙂
Author
Owner

@farag2 commented on GitHub (Apr 3, 2025):

Hello. Unfortunately, no.

@farag2 commented on GitHub (Apr 3, 2025): Hello. Unfortunately, no.
Author
Owner

@farag2 commented on GitHub (Apr 4, 2025):

I moved to this function, and it has no such bug. I guess, I can close the issue as we cannot demand working such features from Terminal after any update. :)

974a8a4f67/src/Sophia_Script_for_Windows_11/Module/Sophia.psm1 (L896)

@farag2 commented on GitHub (Apr 4, 2025): I moved to this function, and it has no such bug. I guess, I can close the issue as we cannot demand working such features from Terminal after any update. :) https://github.com/farag2/Sophia-Script-for-Windows/blob/974a8a4f6772780e4c70bd24c45e3c92b855de56/src/Sophia_Script_for_Windows_11/Module/Sophia.psm1#L896
Author
Owner

@DHowett commented on GitHub (Apr 4, 2025):

we cannot demand working such features from Terminal after any update. :)

You know that's untrue, and you're just saying it to be mean. :)

@DHowett commented on GitHub (Apr 4, 2025): > we cannot demand working such features from Terminal after any update. :) You know that's untrue, and you're just saying it to be mean. :)
Author
Owner

@farag2 commented on GitHub (Apr 4, 2025):

I will be happy if you provide a solution how to fix that)

@farag2 commented on GitHub (Apr 4, 2025): I will be happy if you provide a solution how to fix that)
Author
Owner

@DHowett commented on GitHub (Apr 4, 2025):

The problem is in your script.

Once the output reaches the bottom of the buffer (which will happen in Terminal within the first 30 lines of output, and will happen in conhost after 9031 lines), the cursor position you save is the position of the last line in the buffer.

You then produce additional lines of output, moving the old last line up (imputed $StartOfMenuY -= $Menu.Count)

You then move the cursor back to the actual last line of the screen, and draw the whole thing again.
This pushes up the content again.

The reason Sophia_Script works is that it computes the top of the menu by starting from the cursor and moving up by $menu.count and that it re-computes the y value every time the loop goes around.

The reason your workaround worked before is that it put the console buffer out of sync with the terminal, growing it by $menu.count lines and breaking all future applications that run in that console session. 🙂

@DHowett commented on GitHub (Apr 4, 2025): The problem is in your script. Once the output reaches the bottom of the buffer (which will happen in Terminal within the first 30 lines of output, _and will happen in conhost after 9031 lines_), the cursor position you save is the position of the last line in the buffer. You then produce additional lines of output, moving the old last line up (imputed `$StartOfMenuY -= $Menu.Count`) You then move the cursor back to the _actual last line of the screen_, and draw the whole thing again. This pushes up the content _again_. The reason Sophia_Script works is that it computes the top of the menu by starting from the cursor and moving up by `$menu.count` **and** that it re-computes the `y` value every time the loop goes around. The reason your workaround worked before is that it put the console buffer out of sync with the terminal, growing it by `$menu.count` lines and breaking all future applications that run in that console session. 🙂
Author
Owner

@DHowett commented on GitHub (Apr 4, 2025):

You can trivially reproduce this in the original Windows Console by setting the buffer and the viewport to be the same height:

Image

This is a common configuration that people use, and in which your script does not work.

@DHowett commented on GitHub (Apr 4, 2025): You can trivially reproduce this in the original Windows Console by setting the buffer and the viewport to be the same height: <img width="302" alt="Image" src="https://github.com/user-attachments/assets/266d4cba-0143-4c11-9cb6-badfab22844d" /> This is a common configuration that people use, and in which your script does not work.
Author
Owner

@farag2 commented on GitHub (Apr 5, 2025):

@DHowett , I'm not a pro in PS. :)
What do you think about new Show-menu function?

@farag2 commented on GitHub (Apr 5, 2025): @DHowett , I'm not a pro in PS. :) What do you think about new `Show-menu` function?
Author
Owner

@DHowett commented on GitHub (Apr 30, 2025):

What do you think about new Show-menu function?

It looks good! Especially if it works well. 😄

@DHowett commented on GitHub (Apr 30, 2025): > What do you think about new Show-menu function? It looks good! Especially if it works well. 😄
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: starred/terminal#23094