A different take on Clearing a terminal screen...

Started by airr, July 08, 2024, 06:52:25 PM

Previous topic - Next topic

airr

The CLS() function has one downside of sorts in my opinion.

While it clears the screen, it doesn't seem to clear the scrollback buffer.

So if you scroll up in the terminal window, whatever was there before the call to CLS() is still there.

Sure, we can use SYSTEM("cls") but that is potentially dangerous and isn't really an option.

I tried a bunch of different implementations, from Microsoft themselves to ChatGPT:  NONE cleared the scrollback buffer even though the code comments said that it would.  CONIO's clrscr() also didn't clear the buffer.

I recalled facing a similar issue with MBC (my old BCX port to macOS) which I was able to resolve back then.

With MBC, I resorted to using ANSI escape sequences to achieve clearing both the screen AND the scrollback buffer.

With nothing to lose, I tried it with BCX on a Win11 machine, with both MSVC and Pelles.  It worked!

It's really simple (although you may temporarily lose your vision if you stare at it for to long!), here's the function I used:


Sub ClearScreen()
    !printf("\33[1;1H\33[2J\33[3J");
End Sub


What I DON'T know is if it works on older versions of Windows. I only tested on Win11.

AIR.

Quin

Cool trick! Can confirm it works on Windows 10 and 11 IoT lTSC at least.
-Quin.
GitHub

MrBcx

#2
The forthcoming BCX v8.1.2 will have a revised CLS runtime that clears the scroll back buffer.

Tested on Windows 11.  It seems to work as it should.

Feel free to test on other versions of Windows by swapping out the CLS runtime with this one.



void cls(void)
{
    DWORD cCharsWritten;
    COORD coordScreen = { 0, 0 };
    CONSOLE_SCREEN_BUFFER_INFO csbi;
    DWORD dwConSize;
    WORD attr;

    // Get current screen buffer info
    GetConsoleScreenBufferInfo(hConsole, &csbi);

    // Calculate the console screen size
    dwConSize = csbi.dwSize.X * csbi.dwSize.Y;

    // Fill the console with spaces
    FillConsoleOutputCharacter(hConsole, (TCHAR)' ', dwConSize, coordScreen, &cCharsWritten);

    // Fill the console with the current text and background colors
    attr = (WORD)color_fg + (WORD)color_bg * 16;
    FillConsoleOutputAttribute(hConsole, attr, dwConSize, coordScreen, &cCharsWritten);

    SMALL_RECT rect;
    rect.Left = 0;
    rect.Top = 0;
    rect.Right = csbi.dwSize.X - 1;
    rect.Bottom = csbi.dwSize.Y - 1;
    // Clear the scrollback buffer by resizing the console buffer
    SetConsoleWindowInfo(hConsole, TRUE, &rect);
    SetConsoleScreenBufferSize(hConsole, csbi.dwSize);
    locate(1, 1, 1);
}


Quin

Kevin,
Just tested that version on all my machines, and it seems to work like it should!
Nice work, and looking forward to 8.1.2 :)
-Quin.
GitHub

MrBcx

Quote from: Quin on August 01, 2024, 12:43:52 PM
Kevin,
Just tested that version on all my machines, and it seems to work like it should!
Nice work, and looking forward to 8.1.2 :)

Thank you Quin .. that's good to know.

I updated the code above to overcome a bug with the Lcc-Win32 compiler.
I needed to code a verbose initialization of the SMALL_RECT rect.  I'm likely
the only person on the planet that pays any attention to Lcc-Win32 anymore.


Quin

You may be right about LCC,I haven't paid it any thought until just now.
It seems like a cool product, I just have never used it. How optimized/small are its executables?
-Quin.
GitHub

MrBcx

Quote from: Quin on August 01, 2024, 03:51:22 PM
You may be right about LCC,I haven't paid it any thought until just now.
It seems like a cool product, I just have never used it. How optimized/small are its executables?

Size-wise, Lcc-Win32's exe's and dll's compare closely with Pelles C but that's where the similarities stop.

Lcc-Win32 is a fast-compiler that produces the slowest executables of any c/c++ compiler.

Lcc-Win32 and Pelles C derived from:

https://htmlpreview.github.io/?https://raw.githubusercontent.com/drh/lcc/master/doc/install.html