From 87cf5c04b741b96c8ad0f1c53aee9d6cf908cc06 Mon Sep 17 00:00:00 2001 From: Antonello Provenzano Date: Sat, 19 May 2012 02:48:31 +0200 Subject: [PATCH] Added support for setting input device instances for applications --- .../Deveel.Console/StringInputDevice.cs | 41 +++++++++++++++++++ .../dshell-nunit.vs2010.csproj | 1 + .../Deveel.Console/IApplicationContext.cs | 2 + src/dshell/Deveel.Console/ShellApplication.cs | 7 ++++ 4 files changed, 51 insertions(+) create mode 100644 src/dshell-nunit.vs2010/Deveel.Console/StringInputDevice.cs diff --git a/src/dshell-nunit.vs2010/Deveel.Console/StringInputDevice.cs b/src/dshell-nunit.vs2010/Deveel.Console/StringInputDevice.cs new file mode 100644 index 0000000..1ebfe09 --- /dev/null +++ b/src/dshell-nunit.vs2010/Deveel.Console/StringInputDevice.cs @@ -0,0 +1,41 @@ +using System; +using System.IO; + +namespace Deveel.Console { + public sealed class StringInputDevice : InputDevice { + private StringReader baseReader; + + public StringInputDevice() { + } + + public StringInputDevice(string s) { + baseReader = new StringReader(s); + } + + public void SetInput(string s) { + if (baseReader != null) { + baseReader.Dispose(); + baseReader = null; + } + + baseReader = new StringReader(s); + } + + public override int Read(char[] buffer, int index, int count) { + if (baseReader == null) + return 0; + + return baseReader.Read(buffer, index, count); + } + + protected override void Dispose(bool disposing) { + if (disposing) { + if (baseReader != null) + baseReader.Dispose(); + baseReader = null; + } + + base.Dispose(disposing); + } + } +} \ No newline at end of file diff --git a/src/dshell-nunit.vs2010/dshell-nunit.vs2010.csproj b/src/dshell-nunit.vs2010/dshell-nunit.vs2010.csproj index bac6dbe..ce8773a 100644 --- a/src/dshell-nunit.vs2010/dshell-nunit.vs2010.csproj +++ b/src/dshell-nunit.vs2010/dshell-nunit.vs2010.csproj @@ -34,6 +34,7 @@ + diff --git a/src/dshell/Deveel.Console/IApplicationContext.cs b/src/dshell/Deveel.Console/IApplicationContext.cs index 2172d83..bf33b55 100644 --- a/src/dshell/Deveel.Console/IApplicationContext.cs +++ b/src/dshell/Deveel.Console/IApplicationContext.cs @@ -44,6 +44,8 @@ public interface IApplicationContext : IExecutionContext { bool IsRunning { get; } + void SetInputDevice(InputDevice device); + void SetOutDevice(OutputDevice device); void SetErrorDevice(OutputDevice device); diff --git a/src/dshell/Deveel.Console/ShellApplication.cs b/src/dshell/Deveel.Console/ShellApplication.cs index 366c611..2a7d5a5 100644 --- a/src/dshell/Deveel.Console/ShellApplication.cs +++ b/src/dshell/Deveel.Console/ShellApplication.cs @@ -170,6 +170,13 @@ public void SetPrompt(string text) { (input as ConsoleInputDevice).Prompt = prompt; } + public void SetInputDevice(InputDevice device) { + if (device == null) + throw new ArgumentNullException("device"); + + input = device; + } + public void SetOutDevice(OutputDevice device) { if (device == null) throw new ArgumentNullException("device");