Skip to content
Browse files

removed injection of commandhistory into IConsole derived classes.

improvements of the implementation
  • Loading branch information...
1 parent 4753399 commit 36788239c39710eb479ce46d15c060d1bc687b89 Stephan committed Jun 8, 2012
View
17 src/shell.me/ShellMe.CommandLine.Tests/CommandInitTests.cs
@@ -143,24 +143,19 @@ public void PrintsExceptionsToTheConsole()
[Test]
public void ReturnsLastCommandOnArrowUp()
{
- var console = new TestConsole(new List<string>() { "test", "exit" });
+ //System.ConsoleKey.UpArrow.ToString() represented as "<" in tests
+ var console = new TestConsole(new List<string>() { "test", "<", "exit" });
var commandFactory = new CommandFactory(new[] { new TestCommand() });
- var commandLoop = new CommandLoop(console, commandFactory);
- commandLoop.Start(new[] { "test" });
+ var commandLoop = new CommandLoop(console, commandFactory,new TestCommandHistory(console) );
+ commandLoop.Start(new[] { "test", "<", "exit" });
Assert.AreEqual("Run. Test: False, Text: ", console.OutputQueue[0]);
Assert.AreEqual("Enter commands or type exit to close", console.OutputQueue[1]);
Assert.AreEqual("Run. Test: False, Text: ", console.OutputQueue[2]);
Assert.AreEqual("Enter commands or type exit to close", console.OutputQueue[3]);
- Assert.AreEqual(4, console.OutputQueue.Count);
-
- commandLoop.Start(new[] { System.ConsoleKey.UpArrow.ToString() });
+ Assert.AreEqual("test", console.OutputQueue[4]);
+ Assert.AreEqual(5, console.OutputQueue.Count);
- Assert.AreEqual("Run. Test: False, Text: ", console.OutputQueue[0]);
- Assert.AreEqual("Enter commands or type exit to close", console.OutputQueue[1]);
- Assert.AreEqual("Run. Test: False, Text: ", console.OutputQueue[2]);
- Assert.AreEqual("Enter commands or type exit to close", console.OutputQueue[3]);
- Assert.AreEqual(4, console.OutputQueue.Count);
}
View
23 src/shell.me/ShellMe.CommandLine/CommandLoop.cs
@@ -1,5 +1,6 @@
using System;
using System.Collections.Generic;
+using System.Globalization;
using ShellMe.CommandLine.CommandHandling;
namespace ShellMe.CommandLine
@@ -8,23 +9,30 @@ public class CommandLoop
{
private readonly CommandFactory _commandFactory;
private readonly ICommandPropertyWalker _commandPropertyWalker;
+ private readonly ICommandHistory _commandHistory;
- public CommandLoop() : this(new NativeConsoleWrapper(new InMemoryCommandHistory()))
+ public CommandLoop() : this(new NativeConsoleWrapper())
{}
public CommandLoop(IConsole console) : this(console, new CommandFactory(new ICommand[]{}))
{
}
- public CommandLoop(IConsole console, CommandFactory commandFactory): this(console,commandFactory, new CommandPropertyWalker())
+ public CommandLoop(IConsole console, CommandFactory commandFactory): this(console,commandFactory, new CommandPropertyWalker(), new InMemoryCommandHistory(console))
{
}
- public CommandLoop(IConsole console, CommandFactory commandFactory, ICommandPropertyWalker commandPropertyWalker)
+ public CommandLoop(IConsole console, CommandFactory commandFactory, ICommandHistory commandHistory)
+ : this(console, commandFactory, new CommandPropertyWalker(), commandHistory)
+ {
+ }
+
+ public CommandLoop(IConsole console, CommandFactory commandFactory, ICommandPropertyWalker commandPropertyWalker, ICommandHistory commandHistory)
{
Console = console;
_commandFactory = commandFactory;
_commandPropertyWalker = commandPropertyWalker;
+ _commandHistory = commandHistory;
}
@@ -47,15 +55,18 @@ public void Start(string[] args)
TryToProceedCommand(command, args);
+ var input = string.Empty;
while (!nonInteractive && !exit)
{
- Console.WriteLine("Enter commands or type exit to close");
+
+ if (input != CommandStates.LastCommandWasPrintet.ToString())
+ Console.WriteLine("Enter commands or type exit to close");
if (!nonInteractive)
{
- var input = Console.ReadLine();
+ input = _commandHistory.GetCommand();
- if (!string.IsNullOrEmpty(input))
+ if (!string.IsNullOrEmpty(input) && input != CommandStates.LastCommandWasPrintet.ToString())
{
if (input.ToLower() == "exit")
exit = true;
View
1 src/shell.me/ShellMe.CommandLine/ICommandHistory.cs
@@ -13,5 +13,6 @@ public interface ICommandHistory
string GetPrevioseCommand();
string GetNextCommand();
string FindCommand(string searchstring);
+ string GetCommand();
}
}
View
2 src/shell.me/ShellMe.CommandLine/IConsole.cs
@@ -6,7 +6,7 @@ public interface IConsole
{
void WriteLine(string line);
string ReadLine();
- //ConsoleKeyInfo Readkey();
+ ConsoleKeyInfo Readkey();
ConsoleColor ForegroundColor { get; set; }
void ResetColor();
}
View
58 src/shell.me/ShellMe.CommandLine/InMemoryCommandHistory.cs
@@ -5,15 +5,22 @@
namespace ShellMe.CommandLine
{
- public class InMemoryCommandHistory : ICommandHistory
+ public enum CommandStates
+ {
+ None = 0,
+ LastCommandWasPrintet = 1
+ }
+
+ public class InMemoryCommandHistory : ICommandHistory
{
private readonly List<string> _commandHistory = new List<string>();
private int _currentCommandIndex = 0;
-
private Dictionary<ConsoleKey, Action> _matches;
+ private IConsole _console;
- public InMemoryCommandHistory()
+ public InMemoryCommandHistory(IConsole console)
{
+ _console = console;
_matches = new Dictionary<ConsoleKey, Action>
{
{ ConsoleKey.UpArrow , () => GetPrevioseCommand() },
@@ -44,13 +51,22 @@ public string GetLastCommand()
public string GetPrevioseCommand()
{
- _currentCommandIndex++;
+ if(_currentCommandIndex < _commandHistory.Count)
+ _currentCommandIndex++;
+ else
+ {
+ _currentCommandIndex = _commandHistory.Count - 1;
+ }
return _commandHistory[_currentCommandIndex];
}
public string GetNextCommand()
{
- _currentCommandIndex--;
+ if (_currentCommandIndex != 0)
+ _currentCommandIndex--;
+ else
+ _currentCommandIndex = 0;
+
return _commandHistory[_currentCommandIndex];
}
@@ -59,5 +75,37 @@ public string FindCommand(string searchstring)
return _commandHistory
.FirstOrDefault(cmd => cmd.Contains(searchstring));
}
+
+ public string GetCommand()
+ {
+ bool comandEnd = false;
+ string buffer = string.Empty;
+ int currentIndexOfLine = 0;
+
+ while (!comandEnd)
+ {
+ var keyInfo = _console.Readkey();
+ if (keyInfo.Key == ConsoleKey.Enter || keyInfo.KeyChar == '$')
+ {
+ comandEnd = true;
+ }
+ else if (Matches.ContainsKey(keyInfo.Key))
+ {
+ Action action = Matches[keyInfo.Key];
+ action.Invoke();
+ comandEnd = true;
+ buffer = CommandStates.LastCommandWasPrintet.ToString();
+ }
+ else
+ {
+ buffer += keyInfo.KeyChar;
+ currentIndexOfLine++;
+ }
+ }
+ if (buffer != CommandStates.LastCommandWasPrintet.ToString() && buffer != string.Empty)
+ SaveCommand(buffer);
+ return buffer;
+ }
+
}
}
View
47 src/shell.me/ShellMe.CommandLine/NativeConsoleWrapper.cs
@@ -7,45 +7,26 @@ namespace ShellMe.CommandLine
{
- public NativeConsoleWrapper()
- : this(new InMemoryCommandHistory())
- {}
+ public NativeConsoleWrapper()
+ {}
- public NativeConsoleWrapper(InMemoryCommandHistory commandHistory)
- {
- CommandHistory = commandHistory;
- }
- public void WriteLine(string line)
- {
- Console.WriteLine(line);
- }
+ public void WriteLine(string line)
+ {
+ Console.WriteLine(line);
+ }
- public InMemoryCommandHistory CommandHistory { get; set; }
- public string ReadLine()
- {
- string buffer = string.Empty;
- do
- {
- while (!Console.KeyAvailable)
- {
- var keyInfo = Console.ReadKey();
- if (!CommandHistory.Matches.ContainsKey(keyInfo.Key))
- {
- buffer += keyInfo.KeyChar;
- }
- else
- {
- Action action = CommandHistory.Matches[keyInfo.Key];
- action.Invoke();
- }
- }
- } while (Console.ReadKey(true).Key != ConsoleKey.Enter);
+ public string ReadLine()
+ {
+ return Console.ReadLine();
+ }
- return buffer; // Console.ReadLine();
- }
+ public ConsoleKeyInfo Readkey()
+ {
+ return Console.ReadKey();
+ }
public ConsoleColor ForegroundColor
View
1 src/shell.me/ShellMe.Testing/ShellMe.Testing.csproj
@@ -43,6 +43,7 @@
<ItemGroup>
<Compile Include="ExceptionCommand.cs" />
<Compile Include="IntPropertyCommand.cs" />
+ <Compile Include="TestCommandHistory.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="TestCommand.cs" />
<Compile Include="TestConsole.cs" />
View
114 src/shell.me/ShellMe.Testing/TestCommandHistory.cs
@@ -0,0 +1,114 @@
+using System;
+using System.Collections.Generic;
+using System.Globalization;
+using System.Linq;
+using System.Text;
+
+namespace ShellMe.CommandLine
+{
+ public class TestCommandHistory : ICommandHistory
+ {
+ private readonly List<string> _commandHistory = new List<string>();
+ private int _currentCommandIndex = 0;
+ private Dictionary<char, Action> _matches;
+ private IConsole _console;
+
+
+
+ public TestCommandHistory(IConsole console)
+ {
+ _console = console;
+ _matches = new Dictionary<char, Action>
+ {
+ { '<', () => GetPrevioseCommand() },
+ { '>', () => GetNextCommand() }
+ };
+ }
+
+ public Dictionary<char, Action> Matches
+ {
+ get { return _matches; }
+ set { _matches = value; }
+ }
+
+ public void SaveCommand(string command)
+ {
+ _commandHistory.Add(command);
+ }
+
+ public int GetCurrentCommandIndex()
+ {
+ return _currentCommandIndex;
+ }
+
+ public string GetLastCommand()
+ {
+ return _commandHistory[_commandHistory.Count - 1];
+ }
+
+ public string GetPrevioseCommand()
+ {
+ var ret = string.Empty;
+ if (_currentCommandIndex < _commandHistory.Count)
+ {
+
+ ret = _commandHistory[_currentCommandIndex];
+ _currentCommandIndex++;
+ }
+ else
+ {
+ ret = _commandHistory[_currentCommandIndex];
+ _currentCommandIndex = _commandHistory.Count - 1;
+ }
+ _console.WriteLine(ret);
+ return ret;
+ }
+
+ public string GetNextCommand()
+ {
+ if (_currentCommandIndex != 0)
+ _currentCommandIndex--;
+ else
+ _currentCommandIndex = 0;
+
+ return _commandHistory[_currentCommandIndex];
+ }
+
+ public string FindCommand(string searchstring)
+ {
+ return _commandHistory
+ .FirstOrDefault(cmd => cmd.Contains(searchstring));
+ }
+
+ public string GetCommand()
+ {
+ bool comandEnd = false;
+ string buffer = string.Empty;
+ int currentIndexOfLine = 0;
+
+ while (!comandEnd)
+ {
+ var keyInfo = _console.Readkey();
+ if (keyInfo.Key == ConsoleKey.Enter || keyInfo.KeyChar == '$')
+ {
+ comandEnd = true;
+ }
+ else if (Matches.ContainsKey(keyInfo.KeyChar))
+ {
+ var action = Matches[keyInfo.KeyChar];
+ action.Invoke();
+ comandEnd = true;
+ buffer = CommandStates.LastCommandWasPrintet.ToString();
+ }
+ else
+ {
+ buffer += keyInfo.KeyChar;
+ currentIndexOfLine++;
+ }
+ }
+ if (buffer != CommandStates.LastCommandWasPrintet.ToString() && buffer != string.Empty)
+ SaveCommand(buffer);
+ return buffer;
+ }
+ }
+}
View
38 src/shell.me/ShellMe.Testing/TestConsole.cs
@@ -11,18 +11,11 @@ public class TestConsole : IConsole
private readonly List<string> _commandQueue;
public TestConsole(List<string> commandQueue)
- : this(new InMemoryCommandHistory(),commandQueue)
- {}
-
- public TestConsole(InMemoryCommandHistory commandHistory,List<string> commandQueue)
{
_commandQueue = commandQueue;
OutputQueue = new List<string>();
- CommandHistory = commandHistory;
}
- public InMemoryCommandHistory CommandHistory { get; set; }
-
public List<string> OutputQueue { get; private set; }
public void WriteLine(string line)
@@ -33,27 +26,24 @@ public void WriteLine(string line)
public string ReadLine()
{
var first = _commandQueue.First();
- string buffer = string.Empty;
- foreach (var character in first)
- {
- ConsoleKey consoleKey;
- Enum.TryParse(new string(new[] {character}).ToUpper(), out consoleKey);
+ _commandQueue.RemoveAt(0);
+ return first;
+ }
- var keyInfo = new ConsoleKeyInfo(character, consoleKey, false, false, false);
+ public ConsoleKeyInfo Readkey()
+ {
+ var first = _commandQueue.First();
+ var keyChar = string.IsNullOrEmpty(first) ? '$' : first.ToCharArray()[0];
- if (!CommandHistory.Matches.ContainsKey(keyInfo.Key))
- {
- buffer += keyInfo.KeyChar;
- }
- else
- {
- Action action = CommandHistory.Matches[keyInfo.Key];
- action.Invoke();
- }
- }
+ ConsoleKey consoleKey;
+ Enum.TryParse(new string(new[] { keyChar }).ToUpper(), out consoleKey);
+ var keyInfo = new ConsoleKeyInfo(keyChar, consoleKey, false, false, false);
_commandQueue.RemoveAt(0);
- return buffer;
+ if(first.Length > 0 )
+ _commandQueue.Insert(0,first.Substring(1));
+
+ return keyInfo;
}
public ConsoleColor ForegroundColor { get; set; }

0 comments on commit 3678823

Please sign in to comment.
Something went wrong with that request. Please try again.