Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
farag2 opened this issue Mar 14, 2023 · 4 comments
Closed
Labels
Issue-Bug It either shouldn't be doing this or needs an investigation. Needs-Attention The core contributors need to come back around and look at this ASAP. Needs-Triage It's a new issue that the core contributor team needs to triage at the next triage meeting

Comments

@farag2
Copy link

farag2 commented 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

bug.mp4

Bare powershell.exe (5.1)

notbugged.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

@farag2 farag2 added Issue-Bug It either shouldn't be doing this or needs an investigation. Needs-Triage It's a new issue that the core contributor team needs to triage at the next triage meeting labels Mar 14, 2023
@zadjii-msft
Copy link
Member

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.

@microsoft-github-policy-service microsoft-github-policy-service bot added the Needs-Author-Feedback The original author of the issue/PR needs to come back and respond to something label Mar 14, 2023
@farag2
Copy link
Author

farag2 commented Mar 14, 2023

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

2023-03-14.223640.mp4

@microsoft-github-policy-service microsoft-github-policy-service bot added Needs-Attention The core contributors need to come back around and look at this ASAP. and removed Needs-Author-Feedback The original author of the issue/PR needs to come back and respond to something labels Mar 14, 2023
@lhecker
Copy link
Member

lhecker commented 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.

@farag2
Copy link
Author

farag2 commented 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 farag2 closed this as completed Mar 15, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Issue-Bug It either shouldn't be doing this or needs an investigation. Needs-Attention The core contributors need to come back around and look at this ASAP. Needs-Triage It's a new issue that the core contributor team needs to triage at the next triage meeting
Projects
None yet
Development

No branches or pull requests

3 participants