-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPrettyPrompt.ps1
74 lines (58 loc) · 2.38 KB
/
PrettyPrompt.ps1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
. "$PSScriptRoot\Format-ShortInterval.ps1"
. "$PSScriptRoot\Is-Administrator.ps1"
. "$PSScriptRoot\Utils.ps1"
Function PrettyPrompt([System.ConsoleColor] $UserColor = "Blue", [System.ConsoleColor] $AdminColor = "Red", [System.ConsoleColor] $HostColor = "White") {
<#
.SYNOPSIS
My fav prompt for any terminal
.DESCRIPTION
This prompt is roughly based on https://github.com/pascal-so/bashprompt
Result is roughly like this:
(3.14s) (21:15) Pascal@Tuman D:\documents>
No guarantee that this works with network drives or anything like that.
.PARAMETER UserColor
Print the username in this color.
.PARAMETER AdminColor
Print the username in this color if the current PowerShell is running as Administrator.
.PARAMETER HostColor
Print the computer name in this color.
.EXAMPLE
PrettyPrompt
(3.14s) (21:15) Pascal@Tuman ~>
#>
function Get-LastExecutionTime {
if ((Get-History).count -eq 0) {
return [TimeSpan]0
}
return Get-HistoryEntryDuration((Get-History)[-1])
}
function Get-ShortLocation {
<#
.DESCRIPTION
replace path to home with ~ in the pwd. this is
not really windows style but idk
#>
$FullPathName = $(Get-Location).Path
if ($FullPathName.ToLower().StartsWith($HOME.ToLower())) {
return "~" + $FullPathName.Substring($HOME.length)
} else {
return $FullPathName
}
}
# If the last command failed we print the execution time in red.
$StatusColor = if ($?) {"White"} else {"Red"}
if (Is-Administrator) {
$UserColor = $AdminColor
}
Write-Host "($(Format-ShortInterval(Get-LastExecutionTime)))" -ForegroundColor $StatusColor -NoNewline
Write-Host " ($(Get-Date -Format "HH:mm")) " -NoNewline
Write-Host $env:USERNAME -ForegroundColor $UserColor -NoNewline
Write-Host "@" -NoNewline
Write-Host $env:COMPUTERNAME -ForegroundColor $HostColor -NoNewline
Write-Host " $(Get-ShortLocation)$('>' * ($nestedPromptLevel + 1))" -NoNewline
# This is required otherwise this function returns $null which indicates
# an invalid prompt to PowerShell and therefore PowerShell adds its own
# default PS> prompt... at least that's how I understood it from 1 minute
# of digging around.
return " "
}