-
Notifications
You must be signed in to change notification settings - Fork 38
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
The shell prompt get offsets every time an item was accepted #202
Comments
I don't really think there is a fix for this. Every time you hit See the below function for the redrawn prompt. Lines 122 to 135 in 0364b14
|
Thanks for your explain and so we have to wait the official team of psreadline to fix :(
|
Well maybe. You also have a two lined prompt which is different than mine. Are you using Version |
I'm not sure if this made a difference so I tested again with command (The Hyper Terminal gif is too big somehow, so I didn't upload it but it's almost the same result
Yes, I am using
I am a Chinese and '空格' means 'Space' key, that's probably not the problem
Version |
Looks like our version are matching well except I am Win 10 22H2 running single line That gif looks like it is working like it should. No? Even your original gif didn't appear all that off. The problem is when your prompt gets to the bottom and has to "jump"? |
Exactly, it won't work as expected when the prompt stays at the bottom of terminal. |
Ah ok, so you don't want all that white space generating as you go in and out of Hmm. I wonder if this module can hook into some class in |
Sorry but I do know little about PowerShell and I am not a developer :( Just let me know if you need help or else. |
Any updates on this? I'm experiencing this on macOS pwsh if I can help debug |
I don't even remember what the issue really was. I was trying to read through and I can't tell if OP wants the prompt line to stay in place when |
I've been following this issue for a while, since I have a very similar bug which has led me to removing the recommended tab expansion key handler Subsequent calls to This also does not appear to be an issue with the Ctrl-r binding for selecting command history. PSVersion: I can also confirm that I can reproduce this in both my personal config and when using
Here is the comparison between PSFzf and the Zsh plugin Aloxaf/fzf-tab:
|
Hmm. I wonder if using straight |
I haven't ever noticed this issue being present with direct calls to With my testing, It seems the issue occurs more accurately when the prompt is repositioned to accommodate the
I agree - ideally, the prompt should inherit the new position, rather than returning to its previous one. But if that information is not exposed, then I'm even more unsure as to how this can be solved. |
Hey, guys, I've solved it. It is because PSReadline will resotre the cursor position after invoke key handler. However, when the cursor position is too low, there is not enough buffer to display the fzf ui, thus terminal will increases the buffer and cursor position will be shifted up certain lines, and this behavior cause the original cursor positoin which cached by RSReadline be wrong. To fix this, calculate the right original cursor position and then resotre it. $env:FZF_DEFAULT_OPTS = "--height=50% --layout=reverse"
$env:FZF_CTRL_T_COMMAND = "fd --type f --strip-cwd-prefix --hidden --follow --exclude .git"
$env:FZF_ALT_C_COMMAND = "fd --type d --strip-cwd-prefix --hidden --follow --exclude .git"
# Refer to https://github.com/kelleyma49/PSFzf/issues/202#issuecomment-2495321758
function SetCursorPostion {
param(
[int]$MinHeight = 3
)
$rawUI = (Get-Host).UI.RawUI
$matchs = [regex]::Matches($env:FZF_DEFAULT_OPTS, '--height=~?(\d+)(%?)').Groups
if ($null -eq $matchs) {
# if no --height option, set height to full screen
$fzfHeight = $rawUI.BufferSize.Height
} elseif ($matchs[2].Length -eq 0) {
# if set --height without %, set as the fixed height but at least 6
$fzfHeight = [int]$matchs[1].Value
if ($fzfHeight -lt $MinHeight) {
$fzfHeight = $MinHeight
}
} else {
# if set --height with %, we need to considar --min-height
$fzfHeight = [Math]::Truncate(($rawUI.BufferSize.Height - 1) * [int]$matchs[1].Value / 100)
$fzfMinHeight = [regex]::Matches($env:FZF_DEFAULT_OPTS, '--min-height=(\d+)').Groups
if ($null -eq $fzfMinHeight) {
$fzfMinHeight = 10
} elseif ([int]$fzfMinHeight[1].Value -lt $MinHeight) {
$fzfMinHeight = $MinHeight
} else {
$fzfMinHeight = [int]$fzfMinHeight[1].Value
}
if ($fzfHeight -lt $fzfMinHeight) {
$fzfHeight = $fzfMinHeight
}
}
$Global:RepairedCursorPosition = $rawUI.CursorPosition
if ($Global:RepairedCursorPosition.Y -ge ($rawUI.BufferSize.Height - $FzfHeight)) {
# If the curosr position is too low to display Fzf UI, the prompt line will be shifted
$Global:RepairedCursorPosition.Y = $rawUI.BufferSize.Height - $FzfHeight
$Global:RepairedCursorPosition.X = 0
} else {
$Global:RepairedCursorPosition = $null
}
}
Set-PSReadLineKeyHandler -ViMode Insert -Key Ctrl+t -ScriptBlock {
SetCursorPostion
Invoke-FzfPsReadlineHandlerProvider
}
Set-PSReadLineKeyHandler -ViMode Insert -Key Ctrl+r -ScriptBlock {
SetCursorPostion
Invoke-FzfPsReadlineHandlerHistory
}
Set-PSReadLineKeyHandler -ViMode Insert -Key Alt-c -ScriptBlock {
SetCursorPostion
Invoke-FzfPsReadlineHandlerSetLocation
}
Set-PSReadLineKeyHandler -ViMode Insert -Key Alt-a -ScriptBlock {
SetCursorPostion
Invoke-FzfPsReadlineHandlerHistoryArgs
}
Set-PSReadLineKeyHandler -ViMode Insert -Key Tab -ScriptBlock {
SetCursorPostion 6
Invoke-FzfTabCompletion
}
$Global:__OriginalPrompt = $function:Prompt
function prompt {
# place at beginning of function to avoid oh-my-posh get the wrong last error code
$out += $Global:__OriginalPrompt.Invoke();
$out = "`e]133;A$([char]07)" + $out;
$out += "`e]133;B$([char]07)";
if ($Global:PsReadLineViMode -eq "i") {
$out += $PsReadLineViInsertModeCursor
} else {
$out += $PsReadLineViNormalModeCursor
}
if ($null -ne $Global:RepairedCursorPosition) {
$promptHeight = $out.Split("`n").Count
$Global:RepairedCursorPosition.Y -= $promptHeight
(Get-Host).UI.RawUI.CursorPosition = $Global:RepairedCursorPosition
$Global:RepairedCursorPosition = $null
}
return $out
} |
Here is the final edition $env:FZF_DEFAULT_OPTS = "--height=50% --min-height=8 --layout=reverse --info=right --scrollbar='▐'"
$env:FZF_CTRL_T_COMMAND = "fd --type f --strip-cwd-prefix --hidden --follow --exclude .git"
$env:FZF_ALT_C_COMMAND = "fd --type d --strip-cwd-prefix --hidden --follow --exclude .git"
# Refer to https://github.com/kelleyma49/PSFzf/issues/202#issuecomment-2495321758
function SetCursorPostion {
param(
[int]$MinHeight = 3
)
$rawUI = (Get-Host).UI.RawUI
$matchs = [regex]::Matches($env:FZF_DEFAULT_OPTS, '--height=~?(\d+)(%?)').Groups
if ($null -eq $matchs) {
# if no --height option, set height to full screen
$fzfHeight = $rawUI.BufferSize.Height
} elseif ($matchs[2].Length -eq 0) {
# if set --height without %, set as the fixed height but at least 3
$fzfHeight = [Math]::Max([int]$matchs[1].Value, $MinHeight)
} else {
# if set --height with %, we need to considar --min-height
$fzfMinHeight = [regex]::Matches($env:FZF_DEFAULT_OPTS, '--min-height=(\d+)').Groups
if ($null -eq $fzfMinHeight) {
$fzfMinHeight = 10
} else {
$fzfMinHeight = [Math]::Max([int]$fzfMinHeight[1].Value, $MinHeight)
}
$fzfHeight = [Math]::Max([Math]::Truncate(($rawUI.BufferSize.Height - 1) * [int]$matchs[1].Value / 100), $fzfMinHeight)
}
$Global:PositionBeforePsfzf = $rawUI.CursorPosition
$Global:RepairedCursorPosition = $rawUI.CursorPosition
if ($Global:RepairedCursorPosition.Y -ge ($rawUI.BufferSize.Height - $FzfHeight)) {
# If the curosr position is too low to display Fzf UI, the prompt line will be shifted
$Global:RepairedCursorPosition.Y = $rawUI.BufferSize.Height - $FzfHeight - 1
}
}
Set-PSReadLineKeyHandler -ViMode Insert -Key Ctrl+t -ScriptBlock {
SetCursorPostion
Invoke-FzfPsReadlineHandlerProvider
$Global:RepairedCursorPosition = $null
}
Set-PSReadLineKeyHandler -ViMode Insert -Key Ctrl+r -ScriptBlock {
SetCursorPostion
Invoke-FzfPsReadlineHandlerHistory
$Global:RepairedCursorPosition = $null
}
Set-PSReadLineKeyHandler -ViMode Insert -Key Alt-c -ScriptBlock {
SetCursorPostion
Invoke-FzfPsReadlineHandlerSetLocation
$Global:RepairedCursorPosition = $null
}
Set-PSReadLineKeyHandler -ViMode Insert -Key Alt-a -ScriptBlock {
SetCursorPostion
Invoke-FzfPsReadlineHandlerHistoryArgs
$Global:RepairedCursorPosition = $null
}
Set-PSReadLineKeyHandler -ViMode Insert -Key Tab -ScriptBlock {
SetCursorPostion
Invoke-FzfTabCompletion
$Global:RepairedCursorPosition = $null
}
$Global:__OriginalPrompt = $function:Prompt
function prompt {
# Place at beginning of function to avoid oh-my-posh get the wrong last error code
$out = $Global:__OriginalPrompt.Invoke()[0];
# Transient prompt
if ($out[-1] -eq "`b") {
return $out
}
# Replace \n to \r\n to avoid some bug
# $out = $out -replace "`n", "`r`n"
$out = $out -replace "`n", "`r`n"
# Reset the cursor to the correct shape
if ($Global:PsReadLineViMode -eq "i") {
$out += $PsReadLineViInsertModeCursor
} else {
$out += $PsReadLineViNormalModeCursor
}
# Repair the cursor position after PSReadline key handler
if ($null -ne $Global:RepairedCursorPosition) {
$rawUI = (Get-Host).UI.RawUI
$promptAndCommandHeight = $Global:PositionBeforePsfzf.Y - $rawUI.CursorPosition.Y
$Global:RepairedCursorPosition.X = 0
$Global:RepairedCursorPosition.Y -= $promptAndCommandHeight
$rawUI.CursorPosition = $Global:RepairedCursorPosition
}
return $out
}
|
@mrbeardad Thanks for the great work! |
For now, the only way to do is edit the PSFzf.psd1. On windows, it places at # Functions to export from this module, for best performance, do not use wildcards and do not delete the entry, use an empty array if there are no functions to export.
FunctionsToExport = 'Enable-PsFzfAliases', 'Invoke-FuzzyEdit', 'Invoke-FuzzyFasd',
'Invoke-FuzzyGitStatus', 'Invoke-FuzzyHistory',
'Invoke-FuzzyKillProcess', 'Invoke-FuzzyScoop',
'Invoke-FuzzySetLocation', 'Invoke-FuzzyZLocation', 'Invoke-Fzf',
'Invoke-FzfTabCompletion', 'Invoke-PsFzfGitBranches',
'Invoke-PsFzfGitFiles', 'Invoke-PsFzfGitHashes',
'Invoke-PsFzfGitStashes', 'Invoke-PsFzfGitTags',
'Invoke-PsFzfRipgrep', 'Set-LocationFuzzyEverything',
'Set-PsFzfOption', 'Invoke-FzfPsReadlineHandlerProvider',
'Invoke-FzfPsReadlineHandlerHistory', 'Invoke-FzfPsReadlineHandlerSetLocation',
'Invoke-FzfPsReadlineHandlerHistoryArgs', 'Invoke-TabCompletion' |
@mrbeardad Ok, thanks! |
Hi all, I've found a fix with a single line of change diff --git a/PSFzf.Base.ps1 b/PSFzf.Base.ps1
index 332ca90..e611cd4 100644
--- a/PSFzf.Base.ps1
+++ b/PSFzf.Base.ps1
@@ -128,7 +128,7 @@ function InvokePromptHack()
[Console]::OutputEncoding = [Text.Encoding]::UTF8
try {
- [Microsoft.PowerShell.PSConsoleReadLine]::InvokePrompt()
+ [Microsoft.PowerShell.PSConsoleReadLine]::InvokePrompt($null, [Console]::CursorTop)
} finally {
[Console]::OutputEncoding = $previousOutputEncoding |
Great job, but it is better to consider the prompt and command line height, for example, |
It's happening months ago when I started to use PSFzf, I don't know what's going on behind the scene and it just didn't behave like expected, because when the prompt stays at the bottom of terminal, it got offset each time an item is accepted, looks weird.
I didn't see a related issue here. (If there was a disscussion, please let me know : )
The text was updated successfully, but these errors were encountered: