Advertisement
Guest User

Untitled

a guest
Dec 23rd, 2016
150
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.06 KB | None | 0 0
  1. using System;
  2. using System.Linq;
  3. using System.Text;
  4. using System.Security;
  5.  
  6. namespace CG.ConsoleTest
  7. {
  8.     public static class ConsolePlus
  9.     {
  10.         static ConsolePlus()
  11.         {
  12.             Console.CancelKeyPress += Console_CancelKeyPress;
  13.         }
  14.  
  15.         public static String Input(String prompt)
  16.         {
  17.             Console.ForegroundColor = ConsoleColor.White;
  18.             Console.Write(prompt);
  19.             Console.ForegroundColor = ConsoleColor.Gray;
  20.  
  21.             return Console.ReadLine();
  22.         }
  23.  
  24.         public static String Prompt(String prompt, params string[] options)
  25.         {
  26.             bool optionMatched = false;
  27.             string response;
  28.             var allOptions = options.SelectMany(option => option.Split("/".ToCharArray()).Select(suboption => suboption.Trim()));
  29.  
  30.             do
  31.             {
  32.                 Console.ForegroundColor = ConsoleColor.White;
  33.                 Console.Write(prompt);
  34.                 Console.ForegroundColor = ConsoleColor.Gray;
  35.  
  36.                 response = Console.ReadLine();
  37.  
  38.                 if (response == null)
  39.                 {
  40.                     break;
  41.                 }
  42.                 else
  43.                 {
  44.                     response = response.Trim().ToLower();
  45.  
  46.                     if (allOptions.Any(opt => opt.ToLower().Equals(response)))
  47.                     {
  48.                         optionMatched = true;
  49.                     }
  50.                     else
  51.                     {
  52.                         Console.WriteLine();
  53.                         Console.WriteLine("Valid options are:");
  54.                         foreach (var option in options)
  55.                         {
  56.                             Console.WriteLine(" - {0}", option);
  57.                         }
  58.                         Console.WriteLine();
  59.                     }
  60.                 }
  61.             }
  62.             while (optionMatched == false);
  63.  
  64.             return response;
  65.         }
  66.  
  67.         public static SecureString GetPassword(String prompt)
  68.         {
  69.             Console.ForegroundColor = ConsoleColor.White;
  70.             Console.Write(prompt);
  71.             Console.ForegroundColor = ConsoleColor.Gray;
  72.  
  73.             var password = new SecureString();
  74.             int length = 0;
  75.             ConsoleKeyInfo key;
  76.  
  77.             do
  78.             {
  79.                 key = Console.ReadKey(true);
  80.  
  81.                 if (key.Key != ConsoleKey.Backspace && key.Key != ConsoleKey.Enter)
  82.                 {
  83.                     length++;
  84.                     password.AppendChar(key.KeyChar);
  85.                     Console.Write("*");
  86.                 }
  87.                 else if (key.Key == ConsoleKey.Backspace && length > 0)
  88.                 {
  89.                     password.RemoveAt(--length);
  90.                     Console.Write("\b \b");
  91.                 }
  92.             }
  93.             while (key.Key != ConsoleKey.Enter);
  94.  
  95.             Console.WriteLine();
  96.  
  97.             return password;
  98.         }
  99.  
  100.         private static bool ReadingMultiLine = false;
  101.  
  102.         public static String MultiLineInput(String prompt)
  103.         {
  104.             var output = new StringBuilder();
  105.  
  106.             Console.ForegroundColor = ConsoleColor.White;
  107.             Console.WriteLine(prompt);
  108.             Console.ForegroundColor = ConsoleColor.Gray;
  109.  
  110.             var line = "";
  111.  
  112.             ReadingMultiLine = true;
  113.  
  114.             do
  115.             {
  116.                 Console.Write("> ");
  117.                 line = Console.ReadLine();
  118.  
  119.                 if (line == null)
  120.                 {
  121.                     break;
  122.                 }
  123.                 else
  124.                 {
  125.                     output.AppendLine(line);
  126.                 }
  127.             }
  128.             while (ReadingMultiLine);
  129.  
  130.             Console.WriteLine();
  131.  
  132.             return output.ToString();
  133.         }
  134.  
  135.         private static void Console_CancelKeyPress(object sender, ConsoleCancelEventArgs e)
  136.         {
  137.             if (ReadingMultiLine)
  138.             {
  139.                 ReadingMultiLine = false;
  140.                 e.Cancel = true;
  141.             }
  142.         }
  143.     }
  144. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement