Skip to content

Commit 4e02d52

Browse files
committed
fix(profile): 修复 starship 初始化脚本处理数组输出的问题
- 将缓存键更新为 v2 版本,避免使用旧缓存 - 正确处理 starship 命令返回的字符串数组,显式拼接换行符 - 添加对 --print-full-init 失败的回退处理 - 确保 continuation prompt 也正确处理数组输出
1 parent 1f774ca commit 4e02d52

2 files changed

Lines changed: 17 additions & 6 deletions

File tree

profile/Debug-ProfilePerformance.ps1

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -264,12 +264,16 @@ $platformId = if ($IsWindows) { 'win' } elseif ($IsMacOS) { 'macos' } else { 'li
264264
$cacheDir = Join-Path $profileRoot '.cache'
265265
if (-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*\)'

profile/features/environment.ps1

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff 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 "'", "''"

0 commit comments

Comments
 (0)