fix(pwsh): remove prompt cache

resolves #5332
This commit is contained in:
Jan De Dobbeleer 2024-07-23 13:17:17 +02:00 committed by Jan De Dobbeleer
parent 81d79a7e41
commit 7563d5f4ad
5 changed files with 18 additions and 84 deletions

View file

@ -110,11 +110,13 @@ func init() {
printCmd.Flags().StringVar(&command, "command", "", "tooltip command")
printCmd.Flags().BoolVarP(&plain, "plain", "p", false, "plain text output (no ANSI)")
printCmd.Flags().BoolVar(&cleared, "cleared", false, "do we have a clear terminal or not")
printCmd.Flags().BoolVar(&cached, "cached", false, "use a cached prompt")
printCmd.Flags().BoolVar(&eval, "eval", false, "output the prompt for eval")
printCmd.Flags().IntVar(&column, "column", 0, "the column position of the cursor")
// Deprecated flags
// Deprecated flags, keep to not break CLI integration
printCmd.Flags().IntVarP(&status, "error", "e", 0, "last exit code")
printCmd.Flags().BoolVar(&noStatus, "no-exit-code", false, "no valid exit code (cancelled or no command yet)")
printCmd.Flags().BoolVar(&cached, "cached", false, "use a cached prompt")
RootCmd.AddCommand(printCmd)
}

View file

