-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathWrite-Prompt.ps1
More file actions
114 lines (83 loc) · 2.88 KB
/
Write-Prompt.ps1
File metadata and controls
114 lines (83 loc) · 2.88 KB
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
function Test-Git {
param([IO.DirectoryInfo] $dir)
if(Test-Path (Join-Path $dir.FullName '.git')) {
return $true;
}
if(($dir.Parent -eq $null) -or ($dir -eq $dir.Parent)) {
return $false;
}
return Test-Git ($dir.Parent.FullName)
}
function Write-Prompt {
if($global:FSFormatDefaultColor) {
[Console]::ForegroundColor = $global:FSFormatDefaultColor
}
$isFS = (gi .).PSProvider.Name -eq 'FileSystem';
if($isFS) {
[system.Environment]::CurrentDirectory = (convert-path ".");
}
$user = [Security.Principal.WindowsIdentity]::GetCurrent();
$admin = (New-Object Security.Principal.WindowsPrincipal $user).IsInRole([Security.Principal.WindowsBuiltinRole]::Administrator)
$branch = $null;
if( $isFS -and (Test-Git (cvpa .)) -and (which git)) {
$branch = (git branch 2>$null) | %{ ([regex] '\* (.*)').match($_) } | ? { $_.Success } | %{ $_.Groups[1].Value } | Select-Object -First 1
}
$drive = (Get-Location).Drive
if($drive.Root -eq "$($drive.Name):\") {
$title = $executionContext.SessionState.Path.CurrentLocation
if($branch) {
$title = "[$branch] $title";
}
}
else {
$title = $drive.Name
if($branch) {
$title = "$title@$branch";
}
}
$host.UI.RawUI.WindowTitle = $title
if(Test-Ansi) {
$begining = ''
$ending = '>' * ($nestedPromptLevel + 1)
function esc {
param([string] $code);
[char](27) + "[$code"
}
if($admin) {
$color = '33;1m';
}
else {
$color = '32;1m';
}
if($host.UI.RawUI.CursorPosition.ToString() -ne '0,0')
{
$begining = $begining + [Environment]::NewLine;
}
if($branch) {
$begining = $begining + "$(esc '33;1m')[$(esc '36;1m')$branch$(esc '33;1m')] ";
}
return ( $begining + (esc $color) + $executionContext.SessionState.Path.CurrentLocation + $ending + (esc '37;2m') )
}
else {
if($admin) {
$color = 'Yellow';
}
else {
$color = 'Green';
}
Write-Host ([System.Char](10)) -NoNewLine;
if($branch) {
Write-Host "[" -NoNewLine -ForegroundColor Yellow
Write-Host "$branch" -NoNewLine -ForegroundColor Cyan
Write-Host "] " -NoNewLine -ForegroundColor Yellow
}
Write-Host $executionContext.SessionState.Path.CurrentLocation -NoNewLine -ForegroundColor $color;
Write-Host ('>' * ($nestedPromptLevel + 1)) -NoNewLine -ForegroundColor $color;
if($host.Name -like 'StudioShell*') {
return " ";
}
else {
return " `b";
}
}
}