Skip to content

Instantly share code, notes, and snippets.

@devblackops
Last active February 28, 2023 23:50
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save devblackops/a94e2f75626097a9d11b002572f8f225 to your computer and use it in GitHub Desktop.
Save devblackops/a94e2f75626097a9d11b002572f8f225 to your computer and use it in GitHub Desktop.
My basic prompt
Import-Module posh-git
function prompt {
# The status of the last command run
$lastSuccess = $?
# Colors for prompt
$color = @{
Reset = "`e[0m"
Red = "`e[31;1m"
Green = "`e[32;1m"
Yellow = "`e[33;1m"
Blue = "`e[34;1m"
Grey = "`e[37m"
}
# Icons for prompt
$statusIcon = @{
normal = @{
color = $color.Reset
icon = "`u{2714}"
}
warning = @{
color = $color.Yellow
icon = "`u{0021}"
}
error = @{
color = $color.Red
icon = 'X'
}
}
$redHeart = "$($color.Red)`u{2764}"
$blueArrow = "`e[0;34m`u{25ba}"
# Set color of status icon based on success of last execution
if ($lastSuccess -eq $false) {
$lastExit = "[$($color.Red)$($statusIcon.error.icon)$($color.Reset)]"
} else {
$lastExit = "[$($color.Green)$($statusIcon.normal.icon)$($color.Reset)]"
}
# Get the execution time of the last command
$lastCmdTime = ''
$lastCmd = Get-History -Count 1
if ($null -ne $lastCmd) {
$cmdTime = $lastCmd.Duration.TotalMilliseconds
$units = 'ms'
$timeColor = $color.Green
if ($cmdTime -gt 250 -and $cmdTime -lt 1000) {
$timeColor = $color.Yellow
} elseif ($cmdTime -ge 1000) {
$timeColor = $color.Red
$units = 's'
$cmdTime = $lastCmd.Duration.TotalSeconds
if ($cmdTime -ge 60) {
$units = 'm'
$cmdTIme = $lastCmd.Duration.TotalMinutes
}
}
$lastCmdTime = "$($color.Grey)[$timeColor$($cmdTime.ToString("#.##"))$units$($color.Grey)]$($color.Reset)"
}
# Truncate the current location if too long
$currentDirectory = $executionContext.SessionState.Path.CurrentLocation.Path
$maxPath = 24
if ($currentDirectory.Length -gt $maxPath) {
$currentDirectory = "`u{2026}" + $currentDirectory.SubString($currentDirectory.Length - $maxPath)
}
# Display the prompt
$currentDirectory + $(Write-VcsStatus) + [Environment]::NewLine + "$($lastExit) [$($color.Blue)$(hostname)$($color.Reset)] $lastCmdTime I $redHeart $($color.Reset)PS $blueArrow$($color.Reset) "
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment