Skip to content

Commit cc449bb

Browse files
committed
Ctrl+C handling gracefully, tar fix and fixed case sensitive bug
1 parent 0258dd4 commit cc449bb

1 file changed

Lines changed: 48 additions & 17 deletions

File tree

Microsoft.PowerShell_profile.ps1

Lines changed: 48 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -564,6 +564,7 @@ function Update-Profile {
564564
}
565565
}
566566
catch {
567+
if ($_.Exception -is [System.Management.Automation.PipelineStoppedException]) { throw }
567568
Write-Error "Unable to check for `$profile updates: $_"
568569
}
569570
finally {
@@ -755,6 +756,7 @@ function pubip {
755756
(Invoke-WebRequest https://ifconfig.me/ip -TimeoutSec 10 -UseBasicParsing).Content
756757
}
757758
catch {
759+
if ($_.Exception -is [System.Management.Automation.PipelineStoppedException]) { throw }
758760
Write-Error "Failed to retrieve public IP: $_"
759761
}
760762
}
@@ -831,7 +833,7 @@ function extract {
831833
$path = $resolved.Path
832834
$ext = [System.IO.Path]::GetExtension($path).ToLower()
833835
$baseName = [System.IO.Path]::GetFileNameWithoutExtension($path)
834-
if ($baseName.EndsWith('.tar')) { $ext = '.tar' + $ext }
836+
if ([string]::Equals('.tar', [System.IO.Path]::GetExtension($baseName), [StringComparison]::OrdinalIgnoreCase)) { $ext = '.tar' + $ext }
835837
Write-Host "Extracting $path ..." -ForegroundColor Cyan
836838
switch ($ext) {
837839
'.zip' { Expand-Archive -LiteralPath $path -DestinationPath $pwd -Force }
@@ -892,6 +894,7 @@ function hb {
892894
Write-Output "$url copied to clipboard."
893895
}
894896
catch {
897+
if ($_.Exception -is [System.Management.Automation.PipelineStoppedException]) { throw }
895898
Write-Error "Failed to upload the document. Error: $_"
896899
}
897900
}
@@ -1244,6 +1247,7 @@ function ports {
12441247
} | Format-Table -AutoSize
12451248
}
12461249
catch {
1250+
if ($_.Exception -is [System.Management.Automation.PipelineStoppedException]) { throw }
12471251
netstat -ano | Select-String 'LISTENING'
12481252
}
12491253
}
@@ -1382,6 +1386,7 @@ function vtscan {
13821386
$found = $true
13831387
}
13841388
catch {
1389+
if ($_.Exception -is [System.Management.Automation.PipelineStoppedException]) { throw }
13851390
$status = $null
13861391
if ($_.Exception.Response) { $status = [int]$_.Exception.Response.StatusCode }
13871392
if ($status -ne 404) {
@@ -1423,6 +1428,7 @@ function vtscan {
14231428
Write-Host 'Using large-file upload endpoint.' -ForegroundColor DarkGray
14241429
}
14251430
catch {
1431+
if ($_.Exception -is [System.Management.Automation.PipelineStoppedException]) { throw }
14261432
Write-Error "Failed to get upload URL: $_"
14271433
return
14281434
}
@@ -1446,6 +1452,7 @@ function vtscan {
14461452
Start-Process $vtLink
14471453
}
14481454
catch {
1455+
if ($_.Exception -is [System.Management.Automation.PipelineStoppedException]) { throw }
14491456
Write-Error "Upload failed: $_"
14501457
}
14511458
}
@@ -1797,7 +1804,7 @@ function weather {
17971804
return
17981805
}
17991806
}
1800-
catch { $null = $_ }
1807+
catch { if ($_.Exception -is [System.Management.Automation.PipelineStoppedException]) { throw }; $null = $_ }
18011808

18021809
# Fallback: Open-Meteo (free, no API key)
18031810
try {
@@ -1822,7 +1829,10 @@ function weather {
18221829
$unit = $wx.current_units.temperature_2m
18231830
Write-Host "$($loc.name): ${temp}${unit}" -ForegroundColor Cyan
18241831
}
1825-
catch { Write-Error "Could not fetch weather: $_" }
1832+
catch {
1833+
if ($_.Exception -is [System.Management.Automation.PipelineStoppedException]) { throw }
1834+
Write-Error "Could not fetch weather: $_"
1835+
}
18261836
}
18271837

