Yevrag35

My Prompt Function

Oct 25th, 2020
159
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. Function prompt([string]$PromptText = "PoSH")
  2. {
  3.     $lastSuccess = $?
  4.  
  5.     $color = @{
  6.         Reset  = "`e[0m"
  7.         Red    = "`e[31;1m"
  8.         Green  = "`e[32;1m"
  9.         Yellow = "`e[33;1m"
  10.         Grey   = "`e[37;0m"
  11.     }
  12.  
  13.     # set color of PS based on success of last execution
  14.     if ($lastSuccess -eq $false)
  15.     {
  16.         $lastExit = $color.Red
  17.     }
  18.     else
  19.     {
  20.         $lastExit = $color.Green
  21.     }
  22.  
  23.  
  24.     # get the execution time of the last command
  25.     $lastCmdTime = ""
  26.     $lastCmd = Get-History -Count 1
  27.     if ($null -ne $lastCmd)
  28.     {
  29.         $cmdTime = $lastCmd.Duration.TotalMilliseconds
  30.         $units = "ms"
  31.         $timeColor = $color.Green
  32.         if ($cmdTime -gt 250 -and $cmdTime -lt 1000)
  33.         {
  34.             $timeColor = $color.Yellow
  35.         }
  36.         elseif ($cmdTime -ge 1000)
  37.         {
  38.             $timeColor = $color.Red
  39.             $units = "s"
  40.             $cmdTime = $lastCmd.Duration.TotalSeconds
  41.             if ($cmdTime -ge 60)
  42.             {
  43.                 $units = "m"
  44.                 $cmdTIme = $lastCmd.Duration.TotalMinutes
  45.             }
  46.         }
  47.  
  48.         $lastCmdTime = "$($color.Grey)[$timeColor$($cmdTime.ToString("#.##"))$units$($color.Grey)]$($color.Reset) "
  49.     }
  50.  
  51.     # get git branch information if in a git folder or subfolder
  52.     $gitBranch = ""
  53.     $path = Get-Location
  54.     while ($path -ne "")
  55.     {
  56.         if (Test-Path (Join-Path $path .git))
  57.         {
  58.             # need to do this so the stderr doesn't show up in $error
  59.             $ErrorActionPreferenceOld = $ErrorActionPreference
  60.             $ErrorActionPreference = 'Ignore'
  61.             $branch = git rev-parse --abbrev-ref --symbolic-full-name '@{u}'
  62.             $ErrorActionPreference = $ErrorActionPreferenceOld
  63.  
  64.             # handle case where branch is local
  65.             if ($lastexitcode -ne 0 -or $null -eq $branch)
  66.             {
  67.                 $branch = git rev-parse --abbrev-ref HEAD
  68.             }
  69.  
  70.             $branchColor = $color.Green
  71.  
  72.             if ($branch -match "/master")
  73.             {
  74.                 $branchColor = $color.Red
  75.             }
  76.             $gitBranch = " $($color.Grey)[$branchColor$branch$($color.Grey)]$($color.Reset)"
  77.             break
  78.         }
  79.  
  80.         $path = Split-Path -Path $path -Parent
  81.     }
  82.  
  83.     # truncate the current location if too long
  84.     $currentDirectory = $executionContext.SessionState.Path.CurrentLocation.Path
  85.     $consoleWidth = [Console]::WindowWidth
  86.     $maxPath = [int]($consoleWidth / 2)
  87.     if ($currentDirectory.Length -gt $maxPath)
  88.     {
  89.         $currentDirectory = "`u{2026}" + $currentDirectory.SubString($currentDirectory.Length - $maxPath)
  90.     }
  91.  
  92.     "$lastCmdTime$currentDirectory$gitBranch`n$($lastExit)$PromptText$($color.Reset)$('>' * ($nestedPromptLevel + 1)) "
  93. }
Add Comment
Please, Sign In to add comment