PageRenderTime 4ms CodeModel.GetById 5ms app.highlight 10ms 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
  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
 16using System; using Microsoft;
 17using System.IO;
 18using Microsoft.Scripting;
 19using Microsoft.Scripting.Utils;
 20using System.Threading;
 21
 22namespace Microsoft.Scripting.Hosting.Shell {
 23
 24    public class BasicConsole : IConsole, IDisposable {
 25
 26        private TextWriter _output;
 27        private TextWriter _errorOutput;
 28        private AutoResetEvent _ctrlCEvent;
 29        private Thread _creatingThread;
 30
 31        public TextWriter Output {
 32            get { return _output; }
 33            set {
 34                ContractUtils.RequiresNotNull(value, "value");
 35                _output = value;
 36            }
 37        }
 38
 39        public TextWriter ErrorOutput {
 40            get { return _errorOutput; }
 41            set {
 42                ContractUtils.RequiresNotNull(value, "value");
 43                _errorOutput = value;
 44            }
 45        }
 46
 47        protected AutoResetEvent CtrlCEvent {
 48            get { return _ctrlCEvent; }
 49            set { _ctrlCEvent = value; }
 50        }
 51
 52        protected Thread CreatingThread {
 53            get { return _creatingThread; }
 54            set { _creatingThread = value; }
 55        }
 56
 57        private ConsoleColor _promptColor;
 58        private ConsoleColor _outColor;
 59        private ConsoleColor _errorColor;
 60        private ConsoleColor _warningColor;
 61
 62        public BasicConsole(bool colorful) {            
 63            _output = System.Console.Out;
 64            _errorOutput = System.Console.Error;
 65            SetupColors(colorful);
 66
 67            _creatingThread = Thread.CurrentThread;            
 68
 69#if !SILVERLIGHT // ConsoleCancelEventHandler
 70            Console.CancelKeyPress += new ConsoleCancelEventHandler(delegate(object sender, ConsoleCancelEventArgs e) {
 71                if (e.SpecialKey == ConsoleSpecialKey.ControlC) {
 72                    e.Cancel = true;
 73                    _ctrlCEvent.Set();
 74                    _creatingThread.Abort(new KeyboardInterruptException(""));
 75                }
 76            });
 77#endif
 78            _ctrlCEvent = new AutoResetEvent(false);
 79        }
 80
 81        private void SetupColors(bool colorful) {
 82
 83            if (colorful) {
 84                _promptColor = PickColor(ConsoleColor.Gray, ConsoleColor.White);
 85                _outColor = PickColor(ConsoleColor.Green, ConsoleColor.White);
 86                _errorColor = PickColor(ConsoleColor.Red, ConsoleColor.White);
 87                _warningColor = PickColor(ConsoleColor.Yellow, ConsoleColor.White);
 88            } else {
 89#if !SILVERLIGHT
 90                _promptColor = _outColor = _errorColor = _warningColor = Console.ForegroundColor;
 91#endif
 92            }
 93        }
 94
 95        private static ConsoleColor PickColor(ConsoleColor best, ConsoleColor other) {
 96#if SILVERLIGHT
 97            return best;
 98#else
 99            if (Console.BackgroundColor != best) {
100                return best;
101            }
102
103            return other;
104#endif
105        }
106
107        protected void WriteColor(TextWriter output, string str, ConsoleColor c) {
108#if !SILVERLIGHT // Console.ForegroundColor
109            ConsoleColor origColor = Console.ForegroundColor;
110            Console.ForegroundColor = c;
111#endif
112            output.Write(str);
113            output.Flush();
114
115#if !SILVERLIGHT // Console.ForegroundColor
116            Console.ForegroundColor = origColor;
117#endif
118        }
119
120        #region IConsole Members
121
122        public virtual string ReadLine(int autoIndentSize) {
123            Write("".PadLeft(autoIndentSize), Style.Prompt);
124
125            string res = Console.In.ReadLine();
126            if (res == null) {
127                // we have a race - the Ctrl-C event is delivered
128                // after ReadLine returns.  We need to wait for a little
129                // bit to see which one we got.  This will cause a slight
130                // delay when shutting down the process via ctrl-z, but it's
131                // not really perceptible.  In the ctrl-C case we will return
132                // as soon as the event is signaled.
133#if SILVERLIGHT
134                if (_ctrlCEvent != null && _ctrlCEvent.WaitOne(100))
135#else
136                if (_ctrlCEvent != null && _ctrlCEvent.WaitOne(100, false))
137#endif
138 {
139                    // received ctrl-C
140                    return "";
141                } else {
142                    // received ctrl-Z
143                    return null;
144                }
145            }
146            return "".PadLeft(autoIndentSize) + res;
147        }
148
149        public virtual void Write(string text, Style style) {
150            switch (style) {
151                case Style.Prompt: WriteColor(_output, text, _promptColor); break;
152                case Style.Out: WriteColor(_output, text, _outColor); break;
153                case Style.Error: WriteColor(_errorOutput, text, _errorColor); break;
154                case Style.Warning: WriteColor(_errorOutput, text, _warningColor); break;
155            }
156        }
157
158        public void WriteLine(string text, Style style) {
159            Write(text + Environment.NewLine, style);
160        }
161
162        public void WriteLine() {
163            Write(Environment.NewLine, Style.Out);
164        }
165
166        #endregion
167
168        #region IDisposable Members
169
170        public void Dispose() {
171            if (_ctrlCEvent != null) {
172                _ctrlCEvent.Close();
173            }
174
175            GC.SuppressFinalize(this);
176        }
177
178        #endregion
179    }
180
181}
182