Skip to content

Files

Latest commit

Jun 22, 2021
f2ad00e · Jun 22, 2021

History

History
129 lines (92 loc) · 3.89 KB

ChooseMenuUsage.md

File metadata and controls

129 lines (92 loc) · 3.89 KB

Interactive Choose Menu

Demo

asciicast

Install

Install-Module -Name InteractiveMenu

Prerequisites

This module only works in Powershell version 5.0 and up.

Usage

Sample code can be found here

Using classes

# Import the module
using module InteractiveMenu

# Define the items for the menu
$answerItems = @(
    [InteractiveMenuChooseMenuItem]::new("I choose good", "good", "Good info"),
    [InteractiveMenuChooseMenuItem]::new("I choose bad", "bad", "Bad info")
);

# Define the question of the menu
$question = "Choos between good and bad"

# Instantiate new menu object
$menu = [InteractiveMenuChooseMenu]::new($question, $answerItems);

# [Optional] You can change the colors and the separator
$options = @{
    MenuInfoColor = [ConsoleColor]::DarkYellow
    QuestionColor = [ConsoleColor]::Magenta
    HelpColor = [ConsoleColor]::Cyan
    ErrorColor = [ConsoleColor]::DarkRed
    HighlightColor = [ConsoleColor]::DarkGreen
    OptionSeparator = "      "
}
$menu.SetOptions($options)

# Trigger the menu and receive the user answer
$answer = $menu.GetAnswer()

Write-Host "The user answered: $answer"

Using functions

# Import the module
Import-Module InteractiveMenu

# Define the items for the menu
$answerItems = @(
    Get-InteractiveChooseMenuOption `
        -Label "I choose good" `
        -Value "good" `
        -Info "Good info"
    Get-InteractiveChooseMenuOption `
        -Label "I choose bad" `
        -Value "bad" `
        -Info "Bad info"
)

# [Optional] You can change the colors and the symbols
$options = @{
    MenuInfoColor = [ConsoleColor]::DarkYellow
    QuestionColor = [ConsoleColor]::Magenta
    HelpColor = [ConsoleColor]::Cyan
    ErrorColor = [ConsoleColor]::DarkRed
    HighlightColor = [ConsoleColor]::DarkGreen
    OptionSeparator = "      "
}

# Define the question of the menu
$question = "Choos between good and bad"

# Trigger the menu and receive the user answer
# Note: the options parameter is optional
$answer = Get-InteractiveMenuChooseUserSelection -Question $question -Answers $answerItems -Options $options

Refferences

Class InteractiveMenuChooseMenuItem

Constructors

  • InteractiveMenuChooseMenuItem([string]$label, [string]$value, [string]$info)

Fields

  • [Mandatory] [string]$Label - The text that you want to show for your option
  • [Mandatory] [string]$Value - The value that you want returned if the user selects it
  • [Mandatory] [string]$Info - The information about the option. This information is displayed under the options

Class InteractiveMenuChooseMenu

Constructors

  • InteractiveMenuChooseMenu([string]$question, [InteractiveMenuChooseMenuItem[]]$options)

Fields

  • [Mandatory] [string]$question - The question that you want to show above the options
  • [Mandatory] [InteractiveMenuChooseMenuItem[]]$options - The list of answers to display in the menu

Methods

  • [string] GetAnswer() - Execute this method to trigger the menu and get the answer that the user selected
  • [void] SetOptions([hashtable]$options) - Execute this method to alter the options of the menu (more details in the Options section)

Options

  • MenuInfoColor - Color of the item information. Default: [ConsoleColor]::DarkYellow
  • QuestionColor - Color of the question text. Default: [ConsoleColor]::Magenta
  • HelpColor - Color of the help items. Default: [ConsoleColor]::Cyan
  • ErrorColor - Color of the errors. Default: [ConsoleColor]::DarkRed
  • HighlightColor - Color of the current selected item. Default: [ConsoleColor]::DarkGreen
  • OptionSeparator - The separator between the answers. Default: "spacespacespacespacespacespace" (6 spaces)