Guest User

Untitled

a guest
May 26th, 2018
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.70 KB | None | 0 0
  1. public class Console2
  2. {
  3. public static void Write(string str, ConsoleColor color)
  4. {
  5. Console.ForegroundColor = color;
  6. Console.Write(str);
  7. Console.ResetColor();
  8. }
  9.  
  10. public static void WriteLine(string str, ConsoleColor color)
  11. {
  12. Console2.Write(str, color);
  13. Console.WriteLine();
  14. }
  15.  
  16. enum LexerState {
  17. BEFORE,
  18. ACCENTED,
  19. AFTER,
  20. FIRST_OPENING_BRACE,
  21. SECOND_OPENING_BRACE,
  22. FIRST_CLOSING_BRACE,
  23. SECOND_CLOSING_BRACE
  24. }
  25.  
  26. // Parset een Mustache-achtige-template-string (met max. 1 parameter) en print deze op de console
  27. // met de parameter in de accentedColor.
  28. public static void Write(string str, ConsoleColor basicColor, ConsoleColor accentedColor)
  29. {
  30. string before = "";
  31. string accented = "";
  32. string after = "";
  33.  
  34. LexerState ps = LexerState.BEFORE;
  35.  
  36. foreach (char c in str)
  37. {
  38. switch(ps) {
  39. case LexerState.BEFORE:
  40. if (c != '{')
  41. {
  42. before += c;
  43. }
  44. else
  45. {
  46. ps = LexerState.FIRST_OPENING_BRACE;
  47. }
  48. break;
  49. case LexerState.FIRST_OPENING_BRACE:
  50. if (c == '{')
  51. {
  52. ps = LexerState.SECOND_OPENING_BRACE;
  53. }
  54. else
  55. {
  56. ps = LexerState.BEFORE;
  57. before += '{';
  58. before += c;
  59. }
  60. break;
  61. case LexerState.SECOND_OPENING_BRACE:
  62. if (c == '}')
  63. {
  64. ps = LexerState.FIRST_CLOSING_BRACE;
  65. }
  66. else
  67. {
  68. ps = LexerState.ACCENTED;
  69. accented += c;
  70. }
  71. break;
  72. case LexerState.ACCENTED:
  73. if (c == '}')
  74. {
  75. ps = LexerState.FIRST_CLOSING_BRACE;
  76. }
  77. else
  78. {
  79. accented += c;
  80. }
  81. break;
  82. case LexerState.FIRST_CLOSING_BRACE:
  83. if (c == '}')
  84. {
  85. ps = LexerState.SECOND_CLOSING_BRACE;
  86. }
  87. else
  88. {
  89. accented += '}';
  90. accented += c;
  91. }
  92. break;
  93. case LexerState.SECOND_CLOSING_BRACE:
  94. ps = LexerState.AFTER;
  95. after += c;
  96. break;
  97. case LexerState.AFTER:
  98. after += c;
  99. break;
  100. }
  101. }
  102.  
  103. Console2.Write(before, basicColor);
  104. Console2.Write(accented, accentedColor);
  105. Console2.WriteLine(after, basicColor);
  106. }
  107.  
  108. public static void WriteLine(string str, ConsoleColor basicColor, ConsoleColor accentedColor)
  109. {
  110. Console2.Write(str, basicColor, accentedColor);
  111. }
  112.  
  113. }
Add Comment
Please, Sign In to add comment