Skip to content

API to get prompt's *starting* Y coordinate, for external tab-completers that must scroll terminal and thus relocate prompt position #5026

@cspotcode

Description

@cspotcode

Prerequisites

  • Write a descriptive title.

Description of the new feature/enhancement

PSFzf exposes an alternative to MenuComplete using fzf's fuzzy matching menu. Just like MenuComplete, fzf's menu requires X lines of space beneath the prompt, so it may scroll the terminal upward when activated. When it returns, PSReadLine needs to be informed that the prompt has moved upward to a different Y position.

When the prompt is multiple lines long, this Y position is not the same as (Get-Host).UI.RawUI.CursorPosition. RawUI shows us the end of the prompt, but not the beginning. The beginning is the value which must be passed to InvokePrompt.

Related issues:
kelleyma49/PSFzf#202 (comment)
kelleyma49/PSFzf#227

Proposed technical implementation details (optional)

There are hacks to compute the prompt height, by overriding function prompt to capture the Y coordinate before the prompt is rendered. But the correct solution is for PSReadLine to expose either:

  • GetPromptStartPosition to get the Y coordinate of the prompt, so fzf can subtract the number of lines it scrolled the terminal and pass the result to InvokePrompt
  • MovePromptYPosition if you really don't want to expose the above. This would accept a positive or negative number and add it to the prompt's Y position.

Pseudo-code to show how this might work:

Set-PSReadLineKeyHandler -Key Tab -ScriptBlock {
  $cursor_y = (Get-Host).UI.RawUI.CursorPosition.Y # end of prompt, to account for multi-line prompts
  $prompt_y = [PSReadline]::GetPromptStartPosition().Y # start of prompt
  InvokeFzfMenu # This may scroll the terminal to make space for the menu, and will thus restore cursor position to a new (higher) Y coordinate when finished
  # How much did cursor position move?  Add this delta to prompt's start position
  $new_prompt_y = $prompt_y + (Get-Host).UI.RawUI.CursorPosition.Y - $cursor_y
  [PSReadline]::InvokePrompt($null, $new_prompt_y)
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    Issue-EnhancementIt's a feature request.Needs-Triage 🔍It's a new issue that core contributor team needs to triage.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions