Skip to content
This repository was archived by the owner on Jan 3, 2022. It is now read-only.

Commit 87cf5c0

Browse files
committed
Added support for setting input device instances for applications
1 parent 2f951a2 commit 87cf5c0

File tree

4 files changed

+51
-0
lines changed

4 files changed

+51
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
using System;
2+
using System.IO;
3+
4+
namespace Deveel.Console {
5+
public sealed class StringInputDevice : InputDevice {
6+
private StringReader baseReader;
7+
8+
public StringInputDevice() {
9+
}
10+
11+
public StringInputDevice(string s) {
12+
baseReader = new StringReader(s);
13+
}
14+
15+
public void SetInput(string s) {
16+
if (baseReader != null) {
17+
baseReader.Dispose();
18+
baseReader = null;
19+
}
20+
21+
baseReader = new StringReader(s);
22+
}
23+
24+
public override int Read(char[] buffer, int index, int count) {
25+
if (baseReader == null)
26+
return 0;
27+
28+
return baseReader.Read(buffer, index, count);
29+
}
30+
31+
protected override void Dispose(bool disposing) {
32+
if (disposing) {
33+
if (baseReader != null)
34+
baseReader.Dispose();
35+
baseReader = null;
36+
}
37+
38+
base.Dispose(disposing);
39+
}
40+
}
41+
}

src/dshell-nunit.vs2010/dshell-nunit.vs2010.csproj

+1
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
<Reference Include="System" />
3535
</ItemGroup>
3636
<ItemGroup>
37+
<Compile Include="Deveel.Console\StringInputDevice.cs" />
3738
<Compile Include="Deveel.Console\StringWriteEventArgs.cs" />
3839
<Compile Include="Deveel.Console\TestOutputDevice.cs" />
3940
<Compile Include="Properties\AssemblyInfo.cs" />

src/dshell/Deveel.Console/IApplicationContext.cs

+2
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,8 @@ public interface IApplicationContext : IExecutionContext {
4444
bool IsRunning { get; }
4545

4646

47+
void SetInputDevice(InputDevice device);
48+
4749
void SetOutDevice(OutputDevice device);
4850

4951
void SetErrorDevice(OutputDevice device);

src/dshell/Deveel.Console/ShellApplication.cs

+7
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,13 @@ public void SetPrompt(string text) {
170170
(input as ConsoleInputDevice).Prompt = prompt;
171171
}
172172

173+
public void SetInputDevice(InputDevice device) {
174+
if (device == null)
175+
throw new ArgumentNullException("device");
176+
177+
input = device;
178+
}
179+
173180
public void SetOutDevice(OutputDevice device) {
174181
if (device == null)
175182
throw new ArgumentNullException("device");

0 commit comments

Comments
 (0)