-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmenu.ps1
More file actions
445 lines (378 loc) · 15.7 KB
/
Copy pathmenu.ps1
File metadata and controls
445 lines (378 loc) · 15.7 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
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
# menu.ps1 - Modern PowerShell Menu with Enhanced UI (PowerShell 5.1 Compatible)
#requires -Version 5.0
$ErrorActionPreference = "Stop"
$VerbosePreference = "Continue"
# Configuration
$config = @{
Title = "GitHub PowerShell Scripts"
ApiUrl = "https://api.github.com/repos/algiers/powershell-scripts/contents/Scripts"
Colors = @{
Primary = [System.ConsoleColor]::Cyan
Secondary = [System.ConsoleColor]::DarkCyan
Highlight = [System.ConsoleColor]::Green
Warning = [System.ConsoleColor]::Yellow
Error = [System.ConsoleColor]::Red
Text = [System.ConsoleColor]::White
Header = [System.ConsoleColor]::Magenta
}
Symbols = @{
Loading = "⠋⠙⠹⠸⠼⠴⠦⠧⠇⠏"
Selected = "►"
Bullet = "○"
Success = "✓"
Error = "✗"
Info = "ℹ"
}
WindowTitle = "PowerShell Script Manager"
}
# Set window title
$Host.UI.RawUI.WindowTitle = $config.WindowTitle
# Function to safely manage cursor visibility
function Set-CursorVisible {
param([bool]$Visible)
try {
$Host.UI.RawUI.CursorVisible = $Visible
return $true
} catch {
return $false
}
}
# Function to create a horizontal line
function New-HorizontalLine {
param (
[string]$Char = '─',
[int]$Length = ($Host.UI.RawUI.WindowSize.Width - 2)
)
return (-join ($Char * $Length))
}
# Function to center text
function Get-CenteredText {
param (
[string]$Text,
[int]$Width = ($Host.UI.RawUI.WindowSize.Width)
)
$padding = [Math]::Max(0, ($Width - $Text.Length) / 2)
return (-join (' ' * [Math]::Floor($padding))) + $Text
}
# Function to show a notification
function Show-Notification {
param (
[string]$Message,
[System.ConsoleColor]$Color = $config.Colors.Text,
[string]$Symbol = "",
[switch]$NoNewLine
)
if ($Symbol) {
Write-Host "$Symbol " -NoNewline -ForegroundColor $Color
}
if ($NoNewLine) {
Write-Host $Message -NoNewline -ForegroundColor $Color
}
else {
Write-Host $Message -ForegroundColor $Color
}
}
# Function to display an animated loading indicator
function Show-LoadingAnimation {
param (
[scriptblock]$ScriptBlock,
[string]$LoadingText = "Loading"
)
$cursorSupported = Set-CursorVisible $false
$job = Start-Job -ScriptBlock $ScriptBlock
$symbols = $config.Symbols.Loading.ToCharArray()
$i = 0
$dots = ""
try {
while ($job.State -eq 'Running') {
$dots += "."
if ($dots.Length -gt 3) { $dots = "." }
Write-Host "`r$($symbols[$i]) $LoadingText$dots" -NoNewline -ForegroundColor $config.Colors.Warning
Start-Sleep -Milliseconds 100
$i = ($i + 1) % $symbols.Length
}
Write-Host "`r `r" -NoNewline
$result = Receive-Job -Job $job -Wait -AutoRemoveJob
return $result
}
finally {
if ($cursorSupported) {
Set-CursorVisible $true
}
if ($null -ne $job) {
Remove-Job -Job $job -Force -ErrorAction SilentlyContinue
}
}
}
# Function to fetch scripts with a loading animation
function Get-Scripts {
$maxRetries = 3
$retryCount = 0
while ($retryCount -lt $maxRetries) {
$result = Show-LoadingAnimation -LoadingText "Fetching scripts" -ScriptBlock {
try {
$apiUrl = $using:config.ApiUrl
$scripts = Invoke-RestMethod -Uri $apiUrl -ErrorAction Stop
return $scripts | Where-Object { $_.name -match '\.ps1$' }
}
catch {
# Return the error instead of null
return @{ Error = $_ }
}
}
# Check if we got an error object back
if ($result -is [hashtable] -and $result.ContainsKey('Error')) {
$retryCount++
if ($retryCount -ge $maxRetries) {
Clear-Host
Show-Notification -Message "Failed to fetch scripts after $maxRetries attempts." -Color $config.Colors.Error -Symbol $config.Symbols.Error
Write-Host ""
Show-Notification -Message "Error details: $($result.Error.Message)" -Color $config.Colors.Error
Write-Host ""
Show-Notification -Message "Press any key to retry or Esc to exit..." -Color $config.Colors.Warning
$key = $Host.UI.RawUI.ReadKey('NoEcho,IncludeKeyDown')
if ($key.VirtualKeyCode -eq 27) {
return $null
}
$retryCount = 0 # Reset retry count if user wants to try again
}
else {
Show-Notification -Message "Connection attempt $retryCount failed. Retrying in 2 seconds..." -Color $config.Colors.Warning
Start-Sleep -Seconds 2
}
}
elseif ($null -eq $result -or $result.Count -eq 0) {
Show-Notification -Message "No PowerShell scripts found in the repository." -Color $config.Colors.Warning -Symbol $config.Symbols.Info
Start-Sleep -Seconds 2
Write-Host ""
Show-Notification -Message "Press any key to retry or Esc to exit..." -Color $config.Colors.Warning
$key = $Host.UI.RawUI.ReadKey('NoEcho,IncludeKeyDown')
if ($key.VirtualKeyCode -eq 27) {
return $null
}
$retryCount = 0 # Reset retry count if user wants to try again
}
else {
# Success - return the scripts
return $result
}
}
# If we've exhausted all retries and user chose not to continue
return $null
}
# Function to draw a box around text
function Show-TextBox {
param (
[string]$Title,
[string[]]$Content
)
$width = ($Host.UI.RawUI.WindowSize.Width - 4)
$contentWidth = $width - 2
# Calculate spacing
$titlePadding = [Math]::Max(0, ($width - $Title.Length - 2) / 2)
$titleLeft = [Math]::Floor($titlePadding)
# Top border with title
Write-Host " ┌" -NoNewline -ForegroundColor $config.Colors.Primary
Write-Host (-join ("─" * $titleLeft)) -NoNewline -ForegroundColor $config.Colors.Primary
Write-Host " $Title " -NoNewline -ForegroundColor $config.Colors.Header
Write-Host (-join ("─" * ($width - $titleLeft - $Title.Length - 2))) -NoNewline -ForegroundColor $config.Colors.Primary
Write-Host "┐" -ForegroundColor $config.Colors.Primary
# Empty line
Write-Host " │" -NoNewline -ForegroundColor $config.Colors.Primary
Write-Host (-join (" " * $contentWidth)) -NoNewline
Write-Host "│" -ForegroundColor $config.Colors.Primary
# Content
foreach ($line in $Content) {
Write-Host " │ " -NoNewline -ForegroundColor $config.Colors.Primary
Write-Host $line -NoNewline
# Calculate padding to right border
$padding = $contentWidth - $line.Length - 1
if ($padding -gt 0) {
Write-Host (-join (" " * $padding)) -NoNewline
}
Write-Host "│" -ForegroundColor $config.Colors.Primary
}
# Empty line
Write-Host " │" -NoNewline -ForegroundColor $config.Colors.Primary
Write-Host (-join (" " * $contentWidth)) -NoNewline
Write-Host "│" -ForegroundColor $config.Colors.Primary
# Bottom border
Write-Host " └" -NoNewline -ForegroundColor $config.Colors.Primary
Write-Host (-join ("─" * $width)) -NoNewline -ForegroundColor $config.Colors.Primary
Write-Host "┘" -ForegroundColor $config.Colors.Primary
}
# Function to display the script menu with keyboard navigation
function Show-Menu {
param([array]$Scripts)
$selectedIndex = 0
$scrollOffset = 0
$maxVisibleItems = [Math]::Min($Scripts.Count, $Host.UI.RawUI.WindowSize.Height - 15)
$cursorSupported = Set-CursorVisible $false
try {
while ($true) {
Clear-Host
# Header
$headerTitle = $config.Title
Write-Host (Get-CenteredText -Text $headerTitle) -ForegroundColor $config.Colors.Header
Write-Host (Get-CenteredText -Text (New-HorizontalLine -Char '═' -Length $headerTitle.Length)) -ForegroundColor $config.Colors.Header
Write-Host ""
# Menu box
$helpText = "Use ↑↓ or j/k to navigate, Enter to select, Esc to exit"
Show-TextBox -Title "Available Scripts" -Content @($helpText, "")
# Calculate visible range
if ($selectedIndex - $scrollOffset -ge $maxVisibleItems) {
$scrollOffset = $selectedIndex - $maxVisibleItems + 1
}
elseif ($selectedIndex -lt $scrollOffset) {
$scrollOffset = $selectedIndex
}
$endIndex = [Math]::Min($scrollOffset + $maxVisibleItems - 1, $Scripts.Count - 1)
# Show scroll indicators
if ($scrollOffset -gt 0) {
Write-Host " ⟰ More scripts above" -ForegroundColor $config.Colors.Secondary
}
# Display visible scripts
for ($i = $scrollOffset; $i -le $endIndex; $i++) {
$scriptName = $Scripts[$i].name
# Use if-else instead of ternary operator for PowerShell 5.1 compatibility
if ($i -eq $selectedIndex) {
$scriptSymbol = $config.Symbols.Selected
$scriptColor = $config.Colors.Highlight
} else {
$scriptSymbol = $config.Symbols.Bullet
$scriptColor = $config.Colors.Text
}
Write-Host " " -NoNewline
Write-Host $scriptSymbol -NoNewline -ForegroundColor $scriptColor
Write-Host " " -NoNewline
Write-Host "$scriptName" -ForegroundColor $scriptColor
}
# Show scroll indicators
if ($endIndex -lt $Scripts.Count - 1) {
Write-Host " ⟱ More scripts below" -ForegroundColor $config.Colors.Secondary
}
Write-Host ""
Show-Notification -Message "Script $($selectedIndex + 1) of $($Scripts.Count)" -Color $config.Colors.Secondary -Symbol $config.Symbols.Info
# Get key press
$key = $Host.UI.RawUI.ReadKey("NoEcho,IncludeKeyDown")
# Process key press
switch ($key.VirtualKeyCode) {
38 { if ($selectedIndex -gt 0) { $selectedIndex-- } } # Up Arrow
40 { if ($selectedIndex -lt $Scripts.Count - 1) { $selectedIndex++ } } # Down Arrow
13 { return $selectedIndex } # Enter Key
27 { return -1 } # Escape Key - Return -1 instead of exit
75 { if ($selectedIndex -gt 0) { $selectedIndex-- } } # K key (vim-style)
74 { if ($selectedIndex -lt $Scripts.Count - 1) { $selectedIndex++ } } # J key (vim-style)
36 { $selectedIndex = 0 } # Home key
35 { $selectedIndex = $Scripts.Count - 1 } # End key
33 { $selectedIndex = [Math]::Max(0, $selectedIndex - $maxVisibleItems) } # Page Up
34 { $selectedIndex = [Math]::Min($Scripts.Count - 1, $selectedIndex + $maxVisibleItems) } # Page Down
}
}
}
finally {
if ($cursorSupported) {
Set-CursorVisible $true
}
}
}
# Function to execute a script
function Invoke-SelectedScript {
param (
[object]$Script
)
if ($null -eq $Script) {
return
}
Clear-Host
Write-Host (Get-CenteredText -Text "Executing Script") -ForegroundColor $config.Colors.Header
Write-Host (Get-CenteredText -Text (New-HorizontalLine -Char '═' -Length 16)) -ForegroundColor $config.Colors.Header
Write-Host ""
Show-TextBox -Title $Script.name -Content @("Downloading and preparing to execute...")
try {
$scriptContent = Show-LoadingAnimation -LoadingText "Downloading script" -ScriptBlock {
try {
Invoke-RestMethod -Uri $using:Script.download_url -ErrorAction Stop
}
catch {
return @{ Error = $_ }
}
}
# Check if we got an error object back
if ($scriptContent -is [hashtable] -and $scriptContent.ContainsKey('Error')) {
throw $scriptContent.Error
}
Write-Host ""
Show-Notification -Message "Script downloaded successfully!" -Color $config.Colors.Highlight -Symbol $config.Symbols.Success
Write-Host ""
# Create a temporary script file
$tempFile = [System.IO.Path]::GetTempFileName() + ".ps1"
$scriptContent | Out-File -FilePath $tempFile -Encoding UTF8
# Execute it in a new scope
Show-Notification -Message "Executing script..." -Color $config.Colors.Warning
Write-Host ""
Write-Host (New-HorizontalLine) -ForegroundColor $config.Colors.Secondary
Write-Host ""
& $tempFile
Write-Host ""
Write-Host (New-HorizontalLine) -ForegroundColor $config.Colors.Secondary
Write-Host ""
Show-Notification -Message "Script execution completed!" -Color $config.Colors.Highlight -Symbol $config.Symbols.Success
# Clean up
Remove-Item -Path $tempFile -Force -ErrorAction SilentlyContinue
}
catch {
Write-Host ""
Show-Notification -Message "Error: $_" -Color $config.Colors.Error -Symbol $config.Symbols.Error
}
Write-Host ""
Show-Notification -Message "Press any key to return to the menu..." -Color $config.Colors.Warning
$null = $Host.UI.RawUI.ReadKey('NoEcho,IncludeKeyDown')
}
# Handles exiting the application
function Exit-Application {
Clear-Host
Write-Host (Get-CenteredText -Text "Thank you for using PowerShell Script Manager") -ForegroundColor $config.Colors.Header
Write-Host ""
exit
}
# Main application loop
function Start-Application {
while ($true) {
# Clear screen and set cursor position
Clear-Host
# Fetch scripts
$scripts = Get-Scripts
if (-not $scripts) {
# Check if user wants to exit
Clear-Host
Show-Notification -Message "No scripts available. Press any key to retry or Esc to exit..." -Color $config.Colors.Warning
$key = $Host.UI.RawUI.ReadKey('NoEcho,IncludeKeyDown')
if ($key.VirtualKeyCode -eq 27) {
Exit-Application
}
continue
}
# Show menu and get user selection
$selectedIndex = Show-Menu -Scripts $scripts
# Check if user pressed Esc to exit
if ($selectedIndex -eq -1) {
Exit-Application
}
$selectedScript = $scripts[$selectedIndex]
# Execute the selected script
Invoke-SelectedScript -Script $selectedScript
}
}
# Start the application
try {
Start-Application
}
catch {
Clear-Host
Write-Host "An unexpected error occurred:" -ForegroundColor $config.Colors.Error
Write-Host $_.Exception.Message -ForegroundColor $config.Colors.Error
Write-Host ""
Write-Host "Press any key to exit..." -ForegroundColor $config.Colors.Warning
$null = $Host.UI.RawUI.ReadKey('NoEcho,IncludeKeyDown')
}