-
Notifications
You must be signed in to change notification settings - Fork 1
/
Prompts.ps1
127 lines (113 loc) · 3.59 KB
/
Prompts.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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
function global:Get-TruncatedPath {
param(
[ValidateScript({$_ -gt 10})]
[int]$MaxPath = 20
)
$pp = $pwd.ProviderPath
if ($pp.StartsWith("\\")) {
$endhost = $pp.IndexOf('\', 2)
$host_name = $pp.Substring(2, $endhost-2)
if ($host_name -notmatch "(\d+).(\d+).(\d+).(\d+)") {
$hi = $host_name.IndexOf('.')
if ($hi -gt 0) {
$host_name = $host_name.Substring(0, $hi)
}
}
$host_name = '[' + $host_name + '] '
Write-Host $host_name -NoNewLine -ForegroundColor 'DarkGreen'
$base_path_index = $pp.IndexOf('\', $endhost+1)
if ($base_path_index -le 0) {
$path = "\"
$drive = $pp.Substring($endhost + 1).Replace('$', '')
} else {
$path = $pp.Substring($base_path_index)
$drive = $pp.Substring($endhost + 1, $base_path_index - $endhost - 2)
}
} else {
$host_name = ""
$drive = $pwd.Drive.Name
$path = $pwd.Path.Substring($drive.Length + 1)
}
$adjusted = $false
$lastIndex = $path.Length - $MaxPath
if ($lastIndex -gt 0) {
$lastIndex = $path.LastIndexOf("\", $lastIndex - 1)
$adjusted = $true
}
if ($adjusted) {
$oldPath = $path
$path = "..." + $path.Substring($lastIndex)
if ($path.Length -ge $oldPath.Length) {
$path = $oldPath
}
}
return ("{0}:{1}" -f $drive,$path)
}
function global:Set-NormalPrompt {
param(
[switch]$DoNotRemoveAlias,
[switch]$NoColor
)
# Some modules (e.g. xUtility) create this alias to modify the prompt
if(-not $DoNotRemoveAlias) {
Remove-Item Alias:\Prompt -ErrorAction SilentlyContinue
}
if ($NoColor) {
function global:prompt {
$p = "{0} λ" -f (global:Get-TruncatedPath)
Write-Host $p -NoNewLine
return " "
}
} else {
function global:prompt {
$p = "{0} λ" -f (global:Get-TruncatedPath)
Write-Host $p -NoNewLine -ForegroundColor "DarkGray"
return " "
}
}
}
function global:Set-GitPrompt {
param(
[switch]$DoNotRemoveAlias,
[switch]$NoColor
)
# Some modules (e.g. xUtility) create this alias to modify the prompt
if(-not $DoNotRemoveAlias) {
Remove-Item Alias:\Prompt -ErrorAction SilentlyContinue
}
if ($NoColor) {
function global:prompt {
$realLASTEXITCODE = $LASTEXITCODE
Write-Host($pwd.ProviderPath) -nonewline
Write-VcsStatus
$global:LASTEXITCODE = $realLASTEXITCODE
Write-Host "`nλ" -NoNewLine
return " "
}
} else {
function global:prompt {
$realLASTEXITCODE = $LASTEXITCODE
Write-Host($pwd.ProviderPath) -nonewline
Write-VcsStatus
$global:LASTEXITCODE = $realLASTEXITCODE
Write-Host "`nλ" -NoNewLine -ForegroundColor "DarkGray"
return " "
}
}
}
if ($null -ne (Get-Module -Name PowerLine -ListAvailable)) {
if (-not (Get-Module -Name PowerLine)) {
Import-Module -Name PowerLine
}
function global:Set-NormalPrompt2 {
Set-PowerLinePrompt -Prompt { Get-TruncatedPath } -Colors "#00DDFF","#0066FF" -PowerLineFont -FullColor
}
}
try {
Set-NormalPrompt
}
catch [System.Management.Automation.SessionStateUnauthorizedAccessException] {
Write-Verbose -Message "The prompt seems to be set as readonly, removing ..."
Remove-Item -Path Function:prompt -Force
Set-NormalPrompt
}