shibayan / Sharprompt

Interactive command-line based application framework for C#
MIT License
761 stars 50 forks source link

Set console prompt cursor position at last line if application writes also messages to console #285

Open abrasat opened 3 months ago

abrasat commented 3 months ago

I have a test application where the Prompt.Select is located in a loop and different actions are performed depending on the actual selection. If the actions also write messages to the console, the Prompt.Select text is displayed somewehere in the midddle of the console messages generated during the execution of the different actions. For the first iteration is all ok, but after that the prompt select text is not at the last line anymore. I tried to call Console.SetCursorPosition(0, Console.CursorTop) before Prompt.Select() , but it does not help....

abrasat commented 3 months ago

@shibayan By making the following change in the code, the Prompt.Select displays the message at the right position, after the application-generated messages:

    public RenderScope(OffscreenBuffer offscreenBuffer, IConsoleDriver consoleDriver, int cursorBottom, int writtenLineCount)
    {
        _offscreenBuffer = offscreenBuffer;
        _consoleDriver = consoleDriver;

        // old
        //_cursorBottom = Math.Min(cursorBottom, _consoleDriver.WindowHeight - 1);
        // new
        _cursorBottom =  cursorBottom;

        _writtenLineCount = Math.Min(writtenLineCount, _consoleDriver.WindowHeight - 1);

        _offscreenBuffer.ClearBuffer();
    }