-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathWinGet-Utils.psm1
357 lines (310 loc) · 9.16 KB
/
WinGet-Utils.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
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
Set-StrictMode -Version 3
[string]$IgnoreFilePath = "$PSScriptRoot\winget.{HOSTNAME}.ignore"
<#
.DESCRIPTION
Test whether the current instance of PowerShell is an administrator.
#>
function Test-Administrator
{
$user = [Security.Principal.WindowsIdentity]::GetCurrent()
return (New-Object Security.Principal.WindowsPrincipal $user).IsInRole([Security.Principal.WindowsBuiltinRole]::Administrator)
}
<#
.DESCRIPTION
Test whether the specified file path points to a reparse point (i.e. symlink
or hardlink).
#>
function Test-ReparsePoint
{
param(
[string]$FilePath
)
$file = Get-Item $FilePath -Force -ea SilentlyContinue
return [bool]($file.Attributes -band [IO.FileAttributes]::ReparsePoint)
}
<#
.DESCRIPTION
Gets a list of winget upgrade IDs to ignore. If the file does not exist,
attempt to locate the file in a prior module install. If one exists, a
SymLink will be created if the source itself was a SymLink; otherwise, it
will be copied to the current module version's folder. In cases, where a
SymLink is being created, the user must be an administrator, if not, the
operation will be skipped and a warning will be emitted.
#>
function Get-WinGetSoftwareIgnores
{
Initialize-WinGetIgnore | Out-Null
$ignoreFile = $IgnoreFilePath.Replace('{HOSTNAME}', $(hostname).ToLower())
if (-not(Test-Path $ignoreFile)) {
return $null
}
return Get-Content $ignoreFile
}
<#
.DESCRIPTION
Enter the "Alternate Screen Buffer".
#>
function Enter-AltScreenBuffer
{
$Host.UI.Write([char]27 + "[?1049h")
}
<#
.DESCRIPTION
Exit the "Alternate Screen Buffer".
#>
function Exit-AltScreenBuffer
{
$Host.UI.Write([char]27 + "[?1049l")
}
<#
.DESCRIPTION
Show the terminal cursor.
#>
function Show-TerminalCursor
{
$Host.UI.Write([char]27 + "[?25h")
}
<#
.DESCRIPTION
Hide the terminal cursor.
#>
function Hide-TerminalCursor
{
$Host.UI.Write([char]27 + "[?25l")
}
<#
.DESCRIPTION
Start the display of busy indicator on supported terminals.
#>
function Start-TerminalBusy
{
#https://learn.microsoft.com/en-us/windows/terminal/tutorials/progress-bar-sequences
$Host.UI.Write([char]27 + "]9;4;3;0" + [char]7)
}
<#
.DESCRIPTION
Stop the display of busy indicator on supported terminals.
#>
function Stop-TerminalBusy
{
$Host.UI.Write([char]27 + "]9;4;0;0" + [char]7)
}
<#
.DESCRIPTION
Draws job progress indicators.
#>
function Show-JobProgress
{
param(
[System.Management.Automation.Job]$Job
)
$progressIndicator = @('|', '/', '-', '\')
$progressIter = 0
try {
Start-TerminalBusy
Hide-TerminalCursor
while ($Job.JobStateInfo.State -eq "Running") {
$progressIter = ($progressIter + 1) % $progressIndicator.Count
Write-Host "$($progressIndicator[$progressIter])`b" -NoNewline
Start-Sleep -Milliseconds 125
}
}
finally {
Write-Host " `b" -NoNewline
Stop-TerminalBusy
}
}
<#
.DESCRIPTION
Creates a hyperlink text entry.
#>
function New-HyperLinkText
{
param(
[string]$Url,
[string]$Label
)
"`e]8;;$Url`e\$Label`e]8;;`e\"
}
<#
.DESCRIPTION
Waits for user to press the ENTER key.
#>
function Wait-ConsoleKeyEnter
{
while ($Host.ui.RawUI.ReadKey('NoEcho,IncludeKeyDown').VirtualKeyCode -ne [ConsoleKey]::Enter) {}
}
<#
.DESCRIPTION
Check permissions for creating symbolic links.
#>
function Test-CreateSymlink
{
$success = $true
$symLinkArgs = @{
ItemType = "SymbolicLink"
Path = "$(Split-Path -Parent $PSScriptRoot)"
Name = "check.tmp"
Value = ""
Force = $true
}
try {
$tempFile = New-TemporaryFile
$symLinkArgs.Value = $tempFile.FullName
$symlinkFile = New-Item @symLinkArgs
Remove-Item -Path $symlinkFile
} catch {
$success = $true
} finally {
Remove-Item -Path $tempFile
}
return $success
}
<#
.DESCRIPTION
Displays and services a simple pager UI.
#>
function Show-Paginated
{
param (
[string]$Title,
[string[]]$TextData
)
<#
.DESCRIPTION
Writes the frame buffer to output.
#>
function Show-Frame
{
param(
[string[]]$FrameBuffer,
[int]$FrameWidth
)
$Host.UI.RawUI.CursorPosition = @{ X = 0; Y = 0 }
$FrameBuffer | ForEach-Object {
if ($_.Length -lt $FrameWidth) {
($_ + (' ' * $FrameWidth - $_.Length))
} else {
$_
}
} | ForEach-Object {
Write-Host -NoNewline $_
}
}
<#
.DESCRIPTION
Prepares a line for output to the console. The line is constrained
to the specified width and will be truncated if it exceeds that length.
#>
function Format-LineForConsole
{
param(
[string]$Line,
[int]$FrameWidth
)
if ($Line.Length -gt $FrameWidth) {
# Truncate to fit width
$Line = "$($Line.Substring(0, $FrameWidth - 1))…"
} else {
# Pad the tail to fit $FrameWidth
$Line = $Line + (' ' * ($FrameWidth - $Line.Length))
}
return $Line
}
<#
.DESCRIPTION
Prepares an array of strings for pagination by pre-rendering the word
wrapped output according to the specified width.
#>
function Format-ContentForConsole
{
param (
[string[]]$Content,
[int]$FrameWidth
)
$halfFrameWidth = [Math]::Floor($FrameWidth / 2)
$newContent = @()
foreach ($line in $Content) {
$line = $line.TrimEnd()
if ($line.Length -gt $FrameWidth) {
while ($line.Length -gt $FrameWidth) {
$splitIndex = $line.LastIndexOf(' ', $FrameWidth)
if (($splitIndex -eq -1) -or ($splitIndex -lt $halfFrameWidth)) {
$splitIndex = $FrameWidth
}
$newContent += $line.Substring(0, $splitIndex)
$line = $line.Substring($splitIndex).TrimStart()
}
}
$newContent += $line
}
return $newContent
}
$currentIndex = 0
$reservedLines = 2 # Two lines removed for title and footer rows.
$lastWidth = 0
$header = ''
$content = ''
$footer = ''
$controls = 'Use ⇧/⇩ arrows to scroll, PAGE UP/PAGE DOWN to jump, Q to quit.'
while ($true) {
$pageSize = $Host.UI.RawUI.WindowSize.Height - $reservedLines
$width = $Host.UI.RawUI.WindowSize.Width
if ($width -ne $lastWidth) {
$header = Format-LineForConsole -Line $Title -FrameWidth $width
$content = Format-ContentForConsole -Content $TextData -FrameWidth $width
$footer = Format-LineForConsole -Line $controls -FrameWidth $width
}
# Add the title line to the frame buffer.
[string[]]$frameBuffer = @('')
$frameBuffer += "$($PSStyle.Foreground.Black)$($PSStyle.Background.BrightWhite)$header$($PSStyle.Reset)`n"
# Add the in-view content lines to the frame buffer.
# NOTE: Text must be formatted to account for word wrapping consuming additional lines.
$endIndex = [Math]::Min($currentIndex + $pageSize, $content.Length) - 1
$content[$currentIndex..$endIndex] | ForEach-Object { $frameBuffer += "$(Format-LineForConsole -Line $_ -FrameWidth $width)`n" }
# Add pad lines to the frame buffer.
$padLines = $pageSize - ($endIndex - $currentIndex) - 1
$padLine = "$(' ' * $width)`n"
for ($i = 0; $i -lt $padLines; $i++) { $frameBuffer += $padLine }
# Add footer line to the frame buffer.
$frameBuffer += "$($PSStyle.Foreground.Black)$($PSStyle.Background.BrightWhite)$footer$($PSStyle.Reset)"
Show-Frame -FrameBuffer $frameBuffer -Width $width
Hide-TerminalCursor
if (-not([Console]::KeyAvailable)) {
Start-Sleep -Milliseconds 10
continue
}
$key = [Console]::ReadKey($true)
$currentKey = [char]$key.Key
switch ($currentKey)
{
# Navigate up
{ $_ -eq [ConsoleKey]::UpArrow } {
if ($currentIndex -gt 0) { $currentIndex-- }
}
# Navigate down
{ $_ -eq [ConsoleKey]::DownArrow } {
if ($currentIndex -lt $content.Length - $reservedLines) { $currentIndex++ }
}
# Navigate up by one page
{ $_ -eq [ConsoleKey]::PageUp } {
if ($currentIndex -gt $pageSize) {
$currentIndex -= $pageSize
} else {
$currentIndex = 0
}
}
# Navigate down by one page
{ $_ -eq [ConsoleKey]::PageDown } {
if ($currentIndex + $pageSize -lt $content.Length) {
$currentIndex += $pageSize
} else {
$currentIndex = $content.Length - $reservedLines
}
}
{ $currentKey -eq 'Q' } {
return
}
}
}
}