From 6d0a63352147c77804ddb47adadce48c3e32cc8e Mon Sep 17 00:00:00 2001 From: m1ngsama Date: Tue, 15 Jul 2025 10:00:00 +0800 Subject: [PATCH] perf: optimize PowerShell with lazy loading and utilities --- powershell/user_profile.ps1 | 90 ++++++++++++++++++++++++++++++++++--- 1 file changed, 83 insertions(+), 7 deletions(-) diff --git a/powershell/user_profile.ps1 b/powershell/user_profile.ps1 index ee6bd1a..8397568 100644 --- a/powershell/user_profile.ps1 +++ b/powershell/user_profile.ps1 @@ -1,21 +1,41 @@ # set PowerShell to UTF-8 [console]::InputEncoding = [console]::OutputEncoding = New-Object System.Text.UTF8Encoding -Import-Module posh-git +# Performance: Import modules in background jobs for faster startup +$null = Start-Job -ScriptBlock { + Import-Module posh-git -ErrorAction SilentlyContinue + Import-Module Terminal-Icons -ErrorAction SilentlyContinue + Import-Module PSFzf -ErrorAction SilentlyContinue +} + +# Initialize oh-my-posh immediately (essential for prompt) $omp_config = Join-Path $PSScriptRoot ".\m1ng.omp.json" oh-my-posh --init --shell pwsh --config $omp_config | Invoke-Expression -Import-Module -Name Terminal-Icons - -# PSReadLine +# PSReadLine - Configure immediately for better typing experience Set-PSReadLineOption -EditMode Emacs Set-PSReadLineOption -BellStyle None Set-PSReadLineKeyHandler -Chord 'Ctrl+d' -Function DeleteChar Set-PSReadLineOption -PredictionSource History +Set-PSReadLineOption -PredictionViewStyle ListView +Set-PSReadLineOption -HistorySearchCursorMovesToEnd -# Fzf -Import-Module PSFzf -Set-PsFzfOption -PSReadlineChordProvider 'Ctrl+f' -PSReadlineChordReverseHistory 'Ctrl+r' +# Lazy load modules on first use +$global:__ModulesLoaded = @{} + +function Import-LazyModule { + param($ModuleName) + if (-not $global:__ModulesLoaded[$ModuleName]) { + Import-Module $ModuleName -ErrorAction SilentlyContinue + $global:__ModulesLoaded[$ModuleName] = $true + } +} + +# Fzf - Lazy load configuration +Register-ArgumentCompleter -Native -CommandName 'fzf' -ScriptBlock { + Import-LazyModule PSFzf + Set-PsFzfOption -PSReadlineChordProvider 'Ctrl+f' -PSReadlineChordReverseHistory 'Ctrl+r' +} # Env $env:GIT_SSH = "C:\Windows\system32\OpenSSH\ssh.exe" @@ -34,3 +54,59 @@ function which ($command) { Select-Object -ExpandProperty Path -ErrorAction SilentlyContinue } +# Enhanced directory navigation +function .. { Set-Location .. } +function ... { Set-Location ..\.. } +function .... { Set-Location ..\..\.. } +function ..... { Set-Location ..\..\..\.. } + +# Quick access to common directories +function docs { Set-Location ~\Documents } +function dl { Set-Location ~\Downloads } +function dt { Set-Location ~\Desktop } +function proj { Set-Location ~\Projects } + +# Enhanced ls with colors and formatting +function la { Get-ChildItem -Force @args } +function lh { Get-ChildItem -Force -Hidden @args } + +# Git shortcuts +function gs { git status } +function ga { git add @args } +function gc { git commit -m @args } +function gp { git push } +function gpl { git pull } +function gd { git diff @args } +function gco { git checkout @args } +function gb { git branch @args } +function glog { git log --oneline --graph --all --decorate } + +# System utilities +function reload { & $PROFILE } +function touch($file) { New-Item -ItemType File -Name $file -Force } +function mkcd($dir) { New-Item -ItemType Directory -Path $dir -Force; Set-Location $dir } + +# Network utilities +function Get-PublicIP { (Invoke-WebRequest -Uri "https://api.ipify.org").Content } +function Test-Port($hostname, $port) { + Test-NetConnection -ComputerName $hostname -Port $port +} + +# Development utilities +function dev { + param([string]$project) + if ($project) { + Set-Location ~\Projects\$project + } else { + Set-Location ~\Projects + } +} + +function Open-VSCode { code . } +Set-Alias c Open-VSCode + +# System information +function sysinfo { + Get-ComputerInfo | Select-Object CsName, WindowsVersion, OsArchitecture, CsProcessors +} +