PageRenderTime 45ms CodeModel.GetById 19ms RepoModel.GetById 1ms app.codeStats 0ms

/IronPython_2_0/Src/Microsoft.Scripting/Hosting/Shell/BasicConsole.cs

#
C# | 182 lines | 129 code | 31 blank | 22 comment | 18 complexity | cacdeaaa462ce73eabc21572f393bfc6 MD5 | raw file
Possible License(s): GPL-2.0, MPL-2.0-no-copyleft-exception, CPL-1.0, CC-BY-SA-3.0, BSD-3-Clause, ISC, AGPL-3.0, LGPL-2.1, Apache-2.0
  1. /* ****************************************************************************
  2. *
  3. * Copyright (c) Microsoft Corporation.
  4. *
  5. * This source code is subject to terms and conditions of the Microsoft Public License. A
  6. * copy of the license can be found in the License.html file at the root of this distribution. If
  7. * you cannot locate the Microsoft Public License, please send an email to
  8. * dlr@microsoft.com. By using this source code in any fashion, you are agreeing to be bound
  9. * by the terms of the Microsoft Public License.
  10. *
  11. * You must not remove this notice, or any other, from this software.
  12. *
  13. *
  14. * ***************************************************************************/
  15. using System; using Microsoft;
  16. using System.IO;
  17. using Microsoft.Scripting;
  18. using Microsoft.Scripting.Utils;
  19. using System.Threading;
  20. namespace Microsoft.Scripting.Hosting.Shell {
  21. public class BasicConsole : IConsole, IDisposable {
  22. private TextWriter _output;
  23. private TextWriter _errorOutput;
  24. private AutoResetEvent _ctrlCEvent;
  25. private Thread _creatingThread;
  26. public TextWriter Output {
  27. get { return _output; }
  28. set {
  29. ContractUtils.RequiresNotNull(value, "value");
  30. _output = value;
  31. }
  32. }
  33. public TextWriter ErrorOutput {
  34. get { return _errorOutput; }
  35. set {
  36. ContractUtils.RequiresNotNull(value, "value");
  37. _errorOutput = value;
  38. }
  39. }
  40. protected AutoResetEvent CtrlCEvent {
  41. get { return _ctrlCEvent; }
  42. set { _ctrlCEvent = value; }
  43. }
  44. protected Thread CreatingThread {
  45. get { return _creatingThread; }
  46. set { _creatingThread = value; }
  47. }
  48. private ConsoleColor _promptColor;
  49. private ConsoleColor _outColor;
  50. private ConsoleColor _errorColor;
  51. private ConsoleColor _warningColor;
  52. public BasicConsole(bool colorful) {
  53. _output = System.Console.Out;
  54. _errorOutput = System.Console.Error;
  55. SetupColors(colorful);
  56. _creatingThread = Thread.CurrentThread;
  57. #if !SILVERLIGHT // ConsoleCancelEventHandler
  58. Console.CancelKeyPress += new ConsoleCancelEventHandler(delegate(object sender, ConsoleCancelEventArgs e) {
  59. if (e.SpecialKey == ConsoleSpecialKey.ControlC) {
  60. e.Cancel = true;
  61. _ctrlCEvent.Set();
  62. _creatingThread.Abort(new KeyboardInterruptException(""));
  63. }
  64. });
  65. #endif
  66. _ctrlCEvent = new AutoResetEvent(false);
  67. }
  68. private void SetupColors(bool colorful) {
  69. if (colorful) {
  70. _promptColor = PickColor(ConsoleColor.Gray, ConsoleColor.White);
  71. _outColor = PickColor(ConsoleColor.Green, ConsoleColor.White);
  72. _errorColor = PickColor(ConsoleColor.Red, ConsoleColor.White);
  73. _warningColor = PickColor(ConsoleColor.Yellow, ConsoleColor.White);
  74. } else {
  75. #if !SILVERLIGHT
  76. _promptColor = _outColor = _errorColor = _warningColor = Console.ForegroundColor;
  77. #endif
  78. }
  79. }
  80. private static ConsoleColor PickColor(ConsoleColor best, ConsoleColor other) {
  81. #if SILVERLIGHT
  82. return best;
  83. #else
  84. if (Console.BackgroundColor != best) {
  85. return best;
  86. }
  87. return other;
  88. #endif
  89. }
  90. protected void WriteColor(TextWriter output, string str, ConsoleColor c) {
  91. #if !SILVERLIGHT // Console.ForegroundColor
  92. ConsoleColor origColor = Console.ForegroundColor;
  93. Console.ForegroundColor = c;
  94. #endif
  95. output.Write(str);
  96. output.Flush();
  97. #if !SILVERLIGHT // Console.ForegroundColor
  98. Console.ForegroundColor = origColor;
  99. #endif
  100. }
  101. #region IConsole Members
  102. public virtual string ReadLine(int autoIndentSize) {
  103. Write("".PadLeft(autoIndentSize), Style.Prompt);
  104. string res = Console.In.ReadLine();
  105. if (res == null) {
  106. // we have a race - the Ctrl-C event is delivered
  107. // after ReadLine returns. We need to wait for a little
  108. // bit to see which one we got. This will cause a slight
  109. // delay when shutting down the process via ctrl-z, but it's
  110. // not really perceptible. In the ctrl-C case we will return
  111. // as soon as the event is signaled.
  112. #if SILVERLIGHT
  113. if (_ctrlCEvent != null && _ctrlCEvent.WaitOne(100))
  114. #else
  115. if (_ctrlCEvent != null && _ctrlCEvent.WaitOne(100, false))
  116. #endif
  117. {
  118. // received ctrl-C
  119. return "";
  120. } else {
  121. // received ctrl-Z
  122. return null;
  123. }
  124. }
  125. return "".PadLeft(autoIndentSize) + res;
  126. }
  127. public virtual void Write(string text, Style style) {
  128. switch (style) {
  129. case Style.Prompt: WriteColor(_output, text, _promptColor); break;
  130. case Style.Out: WriteColor(_output, text, _outColor); break;
  131. case Style.Error: WriteColor(_errorOutput, text, _errorColor); break;
  132. case Style.Warning: WriteColor(_errorOutput, text, _warningColor); break;
  133. }
  134. }
  135. public void WriteLine(string text, Style style) {
  136. Write(text + Environment.NewLine, style);
  137. }
  138. public void WriteLine() {
  139. Write(Environment.NewLine, Style.Out);
  140. }
  141. #endregion
  142. #region IDisposable Members
  143. public void Dispose() {
  144. if (_ctrlCEvent != null) {
  145. _ctrlCEvent.Close();
  146. }
  147. GC.SuppressFinalize(this);
  148. }
  149. #endregion
  150. }
  151. }