forked from JeremySkinner/posh-svn
-
Notifications
You must be signed in to change notification settings - Fork 7
/
SvnPrompt.ps1
199 lines (157 loc) · 8.37 KB
/
SvnPrompt.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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
$global:SvnPromptSettings = [PSCustomObject]@{
DefaultForegroundColor = $null
DefaultBackgroundColor = $null
BeforeText = ' ['
BeforeForegroundColor = [ConsoleColor]::Yellow
BeforeBackgroundColor = $null
DelimText = ' |'
DelimForegroundColor = [ConsoleColor]::Yellow
DelimBackgroundColor = $null
AfterText = ']'
AfterForegroundColor = [ConsoleColor]::Yellow
AfterBackgroundColor = $null
FileAddedText = '+'
FileModifiedText = '~'
FileRemovedText = '-'
FileConflictedText = '!'
LocalDefaultStatusSymbol = $null
LocalDefaultStatusForegroundColor = [ConsoleColor]::DarkGreen
LocalDefaultStatusBackgroundColor = $null
LocalWorkingStatusSymbol = '!'
LocalWorkingStatusForegroundColor = [ConsoleColor]::DarkRed
LocalWorkingStatusBackgroundColor = $null
LocalStagedStatusSymbol = '~'
LocalStagedStatusForegroundColor = [ConsoleColor]::Cyan
LocalStagedStatusBackgroundColor = $null
BranchForegroundColor = [ConsoleColor]::Cyan
BranchBackgroundColor = $null
RevisionText = '@'
RevisionForegroundColor = [ConsoleColor]::DarkGray
RevisionBackgroundColor = $null
IndexForegroundColor = [ConsoleColor]::DarkGreen
IndexBackgroundColor = $null
WorkingForegroundColor = [ConsoleColor]::DarkRed
WorkingBackgroundColor = $null
ExternalStatusSymbol = [char]0x2190 # arrow right
ExternalForegroundColor = [ConsoleColor]::DarkGray
ExternalBackgroundColor = $null
IncomingStatusSymbol = [char]0x2193 # Down arrow
IncomingForegroundColor = [ConsoleColor]::Red
IncomingBackgroundColor = $null
ShowStatusWhenZero = $true
EnablePromptStatus = !$Global:SvnMissing
EnableRemoteStatus = $true # show remote server status
EnableExternalFileStatus = $false # include files from externals in counts
ShowExternals = $true
EnableWindowTitle = 'svn ~ '
}
$WindowTitleSupported = $true
if (Get-Module NuGet) {
$WindowTitleSupported = $false
}
function Write-Prompt {
param (
[Parameter(Position = 0, Mandatory = $true)]
$Object,
[Parameter(Mandatory = $false)]
[Nullable[ConsoleColor]] $ForegroundColor = $SvnPromptSettings.DefaultForegroundColor,
[Parameter(Mandatory = $false)]
[Nullable[ConsoleColor]] $BackgroundColor = $SvnPromptSettings.DefaultBackgroundColor
)
$writeHostParams = @{
Object = $Object;
NoNewLine = $true;
}
if (Test-ConsoleColor $BackgroundColor) {
$writeHostParams.BackgroundColor = $BackgroundColor
}
if (Test-ConsoleColor $ForegroundColor) {
$writeHostParams.ForegroundColor = $ForegroundColor
}
Write-Host @writeHostParams
}
function Write-SvnStatus($status) {
$s = $global:SvnPromptSettings
if ($status -and $s) {
Write-Prompt $s.BeforeText -BackgroundColor $s.BeforeBackgroundColor -ForegroundColor $s.BeforeForegroundColor
Write-Prompt $status.Branch -BackgroundColor $s.BranchBackgroundColor -ForegroundColor $s.BranchForegroundColor
Write-Prompt "$($s.RevisionText)$($status.Revision)" -BackgroundColor $s.RevisionBackgroundColor -ForegroundColor $s.RevisionForegroundColor
if ($status.HasIndex) {
if ($s.ShowStatusWhenZero -or $status.Added) {
Write-Prompt " $($s.FileAddedText)$($status.Added)" -BackgroundColor $s.IndexBackgroundColor -ForegroundColor $s.IndexForegroundColor
}
if ($s.ShowStatusWhenZero -or $status.Modified) {
Write-Prompt " $($s.FileModifiedText)$($status.Modified)" -BackgroundColor $s.IndexBackgroundColor -ForegroundColor $s.IndexForegroundColor
}
if ($s.ShowStatusWhenZero -or $status.Deleted) {
Write-Prompt " $($s.FileRemovedText)$($status.Deleted)" -BackgroundColor $s.IndexBackgroundColor -ForegroundColor $s.IndexForegroundColor
}
}
if ($status.HasWorking) {
if ($status.HasIndex) {
Write-Prompt $s.DelimText -BackgroundColor $s.DelimBackgroundColor -ForegroundColor $s.DelimForegroundColor
}
if ($status.Untracked) {
Write-Prompt " $($s.FileAddedText)$($status.Untracked)" -BackgroundColor $s.WorkingBackgroundColor -ForegroundColor $s.WorkingForegroundColor
}
if ($status.Missing) {
Write-Prompt " $($s.FileRemovedText)$($status.Missing)" -BackgroundColor $s.WorkingBackgroundColor -ForegroundColor $s.WorkingForegroundColor
}
if ($status.Conflicted) {
Write-Prompt " $($s.FileConflictedText)$($status.Conflicted)" -BackgroundColor $s.WorkingBackgroundColor -ForegroundColor $s.WorkingForegroundColor
}
}
if ($status.Incoming) {
Write-Prompt " $($s.IncomingStatusSymbol)$($status.Incoming)" -BackgroundColor $s.IncomingBackgroundColor -ForegroundColor $s.IncomingForegroundColor
Write-Prompt "$($s.RevisionText)$($status.IncomingRevision)" -BackgroundColor $s.RevisionBackgroundColor -ForegroundColor $s.RevisionForegroundColor
}
if ($status.HasIndex) {
# We have uncommitted files
$localStatusSymbol = $s.LocalStagedStatusSymbol
$localStatusBackgroundColor = $s.LocalStagedStatusBackgroundColor
$localStatusForegroundColor = $s.LocalStagedStatusForegroundColor
}
elseif ($status.HasWorking) {
# We have uncommitted files
$localStatusSymbol = $s.LocalWorkingStatusSymbol
$localStatusBackgroundColor = $s.LocalWorkingStatusBackgroundColor
$localStatusForegroundColor = $s.LocalWorkingStatusForegroundColor
}
else {
# No uncommited changes
$localStatusSymbol = $s.LocalDefaultStatusSymbol
$localStatusBackgroundColor = $s.LocalDefaultStatusBackgroundColor
$localStatusForegroundColor = $s.LocalDefaultStatusForegroundColor
}
if ($s.ShowExternals -and $status.External) {
if ($status.HasWorking -or $status.HasIndex) {
Write-Prompt $s.DelimText -BackgroundColor $s.DelimBackgroundColor -ForegroundColor $s.DelimForegroundColor
}
Write-Prompt " $($s.ExternalStatusSymbol)$($status.External)" -BackgroundColor $s.ExternalBackgroundColor -ForegroundColor $s.ExternalForegroundColor
}
if ($localStatusSymbol) {
Write-Prompt (" {0}" -f $localStatusSymbol) -BackgroundColor $localStatusBackgroundColor -ForegroundColor $localStatusForegroundColor
}
Write-Prompt $s.AfterText -BackgroundColor $s.AfterBackgroundColor -ForegroundColor $s.AfterForegroundColor
if ($WindowTitleSupported -and $status.Title) {
$Global:CurrentWindowTitle += ' ~ ' + $status.Title
}
}
}
# Should match https://github.com/dahlbyk/posh-git/blob/master/GitPrompt.ps1
if (!(Test-Path Variable:Global:VcsPromptStatuses)) {
$Global:VcsPromptStatuses = @()
}
# Scriptblock that will execute for write-vcsstatus
$PoshSvnVcsPrompt = {
$Global:SvnStatus = Get-SvnStatus
Write-SvnStatus $SvnStatus
}
$Global:VcsPromptStatuses += $PoshSvnVcsPrompt
$ExecutionContext.SessionState.Module.OnRemove = {
$c = $Global:VcsPromptStatuses.Count
$global:VcsPromptStatuses = @( $global:VcsPromptStatuses | Where-Object { $_ -ne $PoshSvnVcsPrompt -and $_ -inotmatch '\bWrite-SvnStatus\b' } ) # cdonnelly 2017-08-01: if the script is redefined in a different module
if ($c -ne 1 + $Global:VcsPromptStatuses.Count) {
Write-Warning "posh-svn: did not remove prompt"
}
}