C# Console CursorTop is not calculated as same as native windows cmd #13766

Closed
opened 2026-01-31 03:51:32 +00:00 by claunia · 7 comments
Owner

Originally created by @MohammadMD1383 on GitHub (May 9, 2021).

Windows Terminal version (or Windows build number)

Windows10 - 20H2 - 19042.964

Other Software

No response

Steps to reproduce

  1. write this program in c#
using System;
using static System.Console;

namespace ConsoleApp
{
	internal static class Program
	{
		public static void Main()
		{
			Write("Hello World");
			ClearLine(CursorTop);
			SetCursorPosition(0, CursorTop - 1); // exception here
		}

		private static void ClearLine(int lineNumber)
		{
			SetCursorPosition(0, lineNumber);
			Write("".PadRight(WindowWidth));
		}
	}
}
  1. compile
  2. run it from windows terminal powershell using .\ConsoleApp.exe

Expected Behavior

it should fill the current line with white space and then because the white spaces count is equal to windows width, after the last white space the cursor should move to next line
then by setting cursor top to cursor top - 1 it shouldn't throw any exception.

I tested this in cmd and powershell and there was no problem.

Actual Behavior

after last white space cursor doesn't go to next line so it throws exception.
image

Originally created by @MohammadMD1383 on GitHub (May 9, 2021). ### Windows Terminal version (or Windows build number) Windows10 - 20H2 - 19042.964 ### Other Software _No response_ ### Steps to reproduce 1. write this program in c# ```csharp using System; using static System.Console; namespace ConsoleApp { internal static class Program { public static void Main() { Write("Hello World"); ClearLine(CursorTop); SetCursorPosition(0, CursorTop - 1); // exception here } private static void ClearLine(int lineNumber) { SetCursorPosition(0, lineNumber); Write("".PadRight(WindowWidth)); } } } ``` 2. compile 3. run it from windows terminal powershell using `.\ConsoleApp.exe` ### Expected Behavior it should fill the current line with white space and then **because the white spaces count is equal to windows width, after the last white space the cursor should move to next line** then by setting cursor top to `cursor top - 1` it shouldn't throw any exception. I tested this in `cmd` and `powershell` and there was no problem. ### Actual Behavior after last white space cursor doesn't go to next line so it throws exception. ![image](https://user-images.githubusercontent.com/69088224/117583427-61074880-b11c-11eb-9385-5d7bef9ee00d.png)
Author
Owner

@LuanVSO commented on GitHub (May 9, 2021):

then by setting cursor top to cursor top - 1 it shouldn't throw any exception.

it should if cursortop ==0

reflow only enters in action if you print WindowWidth+1 [1], if you print only windowWidth the cursor will remain in the same line, and if that line is 0, you will try to decrement it, which will result in -1 which is not a valid line number hence the error.

[1]: if console reflow is actually enabled that is

@LuanVSO commented on GitHub (May 9, 2021): > then by setting cursor top to cursor top - 1 it shouldn't throw any exception. it should if cursortop ==0 reflow only enters in action if you print WindowWidth+1 [1], if you print only windowWidth the cursor will remain in the same line, and if that line is 0, you will try to decrement it, which will result in -1 which is not a valid line number hence the error. [1]: if console reflow is actually enabled that is
Author
Owner

@MohammadMD1383 commented on GitHub (May 10, 2021):

@LuanVSO I tested in cmd and powershell (their own window) and after printing space in count of windowWidth the cursor moved to next line

@MohammadMD1383 commented on GitHub (May 10, 2021): @LuanVSO I tested in cmd and powershell _(their own window)_ and after printing space in count of `windowWidth` the cursor moved to next line
Author
Owner

@LuanVSO commented on GitHub (May 10, 2021):

ok so, for me if i print WindowWidth in windows terminal the line doesn't wrap, but on conhost it does.

@LuanVSO commented on GitHub (May 10, 2021): ok so, for me if i print `WindowWidth` in windows terminal the line doesn't wrap, but on conhost it does.
Author
Owner

@j4james commented on GitHub (May 10, 2021):

This is probably because Windows Terminal has ENABLE_VIRTUAL_TERMINAL_PROCESSING on by default, while conhost has it disabled. So in Windows Terminal you get the delayed wrap effect expected by VT applications, while in conhost the wrap will occur as soon as a character is output in the last column.

@j4james commented on GitHub (May 10, 2021): This is probably because Windows Terminal has `ENABLE_VIRTUAL_TERMINAL_PROCESSING` on by default, while conhost has it disabled. So in Windows Terminal you get the delayed wrap effect expected by VT applications, while in conhost the wrap will occur as soon as a character is output in the last column.
Author
Owner

@DHowett commented on GitHub (May 13, 2021):

Yep, James has the right of it here. For an application that relies on specific console modes being set (sorry- line wrapping behavior unfortunately requires specific modes) our recommendation is that it set those modes on startup.

@DHowett commented on GitHub (May 13, 2021): Yep, James has the right of it here. For an application that relies on specific console modes being set (sorry- line wrapping behavior unfortunately requires specific modes) our recommendation is that it set those modes on startup.
Author
Owner

@DHowett commented on GitHub (May 13, 2021):

OH, And there's a great discussion about this in /dup #8312

@DHowett commented on GitHub (May 13, 2021): *OH*, And there's a great discussion about this in /dup #8312
Author
Owner

@ghost commented on GitHub (May 13, 2021):

Hi! We've identified this issue as a duplicate of another one that already exists on this Issue Tracker. This specific instance is being closed in favor of tracking the concern over on the referenced thread. Thanks for your report!

@ghost commented on GitHub (May 13, 2021): Hi! We've identified this issue as a duplicate of another one that already exists on this Issue Tracker. This specific instance is being closed in favor of tracking the concern over on the referenced thread. Thanks for your report!
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: starred/terminal#13766