92 lines
2.1 KiB
92 lines
2.1 KiB
// <file> |
|
// <copyright see="prj:///doc/copyright.txt"/> |
|
// <license see="prj:///doc/license.txt"/> |
|
// <owner name="Matthew Ward" email="mrward@users.sourceforge.net"/> |
|
// <version>$Revision$</version> |
|
// </file> |
|
|
|
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); } |
|
} |
|
|
|
/// <summary> |
|
/// Runs the console host in its own thread. |
|
/// </summary> |
|
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(); |
|
} |
|
|
|
/// <remarks> |
|
/// 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. |
|
/// </remarks> |
|
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]); |
|
} |
|
} |
|
}
|
|
|