🖥️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
Screen
InstanceYou 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 bufferAddTextLine()
: Adds a simple and static text to the buffer with the extra new lineAddDynamicText()
: 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.
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.
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.
Don't forget to remove your screen from the list of tracked screens once you're done using it. You can call UnsetCurrent()
to make this happen.
If your application contains a main loop wrapped in a try
and catch
block, you must create a finally
block (if not done) and call UnsetCurrent()
on your screen instance. It may be necessary to move your screen instance declaration in your code outside the try
and catch
block.
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
//
// 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