Skip to content

Instantly share code, notes, and snippets.

@he3als
Created May 13, 2023 23:19
Show Gist options
  • Save he3als/b7916404fee86f8d6672e1b872f71210 to your computer and use it in GitHub Desktop.
Save he3als/b7916404fee86f8d6672e1b872f71210 to your computer and use it in GitHub Desktop.
Simple page switching script for PowerShell
<#
The MIT License (MIT)
Copyright (c) 2023 he3als
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
#>
$pages = @(
@{
Title = "Page 1"
Commands = {
Write-Host "page 1 command"
}
},
@{
Title = "Page 2"
Commands = {
Write-Host "page 2 command"
}
},
@{
Title = "Page 3"
Commands = {
Write-Host "page 3 command"
}
}
)
$currentPageIndex = 0
function Wait-Key {
[console]::CursorVisible = $false
$pageInput = $Host.UI.RawUI.ReadKey('NoEcho,IncludeKeyDown')
switch ($pageInput.VirtualKeyCode) {
# Next
78 {
$currentPageIndex = ($currentPageIndex + 1) % $pages.Count
}
# Back
66 {
$currentPageIndex = ($currentPageIndex - 1) % $pages.Count
if ($currentPageIndex -lt 0) {
$currentPageIndex += $pages.Count
}
}
# Enter
13 {
Show-Page
}
default {
# Do nothing
Wait-Key
}
}
Show-Page
}
function Show-Page {
Clear-Host
$currentPage = $pages[$currentPageIndex]
& $currentPage.Commands
# Write-Host "`nCurrent Page: $($currentPage.Title)" -ForegroundColor Yellow
Write-Host "`n--------------------- Page $($currentPageIndex + 1) ---------------------" -ForegroundColor Yellow
Write-Host "(n) Next Page - (b) Previous Page - (enter) Reload"
Wait-Key
}
Show-Page
@he3als
Copy link
Author

he3als commented May 13, 2023

powershell_ClXeIPDv2M.mp4

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment