Skip to content

Commit

Permalink
fix(pwsh): remove prompt cache
Browse files Browse the repository at this point in the history
resolves #5332
  • Loading branch information
JanDeDobbeleer committed Jul 23, 2024
1 parent 81d79a7 commit 7563d5f
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 84 deletions.
6 changes: 4 additions & 2 deletions src/cli/print.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
49 changes: 0 additions & 49 deletions src/prompt/engine.go
Original file line number Diff line number Diff line change
@@ -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"
Expand All @@ -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
Expand All @@ -35,10 +26,6 @@ type Engine struct {

activeSegment *config.Segment
previousActiveSegment *config.Segment

engineCache *engineCache

cached bool
}

func (e *Engine) write(text string) {
Expand Down Expand Up @@ -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.
Expand Down
13 changes: 1 addition & 12 deletions src/prompt/primary.go
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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()
}

Expand Down
14 changes: 5 additions & 9 deletions src/prompt/tooltip.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
20 changes: 8 additions & 12 deletions src/shell/scripts/omp.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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"
Expand All @@ -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.
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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)
Expand Down

0 comments on commit 7563d5f

Please sign in to comment.