18281838
function wifipass {
@@ -1878,7 +1888,10 @@ function speedtest {
18781888
$mbps = [math]::Round((25 * 8) / $elapsed, 1)
18791889
Write-Host "Download: ~${mbps} Mbps ($([math]::Round($elapsed, 1))s for 25 MB)" -ForegroundColor Green
18801890
}
1881-
catch { Write-Error "Speed test failed: $_" }
1891+
catch {
1892+
if ($_.Exception -is [System.Management.Automation.PipelineStoppedException]) { throw }
1893+
Write-Error "Speed test failed: $_"
1894+
}
18821895
}
18831896

18841897
function sizeof {
@@ -1994,6 +2007,7 @@ function http {
19942007
}
19952008
}
19962009
catch {
2010+
if ($_.Exception -is [System.Management.Automation.PipelineStoppedException]) { throw }
19972011
if ($_.Exception.Response) {
19982012
$status = [int]$_.Exception.Response.StatusCode
19992013
Write-Host "$status $($_.Exception.Response.StatusCode)" -ForegroundColor Red
@@ -2140,7 +2154,10 @@ function tlscert {
21402154
Write-Host " Days left: $daysLeft" -ForegroundColor $color
21412155
Write-Host " Thumbprint: $($cert.Thumbprint)" -ForegroundColor DarkGray
21422156
}
2143-
catch { Write-Error "Failed to check certificate for ${Domain}:${Port} - $_" }
2157+
catch {
2158+
if ($_.Exception -is [System.Management.Automation.PipelineStoppedException]) { throw }
2159+
Write-Error "Failed to check certificate for ${Domain}:${Port} - $_"
2160+
}
21442161
finally {
21452162
if ($cert) { $cert.Dispose() }
21462163
if ($ssl) { $ssl.Dispose() }
@@ -2162,7 +2179,8 @@ function portscan {
21622179
try {
21632180
$async = $tcp.BeginConnect($Hostname, $port, $null, $null)
21642181
$connected = $async.AsyncWaitHandle.WaitOne(500) -and $tcp.Connected
2165-
try { $tcp.EndConnect($async) } catch { $null = $_ }
2182+
try { $tcp.EndConnect($async) }
2183+
catch { if ($_.Exception -is [System.Management.Automation.PipelineStoppedException]) { throw }; $null = $_ }
21662184
if ($connected) {
21672185
Write-Host (" {0,-6} open" -f $port) -ForegroundColor Green
21682186
$open++
@@ -2191,7 +2209,10 @@ function ipinfo {
21912209
Write-Host " Org: $($info.org)" -ForegroundColor DarkGray
21922210
Write-Host " AS: $($info.as)" -ForegroundColor DarkGray
21932211
}
2194-
catch { Write-Error "Failed to lookup IP info: $_" }
2212+
catch {
2213+
if ($_.Exception -is [System.Management.Automation.PipelineStoppedException]) { throw }
2214+
Write-Error "Failed to lookup IP info: $_"
2215+
}
21952216
}
21962217

21972218
# Quick timestamped backup of a file
@@ -2216,8 +2237,14 @@ function watch {
22162237
Write-Host ("watch: every {0}s | {1}" -f $Interval, (Get-Date -Format "HH:mm:ss")) -ForegroundColor DarkGray
22172238
Write-Host ""
22182239
try { & $Command }
2219-
catch { Write-Host $_.Exception.Message -ForegroundColor Red }
2220-
Start-Sleep -Seconds $Interval
2240+
catch {
2241+
if ($_.Exception -is [System.Management.Automation.PipelineStoppedException]) { throw }
2242+
Write-Host $_.Exception.Message -ForegroundColor Red
2243+
}
2244+
try { Start-Sleep -Seconds $Interval }
2245+
catch {
2246+
if ($_.Exception -is [System.Management.Automation.PipelineStoppedException]) { throw }
2247+
}
22212248
}
22222249
}
22232250

@@ -2231,8 +2258,8 @@ function whois {
22312258
Write-Host " Status: $($rdap.status -join ', ')" -ForegroundColor White
22322259
if ($rdap.entities) {
22332260
$registrar = $rdap.entities | Where-Object { $_.roles -contains 'registrar' } | Select-Object -First 1
2234-
if ($registrar -and $registrar.vcardArray) {
2235-
$fn = $registrar.vcardArray[1] | Where-Object { $_[0] -eq 'fn' } | ForEach-Object { $_[3] }
2261+
if ($registrar -and $registrar.vcardArray -and @($registrar.vcardArray).Count -gt 1) {
2262+
$fn = $registrar.vcardArray[1] | Where-Object { $_ -and $_[0] -eq 'fn' } | ForEach-Object { $_[3] }
22362263
if ($fn) { Write-Host " Registrar: $fn" -ForegroundColor White }
22372264
}
22382265
}
@@ -2254,6 +2281,7 @@ function whois {
22542281
}
22552282
}
22562283
catch {
2284+
if ($_.Exception -is [System.Management.Automation.PipelineStoppedException]) { throw }
22572285
if ($_.Exception.Response -and [int]$_.Exception.Response.StatusCode -eq 404) {
22582286
Write-Error "Domain not found: $Domain"
22592287
}
@@ -2409,7 +2437,7 @@ if (Get-Command dotnet -ErrorAction SilentlyContinue) {
24092437
# Oh My Posh initialization (interactive only, cached for fast startup)
24102438
if ($isInteractive) {
24112439
if (Get-Command oh-my-posh -ErrorAction SilentlyContinue) {
2412-
# Read theme name + URL from cached config
2440+
# Read theme name + URL from cached config; user-settings.json overrides theme.json
24132441
$profileConfigPath = Join-Path $cacheDir "theme.json"
24142442
$themeName = $null
24152443
$themeUrl = $null
@@ -2421,7 +2449,6 @@ if ($isInteractive) {
24212449
}
24222450
catch { Write-Verbose "Failed to parse theme.json: $_" }
24232451
}
2424-
# User-settings theme override (startup-time, not just Update-Profile)
24252452
$userSettingsStartup = Join-Path $cacheDir "user-settings.json"
24262453
if (Test-Path $userSettingsStartup) {
24272454
try {
@@ -2483,10 +2510,14 @@ if ($isInteractive) {
24832510
$cacheContent = Get-Content $ompCachePath -Raw
24842511
$firstLine = ($cacheContent -split "`n", 2)[0]
24852512
# Fast check: verify header references correct theme path
2486-
if ($firstLine -match '^# OMP_CACHE: .+ \| ' -and $firstLine.EndsWith($localThemePath)) {
2487-
# Also verify that the OMP internal init file referenced in the cache still exists.
2488-
# Cleaning programs (CCleaner, BleachBit) often wipe LocalCache directories,
2489-
# deleting OMP's internal init script and breaking the cached redirect.
2513+
# Header format: # OMP_CACHE: <version> | <theme_path> - compare path case-insensitively (Windows)
2514+
$headerPath = $null
2515+
if ($firstLine -match '^# OMP_CACHE: .+ \| (.+)$') { $headerPath = $Matches[1].Trim() }
2516+
$pathMatches = $headerPath -and [string]::Equals($headerPath, $localThemePath, [StringComparison]::OrdinalIgnoreCase)
2517+
# Also verify that the OMP internal init file referenced in the cache still exists.
2518+
# Cleaning programs (CCleaner, BleachBit) often wipe LocalCache directories,
2519+
# deleting OMP's internal init script and breaking the cached redirect.
2520+
if ($pathMatches) {
24902521
$ompInternalPath = [regex]::Match($cacheContent, "& '([^']+)'").Groups[1].Value
24912522
if ($ompInternalPath -and (Test-Path -LiteralPath $ompInternalPath)) {
24922523
$cacheValid = $true

0 commit comments

Comments
 (0)