Search code examples
powershellprompt

Is there a way to place the prompt cursor at the bottom of the console?


In MSDOS, I wrote a prompt (%PROMPT%) that output a reverse color (white) line at the top of the console with information like MSDOS version and time/date. Then the prompt text ($P$G) and cursor moved to the bottom of the console. Standard output color was Cyan while the prompt string itself was white.

I don't care about a "title" since all windows now have titles. I don't care about the color of the output. I do want the prompt to always appear at the bottom of the console (even after clearing the screen or whatever).

I tried this:

function prompt
{
    $windowHeigth = [Console]::WindowHeight
    $windowWidth  = [Console]::WindowWidth
    [Console]::SetCursorPosition(0, $windowHeigth-1)

    "PS $($ExecutionContext.SessionState.Path.CurrentLocation)$('>' * ($NestedPromptLevel + 1)) "
}

This does place the prompt at the bottom of the console, but when I clear the screen it reverts to the default style prompt location. I can't find out how to find the coordinate of the first row of the last line in the console.


Solution

  • After some experimentation this is what I came up with:

    function prompt
    {
        $rows = [Console]::WindowHeight
        $setCursorBottom = "`e[${rows};0H"
        "${setCursorBottom}PS $($executionContext.SessionState.Path.CurrentLocation)$('>' * ($nestedPromptLevel + 1)) "
    }