|
# Create Menu for Powershell |
|
> Shows strings as a table to be selectable by navigating with arrow keys, in Powershell |
|
|
|
`Create-Menu()` function meta, as powershell comment: |
|
``` |
|
Function Create-Menu() { |
|
<# |
|
.INPUTS |
|
array of strings |
|
|
|
.PARAMETER MenuOptions <String[]> |
|
Takes an array with selections (must be more then one) |
|
.PARAMETER Title <Null|ScriptBlock|String> |
|
Takes a string or a scriptblock, use $global:varname to link to Title, Footer or CallbackSelection (available vars: $Selection, $SelectionValue, $MenuOptions, $MenuOptionsInput, $global:*) |
|
.PARAMETER Selected <Null|Integer> |
|
Initial string to select |
|
.PARAMETER Footer <Null|ScriptBlock|String> |
|
Takes a string or a scriptblock (available vars: $Selection, $SelectionValue, $MenuOptions, $MenuOptionsInput, $global:*) |
|
.PARAMETER CallbackSelection <Null|ScriptBlock> |
|
If you want to trigger something on selection or a key, or change the $MenuOptions/$Selection, return $True to exit and return $False to exit and do -CleanHost (available vars: $Selection, $SelectionValue, $MenuOptions, $MenuOptionsInput, $KeyInput, $global:*) (default: Null) |
|
.PARAMETER Columns <"Auto"|Integer> |
|
Define how many columns should be shown (default: "Auto") |
|
.PARAMETER MaximumColumnWidth <"Auto"|Integer> |
|
The maximum amount of chars in a cell should be displayed, if to large, '...' will be appended |
|
(default: "Auto" if Columns is a number, results in "20" if Columns is "Auto") |
|
.PARAMETER ShowCurrentSelection <Boolean> |
|
Shows the current selection text in full length in the console title (default: $True) |
|
.PARAMETER PassThrou |
|
Without, will output the index of the selection, otherwise the selected string (default: $False) |
|
.PARAMETER ReturnObject |
|
Returns Selection index, SelectionValue string, MenuOptions string[] of maybe modified items, MenuOptionsInput string[] of input strings, Items object of {"Name" maybe modified,"Index","Input" originial string} -- has a higher priority then PassThrou |
|
.PARAMETER BackgroundColor <Cyan|ConsoleColor> |
|
Color for the selection (default: Cyan) |
|
.PARAMETER ForegroundColor <Black|ConsoleColor> |
|
Color for the selection (default: Black) |
|
.PARAMETER ForegroundColorTitle <Cyan|ConsoleColor> |
|
Color for the title (default: Cyan) |
|
.PARAMETER ForegroundColorFooter <Black|ConsoleColor> |
|
Color for the footer (default: Black) |
|
|
|
.PARAMETER CleanHost <Boolean> |
|
Will clear the menu after selecting from the terminal (default: False) |
|
|
|
.OUTPUTS |
|
Default is the index of the selected string, using -PassThrou it will be the selected string |
|
#> |
|
|
|
# for the implentation, see the module source code |
|
} |
|
|
|
``` |
|
|
|
## Usage |
|
|
|
To use it in your own scripts, just load it as module, to make the function available |
|
|
|
```ps1 |
|
New-Module -Name "Create Menu" -ScriptBlock ([Scriptblock]::Create((New-Object System.Net.WebClient).DownloadString("https://gist.githubusercontent.com/BananaAcid/b8efca90cc6ca873fa22a7f9b98d918a/raw/Create-Menu.ps1"))) | Out-Null |
|
|
|
$check = Create-Menu no,yes "Want it?" 1 |
|
|
|
echo ("you " + ($check ? "do" : "do not") + " want it") |
|
``` |
|
|
|
## Examples |
|
show all files as a selection and return its index upon selecting |
|
```ps1 |
|
import-module ./create-menu.ps1 |
|
ls | Create-Menu |
|
``` |
|
|
|
show all files as a selection and return its index upon selecting. Loading from remote location. |
|
```ps1 |
|
New-Module -Name "Create Menu" -ScriptBlock ([Scriptblock]::Create((New-Object System.Net.WebClient).DownloadString("https://gist.githubusercontent.com/BananaAcid/b8efca90cc6ca873fa22a7f9b98d918a/raw/Create-Menu.ps1"))) | Out-Null |
|
ls | Create-Menu |
|
``` |
|
|
|
Shortest version. "no" first, becuase index 0 is equal to false. the "1" selects index 1 initially |
|
```ps1 |
|
$check = Create-Menu no,yes "Want it?" 1 |
|
``` |
|
|
|
Longer version |
|
```ps1 |
|
$check = Create-Menu @("no","yes") -Title "Want it?" -Selected 1 |
|
``` |
|
|
|
Outputs a filename's contents after selecting |
|
```ps1 |
|
ls | create-menu -passthrou -T "Show Content:" |% {cat $_} |
|
``` |
|
|
|
Usage for -MenuOptions by piping in |
|
```ps1 |
|
echo "Select a letter"; $sel = @("a","b","c") | Create-Menu -PassThrou; echo "Selected: $sel" |
|
``` |
|
|
|
A simple string as title |
|
```ps1 |
|
ls ../ | Create-Menu -Title "abc`n-----" |
|
``` |
|
|
|
A scriptblock with an internal variable |
|
```ps1 |
|
ls ../ | Create-Menu -Title {"SEL: $SelectionValue`n-----"} |
|
``` |
|
|
|
A scriptblock with an internal variable, returning the selection opens |
|
```ps1 |
|
ls ../ | Create-Menu -Title {"SEL: $SelectionValue`n-----"} -ReturnObject |
|
``` |
|
|
|
A scriptblock with colored title |
|
```ps1 |
|
ls ../ | Create-Menu -Title {Write-Host Green "SEL: $SelectionValue`n-----"} |
|
``` |
|
|
|
ESC to cancel input |
|
```ps1 |
|
Create-Menu a,b -CallbackSelection {if ($KeyInput -eq 27) { return $False } } |
|
``` |
|
|
|
Show code of pressed key |
|
```ps1 |
|
Create-Menu 0,1,2,3 -t {"SEL: $global:ki`n-----"} -CallbackSelection { $global:ki = $KeyInput } |
|
``` |
|
|
|
Show "Y" and "n", pressing y (89) or n (78) will select the specific index, exit and show the selected |
|
```ps1 |
|
$YesNoCB = { If ($KeyInput -eq 89) { $Selection = 0 ; Return $True } If ($KeyInput -eq 78) { $Selection = 1 ; Return $True } } |
|
Create-Menu Y,n -CallbackSelection $YesNoCb |
|
``` |
|
|
|
Allows to select multiple files with the space key (Keycode 32), then gets their values |
|
```ps1 |
|
$SpacePressed = { If ($KeyInput -eq 32) { if ($SelectionValue -like '`* *') { $MenuOptions[$Selection] = $MenuOptionsInput[$Selection] } Else {$MenuOptions[$Selection] = '* ' + $SelectionValue } } } |
|
$ret = ls ~/ | Create-Menu -t {"Full Name: $SelectionValue`n-----"} -CallbackSelection $SpacePressed -ReturnObject |
|
$selected = $ret.Items |? { $_.Name -like '`* *' } |% Input |
|
``` |
|
|
|
## Optional |
|
- [Create-Menu() module](https://gist.githubusercontent.com/BananaAcid/b8efca90cc6ca873fa22a7f9b98d918a/raw/Create-Menu.ps1): Documentation of function params and usage details |
|
- [Readme.md](https://gist.githubusercontent.com/BananaAcid/b8efca90cc6ca873fa22a7f9b98d918a/raw/_readme.md): The official readme to the project |