Skip to content

Instantly share code, notes, and snippets.

@nicewook
Created November 12, 2020 09:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nicewook/83ea6615ff5a8c2baf5682443f39dc42 to your computer and use it in GitHub Desktop.
Save nicewook/83ea6615ff5a8c2baf5682443f39dc42 to your computer and use it in GitHub Desktop.
function Prompt {
Write-Host # one blank line
# 1. pwd
$pwdArray = $($pwd.path -replace ':', '').split('\')
if ($pwdArray.count -gt 3) {
$linuxPath = "/$($pwdArray[0], '...', $pwdArray[-2], $pwdArray[-1] -join ('/'))"
} else {
$linuxPath = "/$($pwdArray -join ('/'))"
}
write-host $linuxPath -ForegroundColor Cyan -NoNewline
# 2. git status
if (Get-Module Posh-git) {
$vcsStatus = Write-VcsStatus
write-host $vcsStatus -NoNewline
}
# 3. get command execution time
$history = Get-History -ErrorAction Ignore -Count 1
if ($history) {
$elapsedTime = " "
$ts = New-TimeSpan $history.StartExecutionTime $history.EndExecutionTime
switch ($ts) {
{ $_.TotalSeconds -lt 1 } {
[int]$d = $_.TotalMilliseconds
$elapsedTime += '{0}ms' -f ($d)
break
}
{ $_.totalminutes -lt 1 } {
[int]$d = $_.TotalSeconds
$elapsedTime += '{0}s' -f ($d)
break
}
{ $_.totalminutes -ge 1 } {
$elapsedTime += "{0:HH:mm:ss}" -f ([datetime]$ts.Ticks)
break
}
Default {
$elapsedTime += '{0}ms' -f ($d)
}
}
$elapsedTime += " "
}
# 4. check if admin
$currentUser = [Security.Principal.WindowsPrincipal](
[Security.Principal.WindowsIdentity]::GetCurrent())
$isAdminProcess = $currentUser.IsInRole(
[Security.Principal.WindowsBuiltInRole]::Administrator)
$adminHeader = if ($isAdminProcess) { ' Admin ' } else { " $env:USERNAME " }
# 5. time
$now = get-date -format " HH:mm:ss "
# 6. align right: elapsedTime, adminHeader, now
$rightInfo = "$elapsedTime$adminHeader$now "
$startposx = $Host.UI.RawUI.windowsize.width - $rightInfo.length
$startposy = $Host.UI.RawUI.CursorPosition.Y
$Host.UI.RawUI.CursorPosition = New-Object System.Management.Automation.Host.Coordinates $startposx,$startposy
if ($elapsedTime.length -ne 0) {
write-host $elapsedTime -ForegroundColor Black -NoNewline -BackgroundColor DarkYellow
}
write-host $adminHeader -ForegroundColor Black -NoNewline -BackgroundColor DarkGreen
write-host $now -ForegroundColor Gray -NoNewline -BackgroundColor Red
# 8. prompt arrow
if ($host.UI.RawUI.CursorPosition.X -gt 0) { Write-Host }
"$([char]0x2192) "
}
Import-Module posh-git
# Blog posting
# https://jusths.tistory.com/180
# https://jusths.tistory.com/181
# References
# http://www.nichesoftware.co.nz/2018/02/10/powershell-prompt.html
# http://joonro.github.io/blog/posts/powershell-customizations/
# UNICODE
# https://unicode-table.com/kr/sets/arrow-symbols/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment