1

Figure 1

Figure 1: the bottom portion is to scroll, while the top portion is to stay put.

So I'm writing a CLI .NET Core application.

The top portion of the image is to stay put whereas the bottom portion is to scroll under the top portion.

If there was a more direct access to the console, it would be rather easy. But as we are in C#, no such (cross-platform) access exists that I am aware of.

So, how would I achieve the desired goal here, without platform-locking?

austanss
  • 271
  • 3
  • 9
  • There is not a built-in way to do that (although there may be a library of some sort). Depending on what you're using for to handle your CLI (check out https://github.com/dotnet/command-line-api/wiki), you can likely integrate something like that with just keeping track of cursor position. While the banner is cool, it may be worthwhile mentioning that most CLI users will find this behavior unexpected, and therefore unwanted, imho. – CoolBots Sep 08 '20 at 00:37

1 Answers1

3

You can try this if it might give you an idea. Based on this post C# Console : How to make the console stop scrolling automatically?.

static readonly int maxLine = 10;
static int currentLine = 0;

static void Main(string[] args)
{
    setHeader();

    while (true)
    {
        writeLine("Please type and enter:");
        string value = Console.ReadLine();
        writeLine($"Your input is: {value}");
        writeLine($"Current Line: {currentLine}");

        if (currentLine > maxLine)
        {
            resetCursorPosition();
            writeLine($"Your input is: {value}");
            writeLine($"Current Line: {currentLine}");
        }
    }
}

private static void writeLine(string value)
{
    Console.WriteLine(value);
    currentLine = currentLine + 1;
}

private static void resetCursorPosition()
{
    Console.Clear();
    Console.SetCursorPosition(0, 0);
    currentLine = 0;

    setHeader();
}

private static void setHeader()
{
    writeLine("----------------------------------------------");
    writeLine("---------------MY CUSTOM HEADER---------------");
    writeLine("----------------------------------------------");
}

Output:
enter image description here

tontonsevilla
  • 2,649
  • 1
  • 11
  • 18