Last active
December 16, 2020 14:15
-
-
Save MerijnHendriks/d7b832974fd8c109e60bf8f408a5dd25 to your computer and use it in GitHub Desktop.
A simple and modular CLI application
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* CommandSystem.cs | |
* License: NCSA Open Source | |
* Author: Merijn Hendriks | |
*/ | |
namespace App | |
{ | |
public class CommandSystem : ISystem | |
{ | |
List<ICommand> Commands; | |
public CommandSystem() | |
{ | |
Commands = new List<ICommand>(); | |
} | |
public void OnUpdate() | |
{ | |
var input = Console.ReadLine().ToLower().Split(' '); | |
SystemManager.Get<DisplaySystem>().ClearScreen(); | |
foreach (var command in Commands) | |
{ | |
if (command.IsMatch(input)) | |
{ | |
command.Execute(input); | |
return; | |
} | |
} | |
Console.WriteLine($"Cannot find command {input[0]}"); | |
} | |
public void Add<T>() where T : ICommand, new() | |
{ | |
Commands.Add(new T()); | |
} | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* DisplaySystem.cs | |
* License: NCSA Open Source | |
* Author: Merijn Hendriks | |
*/ | |
namespace App | |
{ | |
public class DisplaySystem : ISystem | |
{ | |
public void OnUpdate() | |
{ | |
// code here | |
} | |
public void ClearScreen() | |
{ | |
Console.Clear(); | |
} | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* ExitCommand.cs | |
* License: NCSA Open Source | |
* Author: Merijn Hendriks | |
*/ | |
namespace App | |
{ | |
public class ExitCommand : ICommand | |
{ | |
public bool IsMatch(string[] input) | |
{ | |
return input[0] == "exit"; | |
} | |
public void Execute(string[] input) | |
{ | |
Program.executing = false; | |
Console.WriteLine("Terminating game..."); | |
} | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* HelpCommand.cs | |
* License: NCSA Open Source | |
* Author: Merijn Hendriks | |
*/ | |
namespace App | |
{ | |
public class HelpCommand : ICommand | |
{ | |
public bool IsMatch(string[] input) | |
{ | |
return input[0] == "help"; | |
} | |
public void Execute(string[] input) | |
{ | |
Console.WriteLine("help: shows this menu"); | |
Console.WriteLine("exit: terminates game"); | |
} | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* ICommand.cs | |
* License: NCSA Open Source | |
* Author: Merijn Hendriks | |
*/ | |
namespace App | |
{ | |
public interface ICommand | |
{ | |
public bool IsMatch(string[] input); | |
public void Execute(string[] input); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* ISystem.cs | |
* License: NCSA Open Source | |
* Author: Merijn Hendriks | |
*/ | |
namespace App | |
{ | |
public interface ISystem | |
{ | |
public void OnUpdate(); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* Program.cs | |
* License: NCSA Open Source | |
* Author: Merijn Hendriks | |
*/ | |
using System; | |
using System.Collections.Generic; | |
namespace App | |
{ | |
static class Program | |
{ | |
public static bool executing; | |
static Program() | |
{ | |
executing = true; | |
} | |
static void Main(string[] args) | |
{ | |
// add systems | |
SystemManager.Add<DisplaySystem>(); | |
SystemManager.Add<CommandSystem>(); | |
// add commands | |
var commands = SystemManager.Get<CommandSystem>(); | |
commands.Add<HelpCommand>(); | |
commands.Add<ExitCommand>(); | |
// app loop | |
while (executing) | |
{ | |
SystemManager.OnUpdate(); | |
} | |
} | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* SystemManager.cs | |
* License: NCSA Open Source | |
* Author: Merijn Hendriks | |
*/ | |
namespace App | |
{ | |
public static class SystemManager | |
{ | |
private static List<ISystem> Systems; | |
static SystemManager() | |
{ | |
Systems = new List<ISystem>(); | |
} | |
public static void OnUpdate() | |
{ | |
foreach (var system in Systems) | |
{ | |
system.OnUpdate(); | |
} | |
} | |
public static void Add<T>() where T : ISystem, new() | |
{ | |
Systems.Add(new T()); | |
} | |
public static T Get<T>() where T : ISystem | |
{ | |
foreach (var system in Systems) | |
{ | |
if (system.GetType() == typeof(T)) | |
{ | |
return (T)system; | |
} | |
} | |
return default; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment