diff --git a/TaskRunner/CommandReader.cs b/TaskRunner/CommandReader.cs new file mode 100644 index 0000000..217b159 --- /dev/null +++ b/TaskRunner/CommandReader.cs @@ -0,0 +1,66 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; + +namespace TaskRunner +{ + internal class CommandReader + { + private readonly ITaskLibrary taskLibrary; + private string buffer; + + internal CommandReader(ITaskLibrary TaskLibrary) + { + taskLibrary = TaskLibrary; + } + + internal string GetCommand() + { + buffer = string.Empty; + while (true) + { + ConsoleKeyInfo keyInfo = Console.ReadKey(true); + + switch (keyInfo.Key) + { + case ConsoleKey.Backspace: + RemoveChar(1); + break; + case ConsoleKey.Tab: + string offer = taskLibrary.OfferTaskName(buffer, 0); + if (string.IsNullOrEmpty(offer)) + { + Console.Beep(); + } + else + { + RemoveChar(int.MaxValue); + Console.Write(offer); + buffer = offer; + } + break; + case ConsoleKey.Enter: + Console.WriteLine(); + return buffer; + break; + default: + if (char.IsLetterOrDigit(keyInfo.KeyChar) || char.IsPunctuation(keyInfo.KeyChar)) + { + buffer += keyInfo.KeyChar; + Console.Write(keyInfo.KeyChar); + } + break; + } + } + } + + private void RemoveChar(int count) + { + int countToRemove = count < buffer.Length ? count : buffer.Length; + buffer = buffer.Remove(buffer.Length - countToRemove); + string display = string.Concat(new string('\b',countToRemove), new string(' ',countToRemove), new string('\b',countToRemove)); + Console.Write(display); + } + } +} diff --git a/TaskRunner/ITaskLibrary.cs b/TaskRunner/ITaskLibrary.cs index e71bae9..4d8ca08 100644 --- a/TaskRunner/ITaskLibrary.cs +++ b/TaskRunner/ITaskLibrary.cs @@ -6,5 +6,6 @@ internal interface ITaskLibrary { void RunTask(string name); ReadOnlyCollection Tasks(); + string OfferTaskName(string partial, int offerIndex); } } \ No newline at end of file diff --git a/TaskRunner/Runner.cs b/TaskRunner/Runner.cs index 0d9efb9..f7381e0 100644 --- a/TaskRunner/Runner.cs +++ b/TaskRunner/Runner.cs @@ -6,9 +6,11 @@ namespace TaskRunner public class Runner { private readonly ITaskLibrary taskLibrary; + private CommandReader commandReader; public Runner() { taskLibrary = new TaskLibrary(); + commandReader = new CommandReader(taskLibrary); } public void Go() @@ -18,10 +20,12 @@ public void Go() private void SayHello() { - Console.WriteLine("Welcome to the TaskRunner. Type 'list' to see a list of available tasks to run. Type the name of task to run it or type 'exit' to close the TaskRunner."); - + Console.WriteLine("Welcome to the TaskRunner. Type 'list' to see a list of available tasks to run."); + Console.WriteLine("Type the name of task to run it or type 'exit' to close the TaskRunner."); + Console.WriteLine("Task names have auto complete feature type part of the name and hit [TAB]."); + Console.WriteLine(""); Console.Write(" > "); - var command = Console.ReadLine(); + var command = commandReader.GetCommand(); TakeAction(command); } @@ -58,8 +62,10 @@ private void TakeAction(string command) Console.WriteLine(); Console.WriteLine("Task '{0}' executed. What's next?", command); Console.Write(" > "); - var newCommand = Console.ReadLine(); + var newCommand = commandReader.GetCommand(); TakeAction(newCommand); } + + } } diff --git a/TaskRunner/TaskLibrary.cs b/TaskRunner/TaskLibrary.cs index 4e9e701..a3c245f 100644 --- a/TaskRunner/TaskLibrary.cs +++ b/TaskRunner/TaskLibrary.cs @@ -8,12 +8,14 @@ namespace TaskRunner internal class TaskLibrary : ITaskLibrary { private readonly List tasks; + private readonly List taskNames; private readonly ITaskLoader taskLoader; public TaskLibrary() { taskLoader = new TaskLoader(); tasks = taskLoader.FindTasks(); + taskNames = (from task in tasks select task.Name).ToList(); } public void RunTask(string name) @@ -32,5 +34,12 @@ public ReadOnlyCollection Tasks() { return tasks.AsReadOnly(); } + + public string OfferTaskName(string partial, int offerIndex) + { + return (from name in taskNames + where name.ToLowerInvariant().Contains(partial.ToLowerInvariant()) + select name).ElementAtOrDefault(offerIndex); + } } } \ No newline at end of file diff --git a/TaskRunner/TaskRunner.csproj b/TaskRunner/TaskRunner.csproj index 8cee938..158998b 100644 --- a/TaskRunner/TaskRunner.csproj +++ b/TaskRunner/TaskRunner.csproj @@ -40,6 +40,7 @@ +