This repository has been archived by the owner on Oct 16, 2020. It is now read-only.
/
RubyConsoleHost.cs
96 lines (81 loc) · 2.33 KB
/
RubyConsoleHost.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
// Copyright (c) AlphaSierraPapa for the SharpDevelop Team (for details please see \doc\copyright.txt)
// This code is distributed under the GNU LGPL (for details please see \doc\license.txt)
using System;
using System.Text;
using System.Threading;
using ICSharpCode.Scripting;
using IronRuby;
using IronRuby.Hosting;
using IronRuby.Runtime;
using Microsoft.Scripting.Hosting;
using Microsoft.Scripting.Hosting.Shell;
namespace ICSharpCode.RubyBinding
{
public class RubyConsoleHost : ConsoleHost, IScriptingConsoleHost
{
Thread thread;
IScriptingConsoleTextEditor textEditor;
RubyConsole rubyConsole;
public RubyConsoleHost(IScriptingConsoleTextEditor textEditor, IControlDispatcher dispatcher)
{
this.textEditor = textEditor;
rubyConsole = new RubyConsole(textEditor, dispatcher);
}
public IScriptingConsole ScriptingConsole {
get { return rubyConsole; }
}
/// <summary>
/// Runs the console host in its own thread.
/// </summary>
public void Run()
{
thread = new Thread(RunConsole);
thread.Start();
}
public void Dispose()
{
if (rubyConsole != null) {
rubyConsole.Dispose();
}
if (thread != null) {
thread.Join();
}
}
void RunConsole()
{
Run(new string[0]);
}
protected virtual void SetOutput(ScriptingConsoleOutputStream stream)
{
Runtime.IO.SetOutput(stream, Encoding.UTF8);
}
protected override OptionsParser CreateOptionsParser()
{
return new RubyOptionsParser();
}
protected override CommandLine CreateCommandLine()
{
return new RubyCommandLine();
}
protected override LanguageSetup CreateLanguageSetup()
{
return Ruby.CreateRubySetup();
}
protected override Type Provider {
get { return typeof(RubyContext); }
}
/// <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)
{
ScriptingConsoleOutputStream stream = rubyConsole.CreateOutputStream();
SetOutput(stream);
rubyConsole.CommandLine = commandLine;
return rubyConsole;
}
}
}