Advertisement
Guest User

Untitled

a guest
Dec 23rd, 2016
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 6.06 KB | None | 0 0
  1.         public static int Menu(String prompt, params string[] options)
  2.         {
  3.             if (options == null || options.Length == 0)
  4.                 throw new InvalidOperationException("The options have not been initialized.");
  5.  
  6.             int startLeft = 0;
  7.             int startTop = 0;
  8.             ConsoleColor mainColor = Console.ForegroundColor == ConsoleColor.Gray ? ConsoleColor.Gray : ConsoleColor.White;
  9.  
  10.             var currentIndex = -1;
  11.             bool originalCursorVisibility = Console.CursorVisible;
  12.             var originalForegroundColor = Console.ForegroundColor;
  13.             var originalBackgroundColor = Console.BackgroundColor;
  14.             var result = 0;
  15.             var highlighted = -1;
  16.             var entry = "";
  17.  
  18.             Action<int, string, bool> ShowMenuItem = (index, text, highlight) =>
  19.             {
  20.                 var label = (index + 1).ToString();
  21.  
  22.                 Console.CursorLeft = startLeft;
  23.                 Console.CursorTop = startTop + index;
  24.                 Console.BackgroundColor = ConsoleColor.Black;
  25.                 Console.ForegroundColor = mainColor;
  26.                 Console.Out.Write($"[{label}] ");
  27.                 if (highlight)
  28.                 {
  29.                     Console.BackgroundColor = mainColor;
  30.                     Console.ForegroundColor = ConsoleColor.Black;
  31.                 }
  32.  
  33.                 Console.Out.Write(text);
  34.             };
  35.  
  36.             Action<int> Highlight = i =>
  37.             {
  38.                 if (highlighted != i)
  39.                 {
  40.                     if (highlighted >= 0)
  41.                     {
  42.                         // Unhighlight old
  43.                         ShowMenuItem(highlighted, options[highlighted], false);
  44.                     }
  45.  
  46.                     if (i >= 0 && i < options.Length)
  47.                     {
  48.                         // Highlight new
  49.                         highlighted = i;
  50.                         ShowMenuItem(highlighted, options[highlighted], true);
  51.                     }
  52.                     else
  53.                     {
  54.                         highlighted = -1;
  55.                     }
  56.                 }
  57.             };
  58.  
  59.             if (Console.CursorLeft != 0)
  60.                 Console.Out.WriteLine();
  61.  
  62.             if (!String.IsNullOrEmpty(prompt))
  63.             {
  64.                 Console.WriteLine(prompt);
  65.                 Console.WriteLine();
  66.             }
  67.  
  68.             startLeft = Console.CursorLeft;
  69.             startTop = Console.CursorTop;
  70.  
  71.             Boolean choiceMade = false;
  72.  
  73.             for (int i = 0; i < options.Length; i++)
  74.             {
  75.                 ShowMenuItem(i, options[i], false);
  76.             }
  77.  
  78.             while (!choiceMade)
  79.             {
  80.                 Console.CursorVisible = false;
  81.                 Console.BackgroundColor = originalBackgroundColor;
  82.                 Console.ForegroundColor = originalForegroundColor;
  83.                 Console.CursorLeft = startLeft;
  84.                 Console.CursorTop = startTop + options.Length + 1;
  85.                 Console.Write("> " + entry.PadRight(options.Length));
  86.                 Console.CursorLeft = 2 + entry.Length;
  87.                 Console.CursorTop = startTop + options.Length + 1;
  88.                 Console.CursorVisible = true;
  89.  
  90.                 var input = Console.ReadKey(true);
  91.                 var key = input.Key;
  92.  
  93.                 Console.CursorVisible = false;
  94.  
  95.                 if (key.Equals(ConsoleKey.DownArrow) && currentIndex < options.Length - 1)
  96.                 {
  97.                     currentIndex++;
  98.                     entry = (currentIndex + 1).ToString();
  99.                 }
  100.                 else if (key.Equals(ConsoleKey.UpArrow) && currentIndex > 0)
  101.                 {
  102.                     currentIndex--;
  103.                     entry = (currentIndex + 1).ToString();
  104.                 }
  105.                 else if (key.Equals(ConsoleKey.Home))
  106.                 {
  107.                     currentIndex = 0;
  108.                     entry = (currentIndex + 1).ToString();
  109.                 }
  110.                 else if (key.Equals(ConsoleKey.End))
  111.                 {
  112.                     currentIndex = options.Length - 1;
  113.                     entry = (currentIndex + 1).ToString();
  114.                 }
  115.                 else if (key.Equals(ConsoleKey.PageDown))
  116.                 {
  117.                     currentIndex = Math.Min(currentIndex + 10, options.Length - 1);
  118.                     entry = (currentIndex + 1).ToString();
  119.                 }
  120.                 else if (key.Equals(ConsoleKey.PageUp))
  121.                 {
  122.                     currentIndex = Math.Max(currentIndex - 10, 0);
  123.                     entry = (currentIndex + 1).ToString();
  124.                 }
  125.                 else if (key >= ConsoleKey.D0 && key <= ConsoleKey.D9 && entry.Length < options.Length.ToString().Length)
  126.                 {
  127.                     entry = entry + (key - ConsoleKey.D0).ToString();
  128.                     currentIndex = Int32.Parse(entry) - 1;
  129.                 }
  130.                 else if (key >= ConsoleKey.NumPad0 && key <= ConsoleKey.NumPad9 && entry.Length < options.Length.ToString().Length)
  131.                 {
  132.                     entry = entry + (key - ConsoleKey.NumPad0).ToString();
  133.                     currentIndex = Int32.Parse(entry) - 1;
  134.                 }
  135.                 else if (key.Equals(ConsoleKey.Backspace) && entry.Length > 0)
  136.                 {
  137.                     entry = entry.Substring(0, entry.Length - 1);
  138.                     currentIndex = String.IsNullOrEmpty(entry) ? -1 : Int32.Parse(entry) - 1;
  139.                 }
  140.                 else if (key.Equals(ConsoleKey.Enter))
  141.                 {
  142.                     choiceMade = true;
  143.                     result = currentIndex + 1;
  144.                 }
  145.  
  146.                 Highlight(currentIndex);
  147.             }
  148.  
  149.             Console.CursorVisible = originalCursorVisibility;
  150.             Console.CursorTop = startTop + options.Length + 3;
  151.             Console.CursorLeft = 0;
  152.             Console.BackgroundColor = originalBackgroundColor;
  153.             Console.ForegroundColor = originalForegroundColor;
  154.  
  155.             return result;
  156.         }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement