\n doesn't work when it's the last character of the line and DISABLE_NEWLINE_AUTO_RETURN is enabled #5795

Closed
opened 2026-01-31 00:21:53 +00:00 by claunia · 7 comments
Owner

Originally created by @joaobzrr on GitHub (Jan 7, 2020).

With DISABLE_NEWLINE_AUTO_RETURN enabled, if I write a full line and try to write a newline character at the end the cursor just moves down (picture below). How can I get the cursor to go to the beginning of the next line as usual?

actual

Originally created by @joaobzrr on GitHub (Jan 7, 2020). With DISABLE_NEWLINE_AUTO_RETURN enabled, if I write a full line and try to write a newline character at the end the cursor just moves down (picture below). How can I get the cursor to go to the beginning of the next line as usual? ![actual](https://user-images.githubusercontent.com/43659438/71858871-8a990580-30cb-11ea-9e00-adc0cf9ff0d7.png)
Author
Owner

@j4james commented on GitHub (Jan 7, 2020):

What you're describing is exactly how it's supposed to work. The DISABLE_NEWLINE_AUTO_RETURN flag prevents a newline (\n) from also forcing a carriage return (\r). If you want \n to move the cursor to the beginning of the line, then simply don't set the DISABLE_NEWLINE_AUTO_RETURN flag.

If you're confused by the documentation of DISABLE_NEWLINE_AUTO_RETURN on the SetConsoleMode page, that's because it's wrong. The delayed wrapping behaviour described in that section is triggered automatically by the ENABLE_VIRTUAL_TERMINAL_PROCESSING flag.

@j4james commented on GitHub (Jan 7, 2020): What you're describing is exactly how it's supposed to work. The `DISABLE_NEWLINE_AUTO_RETURN` flag prevents a newline (`\n`) from also forcing a carriage return (`\r`). If you want `\n` to move the cursor to the beginning of the line, then simply don't set the `DISABLE_NEWLINE_AUTO_RETURN` flag. If you're confused by the documentation of `DISABLE_NEWLINE_AUTO_RETURN` on the [SetConsoleMode](https://docs.microsoft.com/en-us/windows/console/setconsolemode) page, that's because it's wrong. The delayed wrapping behaviour described in that section is triggered automatically by the `ENABLE_VIRTUAL_TERMINAL_PROCESSING` flag.
Author
Owner

@joaobzrr commented on GitHub (Jan 7, 2020):

Problem is I want to keep the behavior of DISABLE_NEWLINE_AUTO_RETURN to avoid having to scroll the buffer back up when I write a character at the end of the last line (the one at the very bottom of the window). DISABLE_NEWLINE_AUTO_RETURN fixes this problem but introduces a new one, i.e., the one I described above.

@joaobzrr commented on GitHub (Jan 7, 2020): Problem is I want to keep the behavior of `DISABLE_NEWLINE_AUTO_RETURN` to avoid having to scroll the buffer back up when I write a character at the end of the last line (the one at the very bottom of the window). `DISABLE_NEWLINE_AUTO_RETURN` fixes this problem but introduces a new one, i.e., the one I described above.
Author
Owner

@j4james commented on GitHub (Jan 7, 2020):

DISABLE_NEWLINE_AUTO_RETURN fixes this problem

What makes you think that? It certainly isn't meant to have that effect.

If you want to avoid scrolling when writing to the bottom right corner of the screen, you need to set the ENABLE_VIRTUAL_TERMINAL_PROCESSING flag. Or you could unset the ENABLE_WRAP_AT_EOL_OUTPUT flag, but that obviously means the cursor won't wrap on any line, which may not be what you want.

@j4james commented on GitHub (Jan 7, 2020): > `DISABLE_NEWLINE_AUTO_RETURN` fixes this problem What makes you think that? It certainly isn't meant to have that effect. If you want to avoid scrolling when writing to the bottom right corner of the screen, you need to set the `ENABLE_VIRTUAL_TERMINAL_PROCESSING` flag. Or you could _unset_ the `ENABLE_WRAP_AT_EOL_OUTPUT` flag, but that obviously means the cursor won't wrap on any line, which may not be what you want.
Author
Owner

@zadjii-msft commented on GitHub (Jan 7, 2020):

@x1bzzr Do @j4james's suggestions work for you? He's correct that this is the intended behavior of DISABLE_NEWLINE_AUTO_RETURN

@zadjii-msft commented on GitHub (Jan 7, 2020): @x1bzzr Do @j4james's suggestions work for you? He's correct that this is the intended behavior of `DISABLE_NEWLINE_AUTO_RETURN`
Author
Owner

@joaobzrr commented on GitHub (Jan 7, 2020):

Well, no. I'm still looking for a way to write an entire screen worth of text to the console without having it scroll down after writing the last character in the line at the bottom of the window. If I enable DISABLE_NEWLINE_AUTO_RETURN I get the desired behavior (cursor doesn't go to next line after writing the last character) but I run into another problem which is that when I try to write an entire screen worth of text (meaning characters all the way to the last column) and write a newline at the very end of each line the cursor goes down to the next line but does not return to the beginning (first position in the line).

So I'm wondering if it's possible at all to fill an entire screen with characters without:

  1. Having the buffer scroll down one line after writing the last character.
  2. Disabling ENABLE_WRAP_AT_EOL_OUTPUT.
  3. Disabling ENABLE_VIRTUAL_TERMINAL_PROCESSING.

I also would like to note that simply enabling ENABLE_VIRTUAL_TERMINAL_PROCESSING doesn't seem to stop the screen from scrolling down after writing to the bottom right corner of the screen you suggested, @j4james. If possible, could you provide some example code that demonstrates this?

@joaobzrr commented on GitHub (Jan 7, 2020): Well, no. I'm still looking for a way to write an entire screen worth of text to the console without having it scroll down after writing the last character in the line at the bottom of the window. If I enable `DISABLE_NEWLINE_AUTO_RETURN` I get the desired behavior (cursor doesn't go to next line after writing the last character) but I run into another problem which is that when I try to write an entire screen worth of text (meaning characters all the way to the last column) and write a newline at the very end of each line the cursor goes down to the next line but does not return to the beginning (first position in the line). So I'm wondering if it's possible at all to fill an entire screen with characters without: 1. Having the buffer scroll down one line after writing the last character. 2. Disabling `ENABLE_WRAP_AT_EOL_OUTPUT`. 3. Disabling `ENABLE_VIRTUAL_TERMINAL_PROCESSING`. I also would like to note that simply enabling `ENABLE_VIRTUAL_TERMINAL_PROCESSING` doesn't seem to stop the screen from scrolling down after writing to the bottom right corner of the screen you suggested, @j4james. If possible, could you provide some example code that demonstrates this?
Author
Owner

