File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -264,12 +264,16 @@ $platformId = if ($IsWindows) { 'win' } elseif ($IsMacOS) { 'macos' } else { 'li
264264$cacheDir = Join-Path $profileRoot ' .cache'
265265if (-not $SkipStarship -and $availableTools.Contains (' starship' )) {
266266 $f = Invoke-WithFileCache `
267- - Key " starship-init-powershell-$platformId " `
267+ - Key " starship-init-powershell-$platformId -v2 " `
268268 - MaxAge ([TimeSpan ]::FromDays(7 )) `
269269 - Generator {
270- $initScript = & starship init powershell -- print- full- init
270+ $initScriptLines = & starship init powershell -- print- full- init 2> $null
271+ if ($LASTEXITCODE -ne 0 -or -not $initScriptLines ) {
272+ $initScriptLines = & starship init powershell
273+ }
274+ $initScript = ($initScriptLines -join [System.Environment ]::NewLine)
271275 try {
272- $continuationPrompt = & starship prompt -- continuation
276+ $continuationPrompt = ( & starship prompt -- continuation) -join [ System.Environment ]::NewLine
273277 if ($continuationPrompt ) {
274278 $escaped = $continuationPrompt -replace " '" , " ''"
275279 $pattern = ' Set-PSReadLineOption -ContinuationPrompt \(\s*Invoke-Native -Executable .+? -Arguments @\(\s*"prompt",\s*"--continuation"\s*\)\s*\)'
Original file line number Diff line number Diff line change @@ -205,11 +205,18 @@ function Initialize-Environment {
205205 if ($SkipTools -or $SkipStarship ) { return }
206206 Write-Verbose " 初始化 Starship 提示符"
207207 $cacheDir = Join-Path $profileRoot ' .cache'
208+ $starshipCacheKey = " starship-init-powershell-$platformId -v2"
208209 $starshipFile = Invoke-WithFileCache `
209- - Key " starship-init-powershell- $platformId " `
210+ - Key $starshipCacheKey `
210211 - MaxAge ([TimeSpan ]::FromDays(7 )) `
211212 - Generator {
212- $initScript = & starship init powershell -- print- full- init
213+ # starship init 可能返回 string[];必须显式按换行拼接,避免后续正则时被 PowerShell 隐式拼接成单行
214+ $initScriptLines = & starship init powershell -- print- full- init 2> $null
215+ if ($LASTEXITCODE -ne 0 -or -not $initScriptLines ) {
216+ Write-Verbose " starship 不支持 --print-full-init,回退到默认 init 输出"
217+ $initScriptLines = & starship init powershell
218+ }
219+ $initScript = ($initScriptLines -join [System.Environment ]::NewLine)
213220
214221 # Post-processing: 缓存 continuation prompt 输出,避免每次启动 spawn 子进程(~100-150ms)
215222 # starship init 输出中包含:
@@ -218,7 +225,7 @@ function Initialize-Environment {
218225 # )
219226 # 替换为预先获取的字面量值
220227 try {
221- $continuationPrompt = & starship prompt -- continuation
228+ $continuationPrompt = ( & starship prompt -- continuation) -join [ System.Environment ]::NewLine
222229 if ($continuationPrompt ) {
223230 # 转义单引号用于嵌入字符串字面量
224231 $escaped = $continuationPrompt -replace " '" , " ''"
You can’t perform that action at this time.
0 commit comments