PowerShell美化prompt
2022-04-24 • 预计阅读时间 1 分钟
2022-04-24 • 预计阅读时间 1 分钟
powershell的prompt美化其实有现成的Oh My Posh.而且还可以显示各种git状态.但是这些的前提都是IO很好的情况下.公司电脑由于各种杀毒和安全软件导致IO极差.
根据自己的需要,编写了如下的函数.主要增加一些小 icon
还有提供类似 fish shell 的文件夹缩写显示.
把下面代码加入到你的 profile
里面就可以看看效果了.具体可以根据自己的喜好调整下 icon
内容.
function global:prompt {
$dateTime = get-date -Format "HH:mm:ss"
$current = Split-Path -Leaf -Path $(Get-Location)
$splitChar = [System.IO.Path]::DirectorySeparatorChar
$regex = [regex]::Escape($HOME) + "(\\.*)*$"
$currentDirectory = $(Get-Location).Path -replace $regex , '~$1'
$folders = $currentDirectory.Split($splitChar, [System.StringSplitOptions]::RemoveEmptyEntries)
if ($folders.Length -lt 3) {
# If the path is 0 or 1, then ignore.
}
else {
$firstNameFolders = ForEach ($f in $folders[1..($folders.Length - 2)]) {
if ($f -match '\d*[-|\.]\d*') {
$f.Substring(0, 5)
}
elseif ($f -match '\d{2,2}\s') {
$f.Substring(0, 2)
}
else {
$f[0]
}
}
$folders = @($folders[0]) + $firstNameFolders + @($current)
$currentDirectory = ($folders -join $splitChar)
}
$StartSymbol = [char]::ConvertFromUtf32(0x01F984) + [char]::ConvertFromUtf32(0x0001F525) + ''
$timeSymbol = [char]::ConvertFromUtf32(955)
$ender = ConvertTo-Emoji 0x1F47B
$luckleaf = ConvertTo-Emoji 0x1F340
$preIcon = ConvertTo-Emoji 0x262F
$prePath = ConvertTo-Emoji 0x1FAA1
Write-Host "$preIcon$currentDirectory$ender" -f DarkMagenta -NoNewline ; Write-Host "[$dateTime]" -f Green
Write-Host "$prePath" -f red -nonewline; Write-Host ">" -f red -nonewline; Write-Host ">" -f Yellow -nonewline; Write-Host ">$luckleaf" -f blue -NoNewline;
return " "
}