-
-
Notifications
You must be signed in to change notification settings - Fork 803
/
GitPrompt.ps1
946 lines (809 loc) · 31.2 KB
/
GitPrompt.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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
# Inspired by Mark Embling
# http://www.markembling.info/view/my-ideal-powershell-prompt-with-git-integration
$global:GitPromptSettings = [PoshGitPromptSettings]::new()
$global:GitPromptValues = [PoshGitPromptValues]::new()
# Override some of the normal colors if the background color is set to the default DarkMagenta.
$s = $global:GitPromptSettings
if ($Host.UI.RawUI.BackgroundColor -eq [ConsoleColor]::DarkMagenta) {
$s.LocalDefaultStatusSymbol.ForegroundColor = 'Green'
$s.LocalWorkingStatusSymbol.ForegroundColor = 'Red'
$s.BeforeIndex.ForegroundColor = 'Green'
$s.IndexColor.ForegroundColor = 'Green'
$s.WorkingColor.ForegroundColor = 'Red'
}
<#
.SYNOPSIS
Creates a new instance of a PoshGitPromptSettings object that can be assigned to $GitPromptSettings.
.DESCRIPTION
Creates a new instance of a PoshGitPromptSettings object that can be used to reset the
$GitPromptSettings back to its default.
.INPUTS
None
.OUTPUTS
PoshGitPromptSettings
.EXAMPLE
PS> $GitPromptSettings = New-GitPromptSettings
This will reset the current $GitPromptSettings back to its default.
#>
function New-GitPromptSettings {
[PoshGitPromptSettings]::new()
}
<#
.SYNOPSIS
Writes the object to the display or renders it as a string using ANSI/VT sequences.
.DESCRIPTION
Writes the specified object to the display unless $GitPromptSettings.AnsiConsole
is enabled. In this case, the Object is rendered, along with the specified
colors, as a string with the appropriate ANSI/VT sequences for colors embedded
in the string. If a StringBuilder is provided, the string is appended to the
StringBuilder.
.EXAMPLE
PS C:\> Write-Prompt "PS > " -ForegroundColor Cyan -BackgroundColor Black
On a system where $GitPromptSettings.AnsiConsole is set to $false, this
will write the above to the display using the Write-Host command.
If AnsiConsole is set to $true, this will return a string of the form:
"`e[96m`e[40mPS > `e[0m".
.EXAMPLE
PS C:\> $sb = [System.Text.StringBuilder]::new()
PS C:\> $sb | Write-Prompt "PS > " -ForegroundColor Cyan -BackgroundColor Black
On a system where $GitPromptSettings.AnsiConsole is set to $false, this
will write the above to the display using the Write-Host command.
If AnsiConsole is set to $true, this will append the following string to the
StringBuilder object piped into the command:
"`e[96m`e[40mPS > `e[0m".
#>
function Write-Prompt {
[CmdletBinding(DefaultParameterSetName="Default")]
param(
# Specifies objects to display in the console or render as a string if
# $GitPromptSettings.AnsiConsole is enabled. If the Object is of type
# [PoshGitTextSpan] the other color parameters are ignored since a
# [PoshGitTextSpan] provides the colors.
[Parameter(Mandatory, Position=0)]
$Object,
# Specifies the foreground color.
[Parameter(ParameterSetName="Default")]
$ForegroundColor = $null,
# Specifies the background color.
[Parameter(ParameterSetName="Default")]
$BackgroundColor = $null,
# Specifies both the background and foreground colors via [PoshGitCellColor] object.
[Parameter(ParameterSetName="CellColor")]
[ValidateNotNull()]
[PoshGitCellColor]
$Color,
# When specified and $GitPromptSettings.AnsiConsole is enabled, the Object parameter
# is written to the StringBuilder along with the appropriate ANSI/VT sequences for
# the specified foreground and background colors.
[Parameter(ValueFromPipeline = $true)]
[System.Text.StringBuilder]
$StringBuilder
)
if (!$Object -or (($Object -is [PoshGitTextSpan]) -and !$Object.Text)) {
return $(if ($StringBuilder) { $StringBuilder } else { "" })
}
if ($PSCmdlet.ParameterSetName -eq "CellColor") {
$bgColor = $Color.BackgroundColor
$fgColor = $Color.ForegroundColor
}
else {
$bgColor = $BackgroundColor
$fgColor = $ForegroundColor
}
$s = $global:GitPromptSettings
if ($s) {
if ($null -eq $fgColor) {
$fgColor = $s.DefaultColor.ForegroundColor
}
if ($null -eq $bgColor) {
$bgColor = $s.DefaultColor.BackgroundColor
}
if ($s.AnsiConsole) {
if ($Object -is [PoshGitTextSpan]) {
$str = $Object.ToAnsiString()
}
else {
# If we know which colors were changed, we can reset only these and leave others be.
$reset = [System.Collections.Generic.List[string]]::new()
$e = [char]27 + "["
$fg = $fgColor
if (($null -ne $fg) -and !(Test-VirtualTerminalSequece $fg)) {
$fg = Get-ForegroundVirtualTerminalSequence $fg
$reset.Add('39')
}
$bg = $bgColor
if (($null -ne $bg) -and !(Test-VirtualTerminalSequece $bg)) {
$bg = Get-BackgroundVirtualTerminalSequence $bg
$reset.Add('49')
}
$str = "${Object}"
if (Test-VirtualTerminalSequece $str -Force) {
$reset.Clear()
$reset.Add('0')
}
$str = "${fg}${bg}" + $str
if ($reset.Count -gt 0) {
$str += "${e}$($reset -join ';')m"
}
}
return $(if ($StringBuilder) { $StringBuilder.Append($str) } else { $str })
}
}
if ($Object -is [PoshGitTextSpan]) {
$bgColor = $Object.BackgroundColor
$fgColor = $Object.ForegroundColor
$Object = $Object.Text
}
$writeHostParams = @{
Object = $Object;
NoNewLine = $true;
}
if ($bgColor -and ($bgColor -ge 0) -and ($bgColor -le 15)) {
$writeHostParams.BackgroundColor = $bgColor
}
if ($fgColor -and ($fgColor -ge 0) -and ($fgColor -le 15)) {
$writeHostParams.ForegroundColor = $fgColor
}
Write-Host @writeHostParams
return $(if ($StringBuilder) { $StringBuilder } else { "" })
}
<#
.SYNOPSIS
Writes the Git status for repo. Typically, you use Write-VcsStatus
function instead of this one.
.DESCRIPTION
Writes the Git status for repo. This includes the branch name, branch
status with respect to its remote (if exists), index status, working
dir status, working dir local status and stash count (optional).
Various settings from GitPromptSettngs are used to format and color
the Git status.
On systems that support ANSI terminal sequences, this method will
return a string containing ANSI sequences to color various parts of
the Git status string. This string can be written to the host and
the ANSI sequences will be interpreted and converted to the specified
behaviors which is typically setting the foreground and/or background
color of text.
.EXAMPLE
PS C:\> Write-GitStatus (Get-GitStatus)
Writes the Git status for the current repo.
.INPUTS
System.Management.Automation.PSCustomObject
This is PSCustomObject returned by Get-GitStatus
.OUTPUTS
System.String
This command returns a System.String object.
#>
function Write-GitStatus {
param(
# The Git status object that provides the status information to be written.
# This object is retrieved via the Get-GitStatus command.
[Parameter(Position = 0)]
$Status
)
$s = $global:GitPromptSettings
if (!$Status -or !$s) {
return
}
$sb = [System.Text.StringBuilder]::new(150)
# When prompt is first (default), place the separator before the status summary
if (!$s.DefaultPromptWriteStatusFirst) {
$sb | Write-Prompt $s.PathStatusSeparator.Expand() > $null
}
$sb | Write-Prompt $s.BeforeStatus > $null
$sb | Write-GitBranchName $Status -NoLeadingSpace > $null
$sb | Write-GitBranchStatus $Status > $null
$sb | Write-Prompt $s.BeforeIndex > $null
if ($s.EnableFileStatus -and $Status.HasIndex) {
$sb | Write-GitIndexStatus $Status > $null
if ($Status.HasWorking) {
$sb | Write-Prompt $s.DelimStatus > $null
}
}
if ($s.EnableFileStatus -and $Status.HasWorking) {
$sb | Write-GitWorkingDirStatus $Status > $null
}
$sb | Write-GitWorkingDirStatusSummary $Status > $null
if ($s.EnableStashStatus -and ($Status.StashCount -gt 0)) {
$sb | Write-GitStashCount $Status > $null
}
$sb | Write-Prompt $s.AfterStatus > $null
# When status is first, place the separator after the status summary
if ($s.DefaultPromptWriteStatusFirst) {
$sb | Write-Prompt $s.PathStatusSeparator.Expand() > $null
}
if ($sb.Length -gt 0) {
$sb.ToString()
}
}
<#
.SYNOPSIS
Formats the branch name text according to $GitPromptSettings.
.DESCRIPTION
Formats the branch name text according the $GitPromptSettings:
BranchNameLimit and TruncatedBranchSuffix.
.EXAMPLE
PS C:\> $branchName = Format-GitBranchName (Get-GitStatus).Branch
Gets the branch name formatted as specified by the user's $GitPromptSettings.
.INPUTS
System.String
This is the branch name as a string.
.OUTPUTS
System.String
This command returns a System.String object.
#>
function Format-GitBranchName {
param(
# The branch name to format according to the GitPromptSettings:
# BranchNameLimit and TruncatedBranchSuffix.
[Parameter(Position=0)]
[string]
$BranchName
)
$s = $global:GitPromptSettings
if (!$s -or !$BranchName) {
return "$BranchName"
}
$res = $BranchName
if (($s.BranchNameLimit -gt 0) -and ($BranchName.Length -gt $s.BranchNameLimit))
{
$res = "{0}{1}" -f $BranchName.Substring(0, $s.BranchNameLimit), $s.TruncatedBranchSuffix
}
$res
}
<#
.SYNOPSIS
Gets the colors to use for the branch status.
.DESCRIPTION
Gets the colors to use for the branch status. This color is typically
used for the branch name as well. The default color is specified by
$GitPromptSettins.BranchColor. But depending on the Git status object
passed in, the colors could be changed to match that of one these
other $GitPromptSettings: BranchBehindAndAheadStatusSymbol,
BranchBehindStatusSymbol or BranchAheadStatusSymbol.
.EXAMPLE
PS C:\> $branchStatusColor = Get-GitBranchStatusColor (Get-GitStatus)
Returns a PoshGitTextSpan with the foreground and background colors
for the branch status.
.INPUTS
System.Management.Automation.PSCustomObject
This is PSCustomObject returned by Get-GitStatus
.OUTPUTS
PoshGitTextSpan
A PoshGitTextSpan with colors reflecting those to be used by
branch status symbols.
#>
function Get-GitBranchStatusColor {
param(
# The Git status object that provides branch status information.
# This object is retrieved via the Get-GitStatus command.
[Parameter(Position = 0)]
$Status
)
$s = $global:GitPromptSettings
if (!$s) {
return [PoshGitTextSpan]::new()
}
$branchStatusTextSpan = [PoshGitTextSpan]::new($s.BranchColor)
if (($Status.BehindBy -ge 1) -and ($Status.AheadBy -ge 1)) {
# We are both behind and ahead of remote
$branchStatusTextSpan = [PoshGitTextSpan]::new($s.BranchBehindAndAheadStatusSymbol)
}
elseif ($Status.BehindBy -ge 1) {
# We are behind remote
$branchStatusTextSpan = [PoshGitTextSpan]::new($s.BranchBehindStatusSymbol)
}
elseif ($Status.AheadBy -ge 1) {
# We are ahead of remote
$branchStatusTextSpan = [PoshGitTextSpan]::new($s.BranchAheadStatusSymbol)
}
$branchStatusTextSpan.Text = ''
$branchStatusTextSpan
}
<#
.SYNOPSIS
Writes the branch name given the current Git status.
.DESCRIPTION
Writes the branch name given the current Git status which can retrieved
via the Get-GitStatus command. Branch name can be affected by the
$GitPromptSettings: BranchColor, BranchNameLimit, TruncatedBranchSuffix
and Branch*StatusSymbol colors.
.EXAMPLE
PS C:\> Write-GitBranchName (Get-GitStatus)
Writes the name of the current branch.
.INPUTS
System.Management.Automation.PSCustomObject
This is PSCustomObject returned by Get-GitStatus
.OUTPUTS
System.String, System.Text.StringBuilder
This command returns a System.String object unless the -StringBuilder parameter
is supplied. In this case, it returns a System.Text.StringBuilder.
#>
function Write-GitBranchName {
param(
# The Git status object that provides the status information to be written.
# This object is retrieved via the Get-GitStatus command.
[Parameter(Position = 0)]
$Status,
# If specified the branch name is written into the provided StringBuilder object.
[Parameter(ValueFromPipeline = $true)]
[System.Text.StringBuilder]
$StringBuilder,
# If specified, suppresses the output of the leading space character.
[Parameter()]
[switch]
$NoLeadingSpace
)
$s = $global:GitPromptSettings
if (!$Status -or !$s) {
return $(if ($StringBuilder) { $StringBuilder } else { "" })
}
$str = ""
# Use the branch status colors (or CustomAnsi) to display the branch name
$branchNameTextSpan = Get-GitBranchStatusColor $Status
$branchNameTextSpan.Text = Format-GitBranchName $Status.Branch
if (!$NoLeadingSpace) {
$branchNameTextSpan.Text = " " + $branchNameTextSpan.Text
}
if ($StringBuilder) {
$StringBuilder | Write-Prompt $branchNameTextSpan > $null
}
else {
$str = Write-Prompt $branchNameTextSpan
}
return $(if ($StringBuilder) { $StringBuilder } else { $str })
}
<#
.SYNOPSIS
Writes the branch status text given the current Git status.
.DESCRIPTION
Writes the branch status text given the current Git status which can retrieved
via the Get-GitStatus command. Branch status includes information about the
upstream branch, how far behind and/or ahead the local branch is from the remote.
.EXAMPLE
PS C:\> Write-GitBranchStatus (Get-GitStatus)
Writes the status of the current branch to the host.
.INPUTS
System.Management.Automation.PSCustomObject
This is PSCustomObject returned by Get-GitStatus
.OUTPUTS
System.String, System.Text.StringBuilder
This command returns a System.String object unless the -StringBuilder parameter
is supplied. In this case, it returns a System.Text.StringBuilder.
#>
function Write-GitBranchStatus {
param(
# The Git status object that provides the status information to be written.
# This object is retrieved via the Get-GitStatus command.
[Parameter(Position = 0)]
$Status,
# If specified the branch status is written into the provided StringBuilder object.
[Parameter(ValueFromPipeline = $true)]
[System.Text.StringBuilder]
$StringBuilder,
# If specified, suppresses the output of the leading space character.
[Parameter()]
[switch]
$NoLeadingSpace
)
$s = $global:GitPromptSettings
if (!$Status -or !$s) {
return $(if ($StringBuilder) { $StringBuilder } else { "" })
}
$branchStatusTextSpan = Get-GitBranchStatusColor $Status
if (!$Status.Upstream) {
$branchStatusTextSpan.Text = $s.BranchUntrackedText
}
elseif ($Status.UpstreamGone -eq $true) {
# Upstream branch is gone
$branchStatusTextSpan.Text = $s.BranchGoneStatusSymbol.Text
}
elseif (($Status.BehindBy -eq 0) -and ($Status.AheadBy -eq 0)) {
# We are aligned with remote
$branchStatusTextSpan.Text = $s.BranchIdenticalStatusSymbol.Text
}
elseif (($Status.BehindBy -ge 1) -and ($Status.AheadBy -ge 1)) {
# We are both behind and ahead of remote
if ($s.BranchBehindAndAheadDisplay -eq "Full") {
$branchStatusTextSpan.Text = ("{0}{1} {2}{3}" -f $s.BranchBehindStatusSymbol.Text, $Status.BehindBy, $s.BranchAheadStatusSymbol.Text, $status.AheadBy)
}
elseif ($s.BranchBehindAndAheadDisplay -eq "Compact") {
$branchStatusTextSpan.Text = ("{0}{1}{2}" -f $Status.BehindBy, $s.BranchBehindAndAheadStatusSymbol.Text, $Status.AheadBy)
}
else {
$branchStatusTextSpan.Text = $s.BranchBehindAndAheadStatusSymbol.Text
}
}
elseif ($Status.BehindBy -ge 1) {
# We are behind remote
if (($s.BranchBehindAndAheadDisplay -eq "Full") -Or ($s.BranchBehindAndAheadDisplay -eq "Compact")) {
$branchStatusTextSpan.Text = ("{0}{1}" -f $s.BranchBehindStatusSymbol.Text, $Status.BehindBy)
}
else {
$branchStatusTextSpan.Text = $s.BranchBehindStatusSymbol.Text
}
}
elseif ($Status.AheadBy -ge 1) {
# We are ahead of remote
if (($s.BranchBehindAndAheadDisplay -eq "Full") -or ($s.BranchBehindAndAheadDisplay -eq "Compact")) {
$branchStatusTextSpan.Text = ("{0}{1}" -f $s.BranchAheadStatusSymbol.Text, $Status.AheadBy)
}
else {
$branchStatusTextSpan.Text = $s.BranchAheadStatusSymbol.Text
}
}
else {
# This condition should not be possible but defaulting the variables to be safe
$branchStatusTextSpan.Text = "?"
}
$str = ""
if ($branchStatusTextSpan.Text) {
$textSpan = [PoshGitTextSpan]::new($branchStatusTextSpan)
if (!$NoLeadingSpace) {
$textSpan.Text = " " + $branchStatusTextSpan.Text
}
if ($StringBuilder) {
$StringBuilder | Write-Prompt $textSpan > $null
}
else {
$str = Write-Prompt $textSpan
}
}
return $(if ($StringBuilder) { $StringBuilder } else { $str })
}
<#
.SYNOPSIS
Writes the index status text given the current Git status.
.DESCRIPTION
Writes the index status text given the current Git status.
.EXAMPLE
PS C:\> Write-GitIndexStatus (Get-GitStatus)
Writes the Git index status to the host.
.INPUTS
System.Management.Automation.PSCustomObject
This is PSCustomObject returned by Get-GitStatus
.OUTPUTS
System.String, System.Text.StringBuilder
This command returns a System.String object unless the -StringBuilder parameter
is supplied. In this case, it returns a System.Text.StringBuilder.
#>
function Write-GitIndexStatus {
param(
# The Git status object that provides the status information to be written.
# This object is retrieved via the Get-GitStatus command.
[Parameter(Position = 0)]
$Status,
# If specified the index status is written into the provided StringBuilder object.
[Parameter(ValueFromPipeline = $true)]
[System.Text.StringBuilder]
$StringBuilder,
# If specified, suppresses the output of the leading space character.
[Parameter()]
[switch]
$NoLeadingSpace
)
$s = $global:GitPromptSettings
if (!$Status -or !$s) {
return $(if ($StringBuilder) { $StringBuilder } else { "" })
}
$str = ""
if ($Status.HasIndex) {
if ($s.ShowStatusWhenZero -or $Status.Index.Added) {
$indexStatusText = " "
if ($NoLeadingSpace) {
$indexStatusText = ""
$NoLeadingSpace = $false
}
$indexStatusText += "$($s.FileAddedText)$($Status.Index.Added.Count)"
if ($StringBuilder) {
$StringBuilder | Write-Prompt $indexStatusText -Color $s.IndexColor > $null
}
else {
$str += Write-Prompt $indexStatusText -Color $s.IndexColor
}
}
if ($s.ShowStatusWhenZero -or $status.Index.Modified) {
$indexStatusText = " "
if ($NoLeadingSpace) {
$indexStatusText = ""
$NoLeadingSpace = $false
}
$indexStatusText += "$($s.FileModifiedText)$($status.Index.Modified.Count)"
if ($StringBuilder) {
$StringBuilder | Write-Prompt $indexStatusText -Color $s.IndexColor > $null
}
else {
$str += Write-Prompt $indexStatusText -Color $s.IndexColor
}
}
if ($s.ShowStatusWhenZero -or $Status.Index.Deleted) {
$indexStatusText = " "
if ($NoLeadingSpace) {
$indexStatusText = ""
$NoLeadingSpace = $false
}
$indexStatusText += "$($s.FileRemovedText)$($Status.Index.Deleted.Count)"
if ($StringBuilder) {
$StringBuilder | Write-Prompt $indexStatusText -Color $s.IndexColor > $null
}
else {
$str += Write-Prompt $indexStatusText -Color $s.IndexColor
}
}
if ($Status.Index.Unmerged) {
$indexStatusText = " "
if ($NoLeadingSpace) {
$indexStatusText = ""
$NoLeadingSpace = $false
}
$indexStatusText += "$($s.FileConflictedText)$($Status.Index.Unmerged.Count)"
if ($StringBuilder) {
$StringBuilder | Write-Prompt $indexStatusText -Color $s.IndexColor > $null
}
else {
$str += Write-Prompt $indexStatusText -Color $s.IndexColor
}
}
}
return $(if ($StringBuilder) { $StringBuilder } else { $str })
}
<#
.SYNOPSIS
Writes the working directory status text given the current Git status.
.DESCRIPTION
Writes the working directory status text given the current Git status.
.EXAMPLE
PS C:\> Write-GitWorkingDirStatus (Get-GitStatus)
Writes the Git working directory status to the host.
.INPUTS
System.Management.Automation.PSCustomObject
This is PSCustomObject returned by Get-GitStatus
.OUTPUTS
System.String, System.Text.StringBuilder
This command returns a System.String object unless the -StringBuilder parameter
is supplied. In this case, it returns a System.Text.StringBuilder.
#>
function Write-GitWorkingDirStatus {
param(
# The Git status object that provides the status information to be written.
# This object is retrieved via the Get-GitStatus command.
[Parameter(Position = 0)]
$Status,
# If specified the working dir status is written into the provided StringBuilder object.
[Parameter(ValueFromPipeline = $true)]
[System.Text.StringBuilder]
$StringBuilder,
# If specified, suppresses the output of the leading space character.
[Parameter()]
[switch]
$NoLeadingSpace
)
$s = $global:GitPromptSettings
if (!$Status -or !$s) {
return $(if ($StringBuilder) { $StringBuilder } else { "" })
}
$str = ""
if ($Status.HasWorking) {
if ($s.ShowStatusWhenZero -or $Status.Working.Added) {
$workingStatusText = " "
if ($NoLeadingSpace) {
$workingStatusText = ""
$NoLeadingSpace = $false
}
$workingStatusText += "$($s.FileAddedText)$($Status.Working.Added.Count)"
if ($StringBuilder) {
$StringBuilder | Write-Prompt $workingStatusText -Color $s.WorkingColor > $null
}
else {
$str += Write-Prompt $workingStatusText -Color $s.WorkingColor
}
}
if ($s.ShowStatusWhenZero -or $Status.Working.Modified) {
$workingStatusText = " "
if ($NoLeadingSpace) {
$workingStatusText = ""
$NoLeadingSpace = $false
}
$workingStatusText += "$($s.FileModifiedText)$($Status.Working.Modified.Count)"
if ($StringBuilder) {
$StringBuilder | Write-Prompt $workingStatusText -Color $s.WorkingColor > $null
}
else {
$str += Write-Prompt $workingStatusText -Color $s.WorkingColor
}
}
if ($s.ShowStatusWhenZero -or $Status.Working.Deleted) {
$workingStatusText = " "
if ($NoLeadingSpace) {
$workingStatusText = ""
$NoLeadingSpace = $false
}
$workingStatusText += "$($s.FileRemovedText)$($Status.Working.Deleted.Count)"
if ($StringBuilder) {
$StringBuilder | Write-Prompt $workingStatusText -Color $s.WorkingColor > $null
}
else {
$str += Write-Prompt $workingStatusText -Color $s.WorkingColor
}
}
if ($Status.Working.Unmerged) {
$workingStatusText = " "
if ($NoLeadingSpace) {
$workingStatusText = ""
$NoLeadingSpace = $false
}
$workingStatusText += "$($s.FileConflictedText)$($Status.Working.Unmerged.Count)"
if ($StringBuilder) {
$StringBuilder | Write-Prompt $workingStatusText -Color $s.WorkingColor > $null
}
else {
$str += Write-Prompt $workingStatusText -Color $s.WorkingColor
}
}
}
return $(if ($StringBuilder) { $StringBuilder } else { $str })
}
<#
.SYNOPSIS
Writes the working directory status summary text given the current Git status.
.DESCRIPTION
Writes the working directory status summary text given the current Git status.
If there are any unstaged commits, the $GitPromptSettings.LocalWorkingStatusSymbol
will be output. If not, then if are any staged but uncommmited changes, the
$GitPromptSettings.LocalStagedStatusSymbol will be output. If not, then
$GitPromptSettings.LocalDefaultStatusSymbol will be output.
.EXAMPLE
PS C:\> Write-GitWorkingDirStatusSummary (Get-GitStatus)
Outputs the Git working directory status summary text.
.INPUTS
System.Management.Automation.PSCustomObject
This is PSCustomObject returned by Get-GitStatus
.OUTPUTS
System.String, System.Text.StringBuilder
This command returns a System.String object unless the -StringBuilder parameter
is supplied. In this case, it returns a System.Text.StringBuilder.
#>
function Write-GitWorkingDirStatusSummary {
param(
# The Git status object that provides the status information to be written.
# This object is retrieved via the Get-GitStatus command.
[Parameter(Position = 0)]
$Status,
# If specified the working dir local status is written into the provided StringBuilder object.
[Parameter(ValueFromPipeline = $true)]
[System.Text.StringBuilder]
$StringBuilder,
# If specified, suppresses the output of the leading space character.
[Parameter()]
[switch]
$NoLeadingSpace
)
$s = $global:GitPromptSettings
if (!$Status -or !$s) {
return $(if ($StringBuilder) { $StringBuilder } else { "" })
}
$str = ""
# No uncommited changes
$localStatusSymbol = $s.LocalDefaultStatusSymbol
if ($Status.HasWorking) {
# We have un-staged files in the working tree
$localStatusSymbol = $s.LocalWorkingStatusSymbol
}
elseif ($Status.HasIndex) {
# We have staged but uncommited files
$localStatusSymbol = $s.LocalStagedStatusSymbol
}
if ($localStatusSymbol.Text) {
$textSpan = [PoshGitTextSpan]::new($localStatusSymbol)
if (!$NoLeadingSpace) {
$textSpan.Text = " " + $localStatusSymbol.Text
}
if ($StringBuilder) {
$StringBuilder | Write-Prompt $textSpan > $null
}
else {
$str += Write-Prompt $textSpan
}
}
return $(if ($StringBuilder) { $StringBuilder } else { $str })
}
<#
.SYNOPSIS
Writes the stash count given the current Git status.
.DESCRIPTION
Writes the stash count given the current Git status.
.EXAMPLE
PS C:\> Write-GitStashCount (Get-GitStatus)
Writes the Git stash count to the host.
.INPUTS
System.Management.Automation.PSCustomObject
This is PSCustomObject returned by Get-GitStatus
.OUTPUTS
System.String, System.Text.StringBuilder
This command returns a System.String object unless the -StringBuilder parameter
is supplied. In this case, it returns a System.Text.StringBuilder.
#>
function Write-GitStashCount {
param(
# The Git status object that provides the status information to be written.
# This object is retrieved via the Get-GitStatus command.
[Parameter(Position = 0)]
$Status,
# If specified the working dir local status is written into the provided StringBuilder object.
[Parameter(ValueFromPipeline = $true)]
[System.Text.StringBuilder]
$StringBuilder
)
$s = $global:GitPromptSettings
if (!$Status -or !$s) {
return $(if ($StringBuilder) { $StringBuilder } else { "" })
}
$str = ""
if ($Status.StashCount -gt 0) {
$stashText = "$($Status.StashCount)"
if ($StringBuilder) {
$StringBuilder | Write-Prompt $s.BeforeStash > $null
$StringBuilder | Write-Prompt $stashText -Color $s.StashColor > $null
$StringBuilder | Write-Prompt $s.AfterStash > $null
}
else {
$str += Write-Prompt $s.BeforeStash
$str += Write-Prompt $stashText -Color $s.StashColor
$str += Write-Prompt $s.AfterStash
}
}
return $(if ($StringBuilder) { $StringBuilder } else { $str })
}
if (!(Test-Path Variable:Global:VcsPromptStatuses)) {
$global:VcsPromptStatuses = @()
}
<#
.SYNOPSIS
Writes all version control prompt statuses configured in $global:VcsPromptStatuses.
.DESCRIPTION
Writes all version control prompt statuses configured in $global:VcsPromptStatuses.
By default, this includes the PoshGit prompt status.
.EXAMPLE
PS C:\> Write-VcsStatus
Writes all version control prompt statuses that have been configured
with the global variable $VcsPromptStatuses
#>
function Global:Write-VcsStatus {
Set-ConsoleMode -ANSI
$OFS = ""
$sb = [System.Text.StringBuilder]::new(256)
foreach ($promptStatus in $global:VcsPromptStatuses) {
[void]$sb.Append("$(& $promptStatus)")
}
if ($sb.Length -gt 0) {
$sb.ToString()
}
}
# Add scriptblock that will execute for Write-VcsStatus
$PoshGitVcsPrompt = {
try {
$global:GitStatus = Get-GitStatus
Write-GitStatus $GitStatus
}
catch {
$s = $global:GitPromptSettings
if ($s) {
$errorText = "PoshGitVcsPrompt error: $_"
$sb = [System.Text.StringBuilder]::new()
# When prompt is first (default), place the separator before the status summary
if (!$s.DefaultPromptWriteStatusFirst) {
$sb | Write-Prompt $s.PathStatusSeparator.Expand() > $null
}
$sb | Write-Prompt $s.BeforeStatus > $null
$sb | Write-Prompt $errorText -Color $s.ErrorColor > $null
if ($s.Debug) {
if (!$s.AnsiConsole) { Write-Host }
Write-Verbose "PoshGitVcsPrompt error details: $($_ | Format-List * -Force | Out-String)" -Verbose
}
$sb | Write-Prompt $s.AfterStatus > $null
if ($sb.Length -gt 0) {
$sb.ToString()
}
}
}
}
$global:VcsPromptStatuses += $PoshGitVcsPrompt