Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
66 changes: 66 additions & 0 deletions TaskRunner/CommandReader.cs
Original file line number Diff line number Diff line change
@@ -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);
}
}
}
1 change: 1 addition & 0 deletions TaskRunner/ITaskLibrary.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,6 @@ internal interface ITaskLibrary
{
void RunTask(string name);
ReadOnlyCollection<TaskInformation> Tasks();
string OfferTaskName(string partial, int offerIndex);
}
}
14 changes: 10 additions & 4 deletions TaskRunner/Runner.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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);
}

Expand Down Expand Up @@ -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);
}


}
}
9 changes: 9 additions & 0 deletions TaskRunner/TaskLibrary.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,14 @@ namespace TaskRunner
internal class TaskLibrary : ITaskLibrary
{
private readonly List<TaskInformation> tasks;
private readonly List<string> 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)
Expand All @@ -32,5 +34,12 @@ public ReadOnlyCollection<TaskInformation> 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);
}
}
}
1 change: 1 addition & 0 deletions TaskRunner/TaskRunner.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
<Reference Include="System.Xml" />
</ItemGroup>
<ItemGroup>
<Compile Include="CommandReader.cs" />
<Compile Include="ITask.cs" />
<Compile Include="ITaskLibrary.cs" />
<Compile Include="ITaskLoader.cs" />
Expand Down