Skip to content

Instantly share code, notes, and snippets.

@ctrl-freak
Created January 11, 2017 08:45
Show Gist options
  • Select an option

  • Save ctrl-freak/bdfd15593ff45638dfd9cff47023313c to your computer and use it in GitHub Desktop.

Select an option

Save ctrl-freak/bdfd15593ff45638dfd9cff47023313c to your computer and use it in GitHub Desktop.
# By dastylinrastan
# https://www.reddit.com/r/PowerShell/comments/5n5aux/way_to_tell_how_long_the_last_command_took_to_run/dc8umh2/
# Chocolatey profile
$ChocolateyProfile = "$env:ChocolateyInstall\helpers\chocolateyProfile.psm1"
if (Test-Path($ChocolateyProfile)) {
Import-Module "$ChocolateyProfile"
}
#Justin's Powershell Prompt
function prompt {
function Test-AdminElevation {
# Check for Administrator elevation
$wid=[System.Security.Principal.WindowsIdentity]::GetCurrent()
$prp=new-object System.Security.Principal.WindowsPrincipal($wid)
$adm=[System.Security.Principal.WindowsBuiltInRole]::Administrator
$IsAdmin=$prp.IsInRole($adm)
return $IsAdmin
}
$promptUser = $env:username.toLower()
$promptDomain = $env:userdomain.ToUpper()
if (Test-AdminElevation) {
$adminColor = "Red"
$endPrompt = "#"
} else {
$AdminColor = "Green"
$endPrompt = ">"
}
#Get relative home location
function Write-PromptLocation {
$currentLocation = Get-Location
#Set the default in case we don't do anything special
$promptPath = $currentLocation.Path
#If this is a filesystem path, replace $HOME with ~ if present
if ($currentLocation.Provider.ToString() -eq 'Microsoft.PowerShell.Core\FileSystem') {
$promptPath = $currentLocation.ProviderPath -replace [RegEx]::Escape($HOME),'~'
}
return $promptPath
}
#Look for PoshGit and write the VCS status as appropriate if available
function Write-PromptGitStatus ([switch]$PreLoad) {
#PoshGit Discovery
#Only run once per powershell session, to keep performance high
if ($SCRIPT:promptPoshGitStatus -eq $null) {
#First see if PoshGit and Git are in the normal path.
try {
import-module Posh-Git -ErrorAction stop
get-command git.exe -erroraction Stop | Out-Null
$SCRIPT:promptPoshGitStatus = $true
} catch {
$SCRIPT:promptPoshGitStatus = $false
}
#If not, look for GitHub for Windows
if ($SCRIPT:promptPoshGitStatus -eq $false) {
try {
#Try importing the GitHub for Windows environment
. (Resolve-Path "$env:LOCALAPPDATA\GitHub\shell.ps1" -erroraction stop)
import-module "$env:github_posh_git\posh-git.psm1" -erroraction stop
$SCRIPT:promptPoshGitStatus = $true
}
catch {
$SCRIPT:promptPoshGitStatus = $false
}
}
}
if (!($preload)) {
if ($SCRIPT:promptPoshGitStatus) {
write-vcsstatus
}
}
}
#Get Execution Time of Last Command
function Get-LastCommandTimeString {
$lastCMDTime = ""
$lastCMD = get-history -count 1
#Avoids showing time when an empty "enter" key is hit
if ($script:PromptLastHistoryID -eq $lastCMD.ID) {return}
$script:PromptLastHistoryID = $lastCMD.Id
if ($lastCMD) {
$lastCMDTime = ($lastCMD.EndExecutionTime - $lastCMD.StartExecutionTime)
$lastCMDTimeFormat = "mm\:ss\.fff"
if ($lastCMDTime.Hours) {
$lastCMDTimeFormat = "hh\:" + $lastCMDTimeFormat
}
[PSCustomObject]@{
duration = $lastCMDTime.ToString($lastCMDTimeFormat)
command = ($lastCMD -split ' ')[0]
}
}
}
#Workaround to ensure the prompt loading isn't delayed by Posh-Git Module Initialization
Write-PromptGitStatus -PreLoad
$lastCMDTimeString = Get-LastCommandTimeSTring
if ($lastCMDTimeString) {
write-host -NoNewline -ForegroundColor DarkGray '['
write-host -NoNewline -ForegroundColor DarkGray $lastCMDTimeString.duration
write-host -NoNewline -ForegroundColor DarkGray '] '
#write-host -foregroundcolor DarkGray ')'
#write-host -NoNewline -ForegroundColor DarkGray $lastCMDTimeString.command
#write-host -foregroundcolor DarkGray ')'
}
write-host -NoNewLine -ForegroundColor Cyan '['
write-host -NoNewline -ForegroundColor $AdminColor ($promptDomain + "\" + $promptUser)
write-host -NoNewLine -ForegroundColor Cyan ']'
Write-PromptGitStatus
write-host -ForegroundColor DarkCyan (" " + (Write-PromptLocation))
return ($endPrompt + " ")
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment