### Example Usage of 'Create Menu' Function in PowerShell Source: https://gist.githubusercontent.com/BananaAcid/b8efca90cc6ca873fa22a7f9b98d918a/raw/_readme This example shows how to load the 'Create Menu' module and then use its `Create-Menu` function. The function presents 'no' and 'yes' options, prompts 'Want it?', and pre-selects the second option (index 1, which is 'yes'). The result, a boolean indicating the user's choice, is then used to print a message. ```PowerShell 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") ``` -------------------------------- ### Loading and Using Create-Menu Function - Powershell Source: https://context7_llms This example demonstrates how to load the `Create-Menu` function into a Powershell session by downloading it from a Gist and then using it to create a simple 'Want it?' menu with 'no' and 'yes' options. It also shows how to capture and interpret the selected value. ```Powershell 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") ``` -------------------------------- ### Piping Options to Create-Menu (PowerShell) Source: https://context7_llms This example demonstrates piping an array of strings (`a`, `b`, `c`) directly to `Create-Menu` as menu options. The `-PassThrou` parameter ensures the selected string value is returned and then displayed. ```PowerShell echo "Select a letter"; $sel = @("a","b","c") | Create-Menu -PassThrou; echo "Selected: $sel" ``` -------------------------------- ### Loading Remote Module and Displaying Files (PowerShell) Source: https://context7_llms This example demonstrates how to load the `Create-Menu` module directly from a remote URL using `New-Module` and `WebClient`. After loading, it pipes the file listing (`ls`) to `Create-Menu` for user selection, returning the selected item's index. ```PowerShell 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 ``` -------------------------------- ### Multi-Selection with Space Key (PowerShell) Source: https://context7_llms This advanced example enables multi-selection using the space key. The `CallbackSelection` script block toggles a '*' prefix on selected items. After selection, it filters and extracts the original input values of the marked items. ```PowerShell $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 ``` -------------------------------- ### Dynamic Title and Returning Selected Object (PowerShell) Source: https://context7_llms Similar to the previous example, this uses a script block for a dynamic title. Additionally, the `-ReturnObject` parameter ensures that the actual selected object (e.g., a FileInfo object) is returned, not just its index or name. ```PowerShell ls ../ | Create-Menu -Title {"SEL: $SelectionValue`n-----"} -ReturnObject ``` -------------------------------- ### Canceling Menu Input with ESC Key (PowerShell) Source: https://context7_llms This example shows how to implement a custom callback for key presses. If the `ESC` key (keycode 27) is pressed, the `CallbackSelection` script block returns `$False`, which cancels the menu selection process. ```PowerShell Create-Menu a,b -CallbackSelection {if ($KeyInput -eq 27) { return $False } } ``` -------------------------------- ### Dynamic Menu Title with Script Block (PowerShell) Source: https://context7_llms This example uses a script block for the menu title, allowing dynamic content. The `$SelectionValue` variable within the script block updates to show the currently highlighted menu item, providing interactive feedback. ```PowerShell ls ../ | Create-Menu -Title {"SEL: $SelectionValue`n-----"} ``` -------------------------------- ### Custom Yes/No Selection with Key Codes (PowerShell) Source: https://context7_llms This example defines a script block `$YesNoCB` to handle custom key-based selection. Pressing 'Y' (keycode 89) selects index 0, and 'n' (keycode 78) selects index 1, immediately exiting the menu and returning the selection. ```PowerShell $YesNoCB = { If ($KeyInput -eq 89) { $Selection = 0 ; Return $True } If ($KeyInput -eq 78) { $Selection = 1 ; Return $True } } Create-Menu Y,n -CallbackSelection $YesNoCB ``` -------------------------------- ### Loading 'Create Menu' Module in PowerShell Source: https://gist.githubusercontent.com/BananaAcid/b8efca90cc6ca873fa22a7f9b98d918a/raw/_readme This snippet demonstrates how to load the 'Create Menu' PowerShell module from a remote Gist URL. It uses `New-Module` to create a module on the fly by downloading the script content via `System.Net.WebClient` and converting it into a script block. The `Out-Null` command suppresses any output from the module loading process. ```PowerShell 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 ``` -------------------------------- ### Displaying Files and Returning Selection Index (PowerShell) Source: https://context7_llms This snippet imports the `Create-Menu` module from a local file and then pipes the output of `ls` (listing files) to `Create-Menu`, allowing the user to select a file and returning its numerical index. ```PowerShell import-module ./create-menu.ps1 ls | Create-Menu ``` -------------------------------- ### Function Definition and Parameters for Create-Menu - Powershell Source: https://context7_llms This snippet provides the detailed documentation for the `Create-Menu` Powershell function, outlining all its parameters, their types, descriptions, and default values. It explains the inputs, outputs, and various customization options for creating interactive console menus. ```Powershell Function Create-Menu() { <# .INPUTS array of strings .PARAMETER MenuOptions Takes an array with selections (must be more then one) .PARAMETER Title 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 Initial string to select .PARAMETER Footer Takes a string or a scriptblock (available vars: $Selection, $SelectionValue, $MenuOptions, $MenuOptionsInput, $global:*) .PARAMETER CallbackSelection 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 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 Color for the selection (default: Cyan) .PARAMETER ForegroundColor Color for the selection (default: Black) .PARAMETER ForegroundColorTitle Color for the title (default: Cyan) .PARAMETER ForegroundColorFooter Color for the footer (default: Black) .PARAMETER CleanHost 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 } ``` -------------------------------- ### Creating a Simple Yes/No Menu (PowerShell) Source: https://context7_llms This snippet shows the shortest way to create a menu with 'no' and 'yes' options. 'no' is at index 0, and 'yes' is at index 1. The menu is titled 'Want it?', and 'yes' (index 1) is pre-selected. ```PowerShell $check = Create-Menu no,yes "Want it?" 1 ``` -------------------------------- ### Displaying File Content After Selection (PowerShell) Source: https://context7_llms This command lists files, presents them in a menu with the title 'Show Content:', and uses `-passthrou` to return the selected file object. The selected file is then piped to `cat` (Get-Content) to display its contents. ```PowerShell ls | create-menu -passthrou -T "Show Content:" |% {cat $_} ``` -------------------------------- ### Creating a Detailed Yes/No Menu (PowerShell) Source: https://context7_llms This is a more explicit version of creating a yes/no menu. It uses an array for menu options and named parameters `-Title` and `-Selected` for clarity, setting 'yes' (index 1) as the initial selection. ```PowerShell $check = Create-Menu @("no","yes") -Title "Want it?" -Selected 1 ``` -------------------------------- ### Colored Dynamic Menu Title (PowerShell) Source: https://context7_llms This snippet demonstrates using `Write-Host` within a script block for the menu title to apply color. It displays 'SEL: [SelectedValue]' in green, providing visual enhancement to the interactive menu. ```PowerShell ls ../ | Create-Menu -Title {Write-Host Green "SEL: $SelectionValue`n-----"} ``` -------------------------------- ### Displaying Pressed Key Code (PowerShell) Source: https://context7_llms This snippet demonstrates capturing and displaying the key code of the last pressed key. The `CallbackSelection` script block updates a global variable `$global:ki` with the `KeyInput` value, which is then shown in the dynamic menu title. ```PowerShell Create-Menu 0,1,2,3 -t {"SEL: $global:ki`n-----"} -CallbackSelection { $global:ki = $KeyInput } ``` -------------------------------- ### Using a Simple String as Menu Title (PowerShell) Source: https://context7_llms This snippet shows how to set a multi-line title for the menu using a simple string with an embedded newline character (`n`). It lists files from the parent directory and applies the specified title. ```PowerShell ls ../ | Create-Menu -Title "abc`n-----" ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.