Console Write Utility


/ Published in: C#
Save to your folder(s)

Helps write data to the console screen.


Copy this code and paste it in your HTML
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Globalization;
  4. using System.Linq;
  5. using System.Text;
  6.  
  7. namespace CodeGenerator.Utilities
  8. {
  9. public class Write
  10. {
  11. public const ConsoleColor Statement = ConsoleColor.White;
  12. public const ConsoleColor Note = ConsoleColor.Gray;
  13. public const ConsoleColor Title = ConsoleColor.Yellow;
  14. public const ConsoleColor Question = ConsoleColor.White;
  15. public const ConsoleColor Answer = ConsoleColor.Green;
  16. public const ConsoleColor Error = ConsoleColor.Red;
  17. public const ConsoleColor Background = ConsoleColor.Black;
  18.  
  19. public static void Line(string text, ConsoleColor forecolor = Statement, ConsoleColor backcolor = Background)
  20. {
  21. SetColors(forecolor, backcolor);
  22. Console.WriteLine(text);
  23. Console.ResetColor();
  24. }
  25.  
  26. public static string Input(string text, ConsoleColor questionForecolor = Question, ConsoleColor questionBackcolor = Background, ConsoleColor inputForecolor = Answer, ConsoleColor inputBackcolor = Background)
  27. {
  28. SetColors(questionForecolor,questionBackcolor);
  29. Console.Write(text + " ");
  30. SetColors(inputForecolor, inputBackcolor);
  31. var answer = Console.ReadLine();
  32. Console.ResetColor();
  33. return answer;
  34. }
  35.  
  36. public static bool BoolInput(string text, ConsoleColor questionForecolor = Question, ConsoleColor questionBackcolor = Background, ConsoleColor inputForecolor = Answer, ConsoleColor inputBackcolor = Background)
  37. {
  38. SetColors(questionForecolor,questionBackcolor);
  39. Console.Write(text + " ");
  40. SetColors(Note);
  41. Console.Write("(y/n) ");
  42. SetColors(inputForecolor, inputBackcolor);
  43. var answer = Console.ReadLine() ?? "N";
  44. Console.ResetColor();
  45.  
  46. return answer.ToLower() == "y";
  47. }
  48.  
  49. public static void Value(string key, string value, string seperator = " - ", ConsoleColor keyForecolor = Statement, ConsoleColor keyBackcolor = Background, ConsoleColor valueForecolor = Answer, ConsoleColor valueBackcolor = Background)
  50. {
  51. SetColors(keyForecolor, keyBackcolor);
  52. Console.Write(key);
  53. Console.Write(seperator);
  54. Line(value, valueForecolor,valueBackcolor);
  55. }
  56.  
  57. public static string Options(string[] options, string question = null)
  58. {
  59. for (var i = 0; i < options.Count(); i++)
  60. {
  61. Value(" " + (i+1).ToString(CultureInfo.InvariantCulture), options[i],". ");
  62. }
  63. Blank();
  64. var answer = Input( question != null ? question + "[1-" + options.Count() + "]" : "Please choose an option:[1-" + options.Count() + "]");
  65. return answer;
  66. }
  67.  
  68. public static void Blank()
  69. {
  70. Console.WriteLine();
  71. }
  72.  
  73. public static void SetColors(ConsoleColor foreground = Statement, ConsoleColor background = Background)
  74. {
  75. Console.ForegroundColor = foreground;
  76. Console.BackgroundColor = background;
  77. }
  78. }
  79.  
  80. }

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.