// // // // // $Revision$ // using System; using System.Text; using System.Threading; using IronPython.Hosting; using IronPython.Runtime; using Microsoft.Scripting.Hosting; using Microsoft.Scripting.Hosting.Shell; namespace ICSharpCode.PythonBinding { public class PythonConsoleHost : ConsoleHost, IDisposable { Thread thread; IConsoleTextEditor textEditor; PythonConsole pythonConsole; public PythonConsoleHost(IConsoleTextEditor textEditor) { this.textEditor = textEditor; pythonConsole = new PythonConsole(textEditor); } public PythonConsole PythonConsole { get { return pythonConsole; } } protected override Type Provider { get { return typeof(PythonContext); } } /// /// Runs the console host in its own thread. /// public void Run() { thread = new Thread(RunConsole); thread.Start(); } public void Dispose() { if (pythonConsole != null) { pythonConsole.Dispose(); } if (thread != null) { thread.Join(); } } protected override CommandLine CreateCommandLine() { return new PythonCommandLine(); } protected override OptionsParser CreateOptionsParser() { return new PythonOptionsParser(); } /// /// After the engine is created the standard output is replaced with our custom Stream class so we /// can redirect the stdout to the text editor window. /// This can be done in this method since the Runtime object will have been created before this method /// is called. /// protected override IConsole CreateConsole(ScriptEngine engine, CommandLine commandLine, ConsoleOptions options) { SetOutput(new PythonOutputStream(textEditor)); pythonConsole.CommandLine = commandLine; return pythonConsole; } protected virtual void SetOutput(PythonOutputStream stream) { Runtime.IO.SetOutput(stream, Encoding.UTF8); } void RunConsole() { Run(new string[0]); } } }