@j4james commented on GitHub (Jan 7, 2020):

Here's an example that fills the screen by writing out W*H characters.

#include <windows.h>
#include <stdio.h>

int main()
{
    auto handle = GetStdHandle(STD_OUTPUT_HANDLE);

    // Enable VT mode.
    DWORD mode;
    GetConsoleMode(handle, &mode);
    mode |= ENABLE_VIRTUAL_TERMINAL_PROCESSING;
    SetConsoleMode(handle, mode);

    // Get the window dimensions.
    CONSOLE_SCREEN_BUFFER_INFO csbi;
    GetConsoleScreenBufferInfo(handle, &csbi);
    auto w = csbi.srWindow.Right - csbi.srWindow.Left + 1;
    auto h = csbi.srWindow.Bottom - csbi.srWindow.Top + 1;

    // Move the cursor to the top left of the screen.
    auto x = csbi.srWindow.Left;
    auto y = csbi.srWindow.Top;
    SetConsoleCursorPosition(handle, COORD{ x, y });

    // Fill the screen with text.
    for (auto row = 0; row < h; row++)
    {
        auto c = 'A' + row;
        for (auto col = 0; col < w; col++)
        {
            printf("%c", c);
        }
    }

    // Move the cursor back to the top left.
    SetConsoleCursorPosition(handle, COORD{ x, y });
}
@j4james commented on GitHub (Jan 7, 2020): Here's an example that fills the screen by writing out W*H characters. ```cpp #include <windows.h> #include <stdio.h> int main() { auto handle = GetStdHandle(STD_OUTPUT_HANDLE); // Enable VT mode. DWORD mode; GetConsoleMode(handle, &mode); mode |= ENABLE_VIRTUAL_TERMINAL_PROCESSING; SetConsoleMode(handle, mode); // Get the window dimensions. CONSOLE_SCREEN_BUFFER_INFO csbi; GetConsoleScreenBufferInfo(handle, &csbi); auto w = csbi.srWindow.Right - csbi.srWindow.Left + 1; auto h = csbi.srWindow.Bottom - csbi.srWindow.Top + 1; // Move the cursor to the top left of the screen. auto x = csbi.srWindow.Left; auto y = csbi.srWindow.Top; SetConsoleCursorPosition(handle, COORD{ x, y }); // Fill the screen with text. for (auto row = 0; row < h; row++) { auto c = 'A' + row; for (auto col = 0; col < w; col++) { printf("%c", c); } } // Move the cursor back to the top left. SetConsoleCursorPosition(handle, COORD{ x, y }); } ```
Author
Owner

@joaobzrr commented on GitHub (Jan 7, 2020):

@j4james You're right, I was outputting a newline after writing the last character. Sorry for wasting your time.

@joaobzrr commented on GitHub (Jan 7, 2020): @j4james You're right, I was outputting a newline after writing the last character. Sorry for wasting your time.
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: starred/terminal#5795