-
Notifications
You must be signed in to change notification settings - Fork 0
/
wsl-selector.ps1
109 lines (94 loc) · 3.63 KB
/
wsl-selector.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
# Function to draw the menu
function Draw-Menu {
param (
[int]$SelectedIndex
)
$host.UI.RawUI.CursorPosition = @{X=0; Y=0}
$width = $host.UI.RawUI.WindowSize.Width
$height = $host.UI.RawUI.WindowSize.Height
# Clear the screen
Clear-Host
# Cool ASCII art title
$title = @"
_,--._.-,
/\_r-,\_ )
.-.) _;='_/ (.;
\ \' \/S )
L.'-. _.'|-'
<_`-'\'_.'/
`'-._( \
___ \\, ___
\ .'-. \\ .-'_. /
'._' '.\\/.-'_.'
'--``\('--'
\\
`\\,
\|
"@
# Calculate vertical positioning
$options = @("Perso", "Work", "Host")
$titleLines = ($title -split "`n").Count
$totalContentHeight = $titleLines + 3 + $options.Count * 2 # Title + spacing + options
$topPadding = [math]::Max(0, ($height - $totalContentHeight) / 2)
# Draw top padding
Write-Host ("`n" * $topPadding)
# Draw title
$titleLines = $title -split "`n"
foreach ($line in $titleLines) {
$linePadding = " " * (($width - $line.TrimStart().Length) / 2)
Write-Host "$linePadding$($line.TrimStart())" -ForegroundColor Cyan
}
Write-Host "`n"
# Draw options
$maxLength = ($options | Measure-Object -Property Length -Maximum).Maximum
for ($i = 0; $i -lt $options.Count; $i++) {
$option = $options[$i]
$paddingLength = [math]::Floor(($width - $maxLength - 4) / 2) # -4 to account for "> " and " "
$leftPadding = " " * $paddingLength
$rightPadding = " " * ($width - $paddingLength - $maxLength - 4)
if ($i -eq $SelectedIndex) {
Write-Host $leftPadding -NoNewline
Write-Host ">" -NoNewline -ForegroundColor Cyan
Write-Host " $option".PadRight($maxLength + 1) -NoNewline -ForegroundColor Cyan
Write-Host $rightPadding
} else {
Write-Host "$leftPadding $($option.PadRight($maxLength))$rightPadding" -ForegroundColor DarkGray
}
Write-Host ""
}
# Draw instructions at the bottom
$instructions = "Use arrow keys or hjkl to navigate, Enter to select, q to quit"
$instructionsPadding = " " * (($width - $instructions.Length) / 2)
$bottomPadding = $height - $host.UI.RawUI.CursorPosition.Y - 2
if ($bottomPadding -gt 0) {
Write-Host ("`n" * $bottomPadding)
}
Write-Host "$instructionsPadding$instructions" -ForegroundColor DarkGray
}
# Main script
$SelectedIndex = 0
$running = $true
$options = @("Perso", "Work", "Latent.host")
while ($running) {
Draw-Menu -SelectedIndex $SelectedIndex
$key = $host.UI.RawUI.ReadKey("NoEcho,IncludeKeyDown")
switch ($key.VirtualKeyCode) {
38 { $SelectedIndex = [Math]::Max(0, $SelectedIndex - 1) } # Up arrow
40 { $SelectedIndex = [Math]::Min($options.Count - 1, $SelectedIndex + 1) } # Down arrow
72 { $SelectedIndex = [Math]::Max(0, $SelectedIndex - 1) } # h
74 { $SelectedIndex = [Math]::Min($options.Count - 1, $SelectedIndex + 1) } # j
75 { $SelectedIndex = [Math]::Max(0, $SelectedIndex - 1) } # k
76 { $SelectedIndex = [Math]::Min($options.Count - 1, $SelectedIndex + 1) } # l
81 { $running = $false } # q to quit
13 { # Enter
switch ($SelectedIndex) {
0 { wsl -d Ubuntu -- bash -c "cd ~ && clear && bash" }
1 { wsl -d Ubuntu-24.04 -- bash -c "cd ~ && clear && bash" }
2 { wsl -d Ubuntu -- bash -c "cd ~ && clear && bash ./.config/scripts/ssh-latent-host.sh" }
}
$running = $false
}
}
}
# Clear the console after selection or quitting
Clear-Host