@ -1,10 +1,8 @@
package prompt
import (
"encoding/json"
"strings"
"github.com/jandedobbeleer/oh-my-posh/src/cache"
"github.com/jandedobbeleer/oh-my-posh/src/color"
"github.com/jandedobbeleer/oh-my-posh/src/config"
"github.com/jandedobbeleer/oh-my-posh/src/regex"
@ -16,13 +14,6 @@ import (
var cycle *color.Cycle = &color.Cycle{}
type engineCache struct {
Prompt string
CurrentLineLength int
RPrompt string
RPromptLength int
}
type Engine struct {
Config *config.Config
Env runtime.Environment
@ -35,10 +26,6 @@ type Engine struct {
activeSegment *config.Segment
previousActiveSegment *config.Segment
engineCache *engineCache
cached bool
}
func (e *Engine) write(text string) {
@ -512,42 +499,6 @@ func (e *Engine) adjustTrailingDiamondColorOverrides() {
}
}
func (e *Engine) restoreEngineFromCache() bool {
if !e.Env.Flags().Cached {
return false
}
data, ok := e.Env.Cache().Get(cache.ENGINECACHE)
if !ok {
return false
}
var engineCache engineCache
err := json.Unmarshal([]byte(data), &engineCache)
if err != nil {
return false
}
e.engineCache = &engineCache
e.write(e.engineCache.Prompt)
e.currentLineLength = e.engineCache.CurrentLineLength
e.rprompt = e.engineCache.RPrompt
e.rpromptLength = e.engineCache.RPromptLength
e.cached = true
return true
}
func (e *Engine) updateEngineCache(value *engineCache) {
cacheJSON, err := json.Marshal(value)
if err != nil {
return
}
e.Env.Cache().Set(cache.ENGINECACHE, string(cacheJSON), 1440)
}
// New returns a prompt engine initialized with the
// given configuration options, and is ready to print any
// of the prompt components.

View file

@ -12,9 +12,7 @@ import (
func (e *Engine) Primary() string {
needsPrimaryRightPrompt := e.needsPrimaryRightPrompt()
if !e.restoreEngineFromCache() {
e.writePrimaryPrompt(needsPrimaryRightPrompt)
}
e.writePrimaryPrompt(needsPrimaryRightPrompt)
switch e.Env.Shell() {
case shell.ZSH:
@ -43,15 +41,6 @@ func (e *Engine) Primary() string {
e.writePrimaryRightPrompt()
}
if !e.cached {
e.updateEngineCache(&engineCache{
Prompt: e.prompt.String(),
CurrentLineLength: e.currentLineLength,
RPrompt: e.rprompt,
RPromptLength: e.rpromptLength,
})
}
return e.string()
}

View file

@ -35,29 +35,25 @@ func (e *Engine) Tooltip(tip string) string {
Alignment: config.Right,
Segments: tooltips,
}
block.Init(e.Env)
if !block.Enabled() {
return ""
}
text, length := e.renderBlockSegments(block)
switch e.Env.Shell() {
case shell.PWSH, shell.PWSH5:
defer func() {
// If a prompt cache is available, we update the right prompt to the new tooltip for reuse.
if e.restoreEngineFromCache() {
e.engineCache.RPrompt = text
e.engineCache.RPromptLength = length
e.updateEngineCache(e.engineCache)
}
}()
e.rprompt = text
e.currentLineLength = e.Env.Flags().Column
space, ok := e.canWriteRightBlock(length, true)
if !ok {
return ""
}
e.write(terminal.SaveCursorPosition())
e.write(strings.Repeat(" ", space))
e.write(text)

View file

@ -17,9 +17,6 @@ New-Module -Name "oh-my-posh-core" -ScriptBlock {
# Check `ConstrainedLanguage` mode.
$script:ConstrainedLanguageMode = $ExecutionContext.SessionState.LanguageMode -eq "ConstrainedLanguage"
# This indicates whether a new prompt should be rendered instead of using the cached one.
$script:NewPrompt = $true
# Prompt related backup.
$script:OriginalPromptFunction = $Function:prompt
$script:OriginalContinuationPrompt = (Get-PSReadLineOption).ContinuationPrompt
@ -157,11 +154,13 @@ New-Module -Name "oh-my-posh-core" -ScriptBlock {
if (!$command -or ($command -eq $script:TooltipCommand)) {
return
}
$script:TooltipCommand = $command
$column = $Host.UI.RawUI.CursorPosition.X
$terminalWidth = Get-TerminalWidth
$cleanPSWD = Get-CleanPSWD
$stackCount = global:Get-PoshStackCount
$arguments = @(
"print"
"tooltip"
@ -176,12 +175,13 @@ New-Module -Name "oh-my-posh-core" -ScriptBlock {
"--column=$column"
"--terminal-width=$terminalWidth"
"--no-status=$script:NoExitCode"
"--cached=$true"
)
$standardOut = (Start-Utf8Process $script:OMPExecutable $arguments) -join ''
if (!$standardOut) {
return
}
Write-Host $standardOut -NoNewline
# Workaround to prevent the text after cursor from disappearing when the tooltip is printed.
@ -210,7 +210,7 @@ New-Module -Name "oh-my-posh-core" -ScriptBlock {
[Microsoft.PowerShell.PSConsoleReadLine]::GetBufferState([ref]$null, [ref]$null, [ref]$parseErrors, [ref]$null)
$executingCommand = $parseErrors.Count -eq 0
if ($executingCommand) {
$script:NewPrompt = $true
$script:newPrompt = $true
$script:TooltipCommand = ''
if ('::TRANSIENT::' -eq 'true') {
Set-TransientPrompt
@ -231,7 +231,7 @@ New-Module -Name "oh-my-posh-core" -ScriptBlock {
[Microsoft.PowerShell.PSConsoleReadLine]::GetSelectionState([ref]$start, [ref]$null)
# only render a transient prompt when no text is selected
if ($start -eq -1) {
$script:NewPrompt = $true
$script:newPrompt = $true
$script:TooltipCommand = ''
if ('::TRANSIENT::' -eq 'true') {
Set-TransientPrompt
@ -432,15 +432,11 @@ Example:
Set-PoshPromptType
# Whether we should use a cached prompt.
$useCache = !$script:NewPrompt
if ($script:PromptType -ne 'transient') {
if ($script:NewPrompt) {
$script:NewPrompt = $false
}
Update-PoshErrorCode
}
$cleanPSWD = Get-CleanPSWD
$stackCount = global:Get-PoshStackCount
Set-PoshContext
@ -462,8 +458,8 @@ Example:
"--terminal-width=$terminalWidth"
"--shell=$script:ShellName"
"--no-status=$script:NoExitCode"
"--cached=$useCache"
)
$standardOut = Start-Utf8Process $script:OMPExecutable $arguments
# make sure PSReadLine knows if we have a multiline prompt
Set-PSReadLineOption -ExtraPromptLineCount (($standardOut | Measure-Object -Line).Lines - 1)