namespace Microsoft.PowerShell.Linux.Host { using System; using System.Collections.Generic; using System.Collections.ObjectModel; using System.Globalization; using System.Management.Automation; using System.Management.Automation.Host; using System.Text; /// /// A sample implementation of the PSHostUserInterface abstract class for /// console applications. Not all members are implemented. Those that are /// not implemented throw a NotImplementedException exception or return /// nothing. Members that are implemented include those that map easily to /// Console APIs and a basic implementation of the prompt API provided. /// internal class MyHostUserInterface : PSHostUserInterface, IHostUISupportsMultipleChoiceSelection { /// /// A reference to the PSRawUserInterface implementation. /// private MyRawUserInterface myRawUi = new MyRawUserInterface(); /// /// Gets an instance of the PSRawUserInterface class for this host /// application. /// public override PSHostRawUserInterface RawUI { get { return this.myRawUi; } } /// /// Prompts the user for input. /// The caption or title of the prompt. /// The text of the prompt. /// A collection of FieldDescription objects /// that describe each field of the prompt. /// A dictionary object that contains the results of the user /// prompts. public override Dictionary Prompt( string caption, string message, Collection descriptions) { this.Write( ConsoleColor.White, ConsoleColor.Black, caption + "\n" + message + " "); Dictionary results = new Dictionary(); foreach (FieldDescription fd in descriptions) { string[] label = GetHotkeyAndLabel(fd.Label); this.WriteLine(label[1]); string userData = Console.ReadLine(); if (userData == null) { return null; } results[fd.Name] = PSObject.AsPSObject(userData); } return results; } /// /// Provides a set of choices that enable the user to choose a /// single option from a set of options. /// /// Text that proceeds (a title) the choices. /// A message that describes the choice. /// A collection of ChoiceDescription objects that /// describ each choice. /// The index of the label in the Choices /// parameter collection. To indicate no default choice, set to -1. /// The index of the Choices parameter collection element that /// corresponds to the option that is selected by the user. public override int PromptForChoice( string caption, string message, Collection choices, int defaultChoice) { // Write the caption and message strings in Blue. this.WriteLine( ConsoleColor.Blue, ConsoleColor.Black, caption + "\n" + message + "\n"); // Convert the choice collection into something that is // easier to work with. See the BuildHotkeysAndPlainLabels // method for details. string[,] promptData = BuildHotkeysAndPlainLabels(choices); // Format the overall choice prompt string to display. StringBuilder sb = new StringBuilder(); for (int element = 0; element < choices.Count; element++) { sb.Append(String.Format( CultureInfo.CurrentCulture, "|{0}> {1} ", promptData[0, element], promptData[1, element])); } sb.Append(String.Format( CultureInfo.CurrentCulture, "[Default is ({0}]", promptData[0, defaultChoice])); // Read prompts until a match is made, the default is // chosen, or the loop is interrupted with ctrl-C. while (true) { this.WriteLine(ConsoleColor.Cyan, ConsoleColor.Black, sb.ToString()); string data = Console.ReadLine().Trim().ToUpper(); // If the choice string was empty, use the default selection. if (data.Length == 0) { return defaultChoice; } // See if the selection matched and return the // corresponding index if it did. for (int i = 0; i < choices.Count; i++) { if (promptData[0, i] == data) { return i; } } this.WriteErrorLine("Invalid choice: " + data); } } #region IHostUISupportsMultipleChoiceSelection Members /// /// Provides a set of choices that enable the user to choose a one or /// more options from a set of options. /// /// Text that proceeds (a title) the choices. /// A message that describes the choice. /// A collection of ChoiceDescription objects that /// describ each choice. /// The index of the label in the Choices /// parameter collection. To indicate no default choice, set to -1. /// The index of the Choices parameter collection element that /// corresponds to the option that is selected by the user. public Collection PromptForChoice( string caption, string message, Collection choices, IEnumerable defaultChoices) { // Write the caption and message strings in Blue. this.WriteLine( ConsoleColor.Blue, ConsoleColor.Black, caption + "\n" + message + "\n"); // Convert the choice collection into something that is // easier to work with. See the BuildHotkeysAndPlainLabels // method for details. string[,] promptData = BuildHotkeysAndPlainLabels(choices); // Format the overall choice prompt string to display. StringBuilder sb = new StringBuilder(); for (int element = 0; element < choices.Count; element++) { sb.Append(String.Format( CultureInfo.CurrentCulture, "|{0}> {1} ", promptData[0, element], promptData[1, element])); } Collection defaultResults = new Collection(); if (defaultChoices != null) { int countDefaults = 0; foreach (int defaultChoice in defaultChoices) { ++countDefaults; defaultResults.Add(defaultChoice); } if (countDefaults != 0) { sb.Append(countDefaults == 1 ? "[Default choice is " : "[Default choices are "); foreach (int defaultChoice in defaultChoices) { sb.AppendFormat( CultureInfo.CurrentCulture, "\"{0}\",", promptData[0, defaultChoice]); } sb.Remove(sb.Length - 1, 1); sb.Append("]"); } } this.WriteLine( ConsoleColor.Cyan, ConsoleColor.Black, sb.ToString()); // Read prompts until a match is made, the default is // chosen, or the loop is interrupted with ctrl-C. Collection results = new Collection(); while (true) { ReadNext: string prompt = string.Format(CultureInfo.CurrentCulture, "Choice[{0}]:", results.Count); this.Write(ConsoleColor.Cyan, ConsoleColor.Black, prompt); string data = Console.ReadLine().Trim().ToUpper(); // If the choice string was empty, no more choices have been made. // If there were no choices made, return the defaults if (data.Length == 0) { return (results.Count == 0) ? defaultResults : results; } // See if the selection matched and return the // corresponding index if it did. for (int i = 0; i < choices.Count; i++) { if (promptData[0, i] == data) { results.Add(i); goto ReadNext; } } this.WriteErrorLine("Invalid choice: " + data); } } #endregion /// /// Prompts the user for credentials with a specified prompt window /// caption, prompt message, user name, and target name. In this /// example this functionality is not needed so the method throws a /// NotImplementException exception. /// /// The caption for the message window. /// The text of the message. /// The user name whose credential is to be /// prompted for. /// The name of the target for which the /// credential is collected. /// Throws a NotImplementedException exception. public override PSCredential PromptForCredential( string caption, string message, string userName, string targetName) { throw new NotImplementedException( "The method or operation is not implemented."); } /// /// Prompts the user for credentials by using a specified prompt window /// caption, prompt message, user name and target name, credential /// types allowed to be returned, and UI behavior options. In this /// example this functionality is not needed so the method throws a /// NotImplementException exception. /// /// The caption for the message window. /// The text of the message. /// The user name whose credential is to be /// prompted for. /// The name of the target for which the /// credential is collected. /// A PSCredentialTypes constant /// that identifies the type of credentials that can be returned. /// A PSCredentialUIOptions constant that /// identifies the UI behavior when it gathers the credentials. /// Throws a NotImplementedException exception. public override PSCredential PromptForCredential( string caption, string message, string userName, string targetName, PSCredentialTypes allowedCredentialTypes, PSCredentialUIOptions options) { throw new NotImplementedException( "The method or operation is not implemented."); } /// /// Reads characters that are entered by the user until a newline /// (carriage return) is encountered. /// /// The characters that are entered by the user. public override string ReadLine() { return Console.ReadLine(); } /// /// Reads characters entered by the user until a newline (carriage return) /// is encountered and returns the characters as a secure string. In this /// example this functionality is not needed so the method throws a /// NotImplementException exception. /// /// Throws a NotImplemented exception. public override System.Security.SecureString ReadLineAsSecureString() { throw new NotImplementedException("The method or operation is not implemented."); } /// /// Writes characters to the output display of the host. /// /// The characters to be written. public override void Write(string value) { Console.Write(value); } /// /// Writes characters to the output display of the host with possible /// foreground and background colors. /// /// The color of the characters. /// The backgound color to use. /// The characters to be written. public override void Write( ConsoleColor foregroundColor, ConsoleColor backgroundColor, string value) { Console.ForegroundColor = foregroundColor; Console.BackgroundColor = backgroundColor; Console.Write(value); //Console.Write(value); Console.ResetColor(); } /// /// Writes a line of characters to the output display of the host /// with foreground and background colors and appends a newline (carriage return). /// /// The forground color of the display. /// The background color of the display. /// The line to be written. public override void WriteLine( ConsoleColor foregroundColor, ConsoleColor backgroundColor, string value) { Console.ForegroundColor = foregroundColor; Console.BackgroundColor = backgroundColor; Console.WriteLine(value); Console.ResetColor(); } /// /// Writes a debug message to the output display of the host. /// /// The debug message that is displayed. public override void WriteDebugLine(string message) { this.WriteLine( ConsoleColor.Yellow, ConsoleColor.Black, String.Format(CultureInfo.CurrentCulture, "DEBUG: {0}", message)); } /// /// Writes an error message to the output display of the host. /// /// The error message that is displayed. public override void WriteErrorLine(string value) { this.WriteLine( ConsoleColor.Red, ConsoleColor.Black, value); } /// /// Writes a newline character (carriage return) /// to the output display of the host. /// public override void WriteLine() { Console.WriteLine(); } /// /// Writes a line of characters to the output display of the host /// and appends a newline character(carriage return). /// /// The line to be written. public override void WriteLine(string value) { Console.WriteLine(value); } /// /// Writes a progress report to the output display of the host. /// /// Unique identifier of the source of the record. /// A ProgressReport object. public override void WriteProgress(long sourceId, ProgressRecord record) { } /// /// Writes a verbose message to the output display of the host. /// /// The verbose message that is displayed. public override void WriteVerboseLine(string message) { this.WriteLine( ConsoleColor.Green, ConsoleColor.Black, String.Format(CultureInfo.CurrentCulture, "VERBOSE: {0}", message)); } /// /// Writes a warning message to the output display of the host. /// /// The warning message that is displayed. public override void WriteWarningLine(string message) { this.WriteLine( ConsoleColor.Yellow, ConsoleColor.Black, String.Format(CultureInfo.CurrentCulture, "WARNING: {0}", message)); } /// /// Parse a string containing a hotkey character. /// Take a string of the form /// Yes to &all /// and returns a two-dimensional array split out as /// "A", "Yes to all". /// /// The string to process /// /// A two dimensional array containing the parsed components. /// private static string[] GetHotkeyAndLabel(string input) { string[] result = new string[] { String.Empty, String.Empty }; string[] fragments = input.Split('&'); if (fragments.Length == 2) { if (fragments[1].Length > 0) { result[0] = fragments[1][0].ToString().ToUpper(); } result[1] = (fragments[0] + fragments[1]).Trim(); } else { result[1] = input; } return result; } /// /// This is a private worker function splits out the /// accelerator keys from the menu and builds a two /// dimentional array with the first access containing the /// accelerator and the second containing the label string /// with the & removed. /// /// The choice collection to process /// /// A two dimensional array containing the accelerator characters /// and the cleaned-up labels private static string[,] BuildHotkeysAndPlainLabels( Collection choices) { // Allocate the result array string[,] hotkeysAndPlainLabels = new string[2, choices.Count]; for (int i = 0; i < choices.Count; ++i) { string[] hotkeyAndLabel = GetHotkeyAndLabel(choices[i].Label); hotkeysAndPlainLabels[0, i] = hotkeyAndLabel[0]; hotkeysAndPlainLabels[1, i] = hotkeyAndLabel[1]; } return hotkeysAndPlainLabels; } } }