feat: Enable transience for Cmd and PowerShell (#4143)

This commit is contained in:
Rashil Gandhi
2022-07-25 07:40:40 +05:30
committed by GitHub
parent 1b65a7bb77
commit 6e9c013e60
3 changed files with 120 additions and 2 deletions
+12
View File
@@ -49,6 +49,18 @@ function starship_prompt:rightfilter(prompt)
):read("*a")
end
if starship_transient_prompt_func ~= nil then
function starship_prompt:transientfilter(prompt)
return starship_transient_prompt_func(prompt)
end
end
if starship_transient_rprompt_func ~= nil then
function starship_prompt:transientrightfilter(prompt)
return starship_transient_rprompt_func(prompt)
end
end
local characterset = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"
local randomkey = ""
math.randomseed(os.time())
+47 -2
View File
@@ -64,6 +64,36 @@ $null = New-Module starship {
$process.StandardOutput.ReadToEnd();
}
function Enable-TransientPrompt {
Set-PSReadLineKeyHandler -Key Enter -ScriptBlock {
$previousOutputEncoding = [Console]::OutputEncoding
try {
$parseErrors = $null
[Microsoft.PowerShell.PSConsoleReadLine]::GetBufferState([ref]$null, [ref]$null, [ref]$parseErrors, [ref]$null)
if ($parseErrors.Count -eq 0) {
$script:TransientPrompt = $true
[Console]::OutputEncoding = [Text.Encoding]::UTF8
[Microsoft.PowerShell.PSConsoleReadLine]::InvokePrompt()
}
} finally {
if ($script:DoesUseLists) {
# If PSReadline is set to display suggestion list, this workaround is needed to clear the buffer below
# before accepting the current commandline. The max amount of items in the list is 10, so 12 lines
# are cleared (10 + 1 more for the prompt + 1 more for current commandline).
[Microsoft.PowerShell.PSConsoleReadLine]::Insert("`n" * [math]::Min($Host.UI.RawUI.WindowSize.Height - $Host.UI.RawUI.CursorPosition.Y - 1, 12))
[Microsoft.PowerShell.PSConsoleReadLine]::Undo()
}
[Microsoft.PowerShell.PSConsoleReadLine]::AcceptLine()
[Console]::OutputEncoding = $previousOutputEncoding
}
}
}
function Disable-TransientPrompt {
Set-PSReadLineKeyHandler -Key Enter -Function AcceptLine
$script:TransientPrompt = $false
}
function global:prompt {
$origDollarQuestion = $global:?
$origLastExitCode = $global:LASTEXITCODE
@@ -106,7 +136,16 @@ $null = New-Module starship {
$arguments += "--status=$($lastExitCodeForPrompt)"
# Invoke Starship
$promptText = Invoke-Native -Executable ::STARSHIP:: -Arguments $arguments
$promptText = if ($script:TransientPrompt) {
$script:TransientPrompt = $false
if (Test-Path function:Invoke-Starship-TransientFunction) {
Invoke-Starship-TransientFunction
} else {
"$([char]0x1B)[1;32m$([char]0x1B)[0m "
}
} else {
Invoke-Native -Executable ::STARSHIP:: -Arguments $arguments
}
# Set the number of extra lines in the prompt for PSReadLine prompt redraw.
Set-PSReadLineOption -ExtraPromptLineCount ($promptText.Split("`n").Length - 1)
@@ -141,6 +180,9 @@ $null = New-Module starship {
# Disable virtualenv prompt, it breaks starship
$ENV:VIRTUAL_ENV_DISABLE_PROMPT=1
$script:TransientPrompt = $false
$script:DoesUseLists = (Get-PSReadLineOption).PredictionViewStyle -eq 'ListView'
if ($PSVersionTable.PSVersion.Major -gt 5) {
$ENV:STARSHIP_SHELL = "pwsh"
} else {
@@ -158,5 +200,8 @@ $null = New-Module starship {
)
)
Export-ModuleMember
Export-ModuleMember -Function @(
"Enable-TransientPrompt"
"Disable-TransientPrompt"
)
}