-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathDo-Progress.psm1
150 lines (141 loc) · 5.82 KB
/
Do-Progress.psm1
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
function Do-Progress {
[CmdletBinding()]
param(
[Parameter(Mandatory=$true)]
[int]$count,
[Parameter(Mandatory=$true)]
[int]$total,
[Parameter(Mandatory=$false)]
[int]$started,
[Parameter(Mandatory=$false)]
[switch]$NoClutter
)
# Calculate percentage
if($total -lt 1) {
[int]$percentComplete = 100
} else {
[int]$percentComplete = 100 / $total * $count
}
# Keep the actual percentage for displaying number on screen
$displayPercent = $percentComplete
# Make 100% equal to 50, as it will fit a default PowerShell window. 100 characters would be excessive.
[int]$percentComplete = $percentComplete / 2
# Empty variables from previous run
# Used to create 'chunks' to display in console with the least number of Write-Hosts. Significantly increases performance.
$completeBarPre = ""
$completeBarPost = ""
$completeString = ""
$startedBarPre = ""
$startedBarPost = ""
$startedString = ""
$incompleteBarPre = ""
$incompleteBarPost = ""
$incompleteString = ""
if ($displayPercent -eq $Global:LAST_PERCENT -and $NoClutter) {
# Do nothing. This will make significantly less draw calls, especially on large jobs.
# Only applies in NoClutter mode as I can't think of an efficient way to update the stats without re-drawing the bar.
} else {
$Global:LAST_PERCENT = $displayPercent
# Calculate what character should be used in each position
for($i = 0; $i -le $percentComplete; $i++) {
if ($i -eq 24) {
$completeString += $displayPercent.ToString()[0]
} elseif ($i -eq 25){
$completeString += $displayPercent.ToString()[1]
} elseif ($i -eq 26){
if($displayPercent -lt 100) {
$completeString += "%"
} else {
$completeString += $displayPercent.ToString()[2]
}
} elseif ($i -eq 27){
if($displayPercent -eq 100) {
$completeString += "%"
} else {
$completeBarPost += "-"
}
} else {
if ($i -lt 24) {
$completeBarPre += "-"
} else {
$completeBarPost += "-"
}
}
}
if($started) {
[int]$percentStarted = 100 / $total * $started
[int]$percentStarted = $percentStarted / 2
for(; $i -le $percentStarted; $i++) {
if ($i -eq 24) {
$startedString += $displayPercent.ToString()[0]
} elseif ($i -eq 25){
$startedString += $displayPercent.ToString()[1]
} elseif ($i -eq 26){
if($displayPercent -lt 100) {
$startedString += "%"
} else {
$startedString += $displayPercent.ToString()[2]
}
} elseif ($i -eq 27){
if($displayPercent -eq 100) {
$startedString += "%"
} else {
$startedBarPost += "-"
}
} else {
if ($i -lt 24) {
$startedBarPre += "-"
} else {
$startedBarPost += "-"
}
}
}
}
for(;$i -le 50; $i++) {
if ($i -eq 24) {
$incompleteString += $displayPercent.ToString()[0]
} elseif ($i -eq 25){
$incompleteString += $displayPercent.ToString()[1]
} elseif ($i -eq 26){
$incompleteString += "%"
} else {
if ($i -lt 24) {
$incompleteBarPre += "-"
} else {
$incompleteBarPost += "-"
}
}
}
# Return carriage. Allows us to overwrite the current line within the console.
Write-Host "`r" -NoNewline
# Draw the loading bar. Unsure if it would be more efficient to check if Post vars are empty and exclude if so. Will have to test.
Write-Host "[" -NoNewline -BackgroundColor Black
Write-Host $completeBarPre -BackgroundColor Green -ForegroundColor Green -NoNewline
Write-Host $completeString -BackgroundColor Green -ForegroundColor Black -NoNewline
Write-Host $completeBarPost -BackgroundColor Green -ForegroundColor Green -NoNewline
Write-Host $startedBarPre -BackgroundColor Yellow -ForegroundColor Yellow -NoNewline
Write-Host $startedString -BackgroundColor Yellow -ForegroundColor Black -NoNewline
Write-Host $startedBarPost -BackgroundColor Yellow -ForegroundColor Yellow -NoNewline
Write-Host $incompleteBarPre -BackgroundColor Red -ForegroundColor Red -NoNewline
Write-Host $incompleteString -BackgroundColor Red -ForegroundColor White -NoNewline
Write-Host $incompleteBarPost -BackgroundColor Red -ForegroundColor Red -NoNewline
Write-Host "]" -NoNewline -BackgroundColor Black
}
# Append the stats to the end of the loading bar
if (!$NoClutter) {
if ($started) {
Write-Host " Completed: $count, Started: $started, Total: $total" -NoNewline #"`r"
} else {
Write-Host " Completed: $count, Total: $total" -NoNewline #"`r"
}
}
if($count -eq $total){
# Go to new line, so external script doesn't write over the bar or on same line
# Write another line so there's an empty line after the loading bar. Looks far cleaner.
Write-Host ""
Write-Host ""
$Global:LAST_PERCENT = $null
}
}
# Export the function
Export-ModuleMember -Function *