🖥️Console Screen

Your screen in front of you

Nitrocid KS offers the console screen feature, which allows you to define a screen for your interactive console application. This guarantees you a dynamic terminal sequence generation that you can print to the console. Usage of the VT sequences, as seen in the Terminaux manual, can be found here:

Screen Instance

The explanation provided here is not exhaustive, but a more detailed explanation can be found in the Terminaux manual here:

Console Screen

You can get started by making a new instance of the Screen class and using it to add a new ScreenPart instance with its name to make a layer for your rendering sequences. This facilitates buffering the screens to the console.

The screen part instance allows you to add text in different ways:

  • AddText(): Adds a simple and static text to the buffer

  • AddTextLine(): Adds a simple and static text to the buffer with the extra new line

  • AddDynamicText(): This is the key of the Screen feature. It allows you to define a function delegate that generates text dynamically.

  • Position(): Adds a VT sequence that changes the position. Works for static text addition.

  • LeftPosition(): Adds a VT sequence that changes the cursor left position. Works for static text addition.

  • TopPosition(): Adds a VT sequence that changes the cursor top position. Works for static text addition.

  • ForegroundColor(): Adds a VT sequence that changes the foreground color. Works for static text addition.

  • BackgroundColor(): Adds a VT sequence that changes the background color. Works for static text addition.

  • ResetColor(): Adds a VT sequence that resets the colors.

  • Clear(): Clears the whole buffer.

  • GetBuffer(): Gets the resulting buffer.

AddDynamicText() is needed if you want to display anything that changes, including a box that changes when the console is resized.

Screen Management

The screen management tools allow you to manipulate with the screen rendering, such as getting the current screen instance, rendering the current screen once, etc.

  • Render() renders the current screen.

  • Render(Screen) renders the specified screen.

In the Render() functions, you can also tell the renderer to clear the screen by passing true to the optional argument, clearScreen.

However, for Render() to work, you need to add your screen instance to the list of tracked screens in the screen manager. This can be done by calling the SetCurrent() function on your screen instance.

When this is done, the screensaver manager and the console resize listener will refresh and redraw your screen, taking new console window dimensions to account. This reaction makes your interactive console applications that use screens responsive to the resize events.

An example

The kernel interactive testing system allows you to try the demonstration of this feature out to show you the concept of what happens when you try to resize the console when the kernel tracks your screen instance.

The screen instance in question shows you two rulers:

  • A horizontal ruler that shows you the width of the console window

  • A vertical ruler that shows you the height of the console window

https://github.com/Aptivi/NitrocidKS/blob/main/public/Nitrocid/Kernel/Debugging/Testing/Facades/TestScreen.cs
//
// Nitrocid KS  Copyright (C) 2018-2025  Aptivi
//
// This file is part of Nitrocid KS
//
// Nitrocid KS is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Nitrocid KS is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY, without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program.  If not, see <https://www.gnu.org/licenses/>.
//

using Terminaux.Base.Buffered;
using Terminaux.Colors.Themes.Colors;
using Terminaux.Inputs.Styles.Infobox;
using Nitrocid.Languages;
using System;
using System.Text;
using Terminaux.Colors;
using Terminaux.Sequences.Builder.Types;
using Terminaux.Base;
using Terminaux.Colors.Data;
using Terminaux.Inputs;
using Terminaux.Base.Extensions;
using Terminaux.Inputs.Styles.Infobox.Tools;

namespace Nitrocid.Kernel.Debugging.Testing.Facades
{
    internal class TestScreen : TestFacade
    {
        public override string TestName => LanguageTools.GetLocalized("NKS_KERNEL_DEBUGGING_TESTFACADES_TESTSCREEN_DESC");
        public override TestSection TestSection => TestSection.ConsoleBase;
        public override void Run()
        {
            // Show the screen measurement sticks
            var stickScreen = new Screen();
            try
            {
                var stickScreenPart = new ScreenPart();
                stickScreenPart.AddDynamicText(() =>
                {
                    var builder = new StringBuilder();
                    builder.Append(
                        ConsolePositioning.RenderChangePosition(0, 1) +
                        ColorTools.RenderSetConsoleColor(new Color(ConsoleColors.Silver), true) +
                        GenerateWidthStick() + GenerateHeightStick() +
                        ColorTools.RenderResetColors()
                    );
                    return builder.ToString();
                });
                stickScreen.AddBufferedPart("Test", stickScreenPart);
                ScreenTools.SetCurrent(stickScreen);
                ScreenTools.Render();
                Input.ReadKey();
            }
            catch (Exception ex)
            {
                InfoBoxModalColor.WriteInfoBoxModal(LanguageTools.GetLocalized("NKS_KERNEL_DEBUGGING_TESTFACADES_TESTSCREEN_FAILED") + $" {ex.Message}", new InfoBoxSettings()
                {
                    ForegroundColor = ThemeColorsTools.GetColor(ThemeColorType.Error)
                });
            }
            finally
            {
                ScreenTools.UnsetCurrent(stickScreen);
            }
        }

        private static string GenerateWidthStick() =>
            new(' ', ConsoleWrapper.WindowWidth);

        private static string GenerateHeightStick()
        {
            var stick = new StringBuilder();
            for (int i = 0; i < ConsoleWrapper.WindowHeight; i++)
            {
                stick.Append(CsiSequences.GenerateCsiCursorPosition(2, i));
                stick.Append(' ');
            }
            return stick.ToString();
        }
    }
}

Last updated