Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Linq;
- using System.Text;
- using System.Security;
- namespace CG.ConsoleTest
- {
- public static class ConsolePlus
- {
- static ConsolePlus()
- {
- Console.CancelKeyPress += Console_CancelKeyPress;
- }
- public static String Input(String prompt)
- {
- Console.ForegroundColor = ConsoleColor.White;
- Console.Write(prompt);
- Console.ForegroundColor = ConsoleColor.Gray;
- return Console.ReadLine();
- }
- public static String Prompt(String prompt, params string[] options)
- {
- bool optionMatched = false;
- string response;
- var allOptions = options.SelectMany(option => option.Split("/".ToCharArray()).Select(suboption => suboption.Trim()));
- do
- {
- Console.ForegroundColor = ConsoleColor.White;
- Console.Write(prompt);
- Console.ForegroundColor = ConsoleColor.Gray;
- response = Console.ReadLine();
- if (response == null)
- {
- break;
- }
- else
- {
- response = response.Trim().ToLower();
- if (allOptions.Any(opt => opt.ToLower().Equals(response)))
- {
- optionMatched = true;
- }
- else
- {
- Console.WriteLine();
- Console.WriteLine("Valid options are:");
- foreach (var option in options)
- {
- Console.WriteLine(" - {0}", option);
- }
- Console.WriteLine();
- }
- }
- }
- while (optionMatched == false);
- return response;
- }
- public static SecureString GetPassword(String prompt)
- {
- Console.ForegroundColor = ConsoleColor.White;
- Console.Write(prompt);
- Console.ForegroundColor = ConsoleColor.Gray;
- var password = new SecureString();
- int length = 0;
- ConsoleKeyInfo key;
- do
- {
- key = Console.ReadKey(true);
- if (key.Key != ConsoleKey.Backspace && key.Key != ConsoleKey.Enter)
- {
- length++;
- password.AppendChar(key.KeyChar);
- Console.Write("*");
- }
- else if (key.Key == ConsoleKey.Backspace && length > 0)
- {
- password.RemoveAt(--length);
- Console.Write("\b \b");
- }
- }
- while (key.Key != ConsoleKey.Enter);
- Console.WriteLine();
- return password;
- }
- private static bool ReadingMultiLine = false;
- public static String MultiLineInput(String prompt)
- {
- var output = new StringBuilder();
- Console.ForegroundColor = ConsoleColor.White;
- Console.WriteLine(prompt);
- Console.ForegroundColor = ConsoleColor.Gray;
- var line = "";
- ReadingMultiLine = true;
- do
- {
- Console.Write("> ");
- line = Console.ReadLine();
- if (line == null)
- {
- break;
- }
- else
- {
- output.AppendLine(line);
- }
- }
- while (ReadingMultiLine);
- Console.WriteLine();
- return output.ToString();
- }
- private static void Console_CancelKeyPress(object sender, ConsoleCancelEventArgs e)
- {
- if (ReadingMultiLine)
- {
- ReadingMultiLine = false;
- e.Cancel = true;
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement