Skip to content

Instantly share code, notes, and snippets.

@MerijnHendriks
Last active December 16, 2020 14:15
Show Gist options
  • Save MerijnHendriks/d7b832974fd8c109e60bf8f408a5dd25 to your computer and use it in GitHub Desktop.
Save MerijnHendriks/d7b832974fd8c109e60bf8f408a5dd25 to your computer and use it in GitHub Desktop.
A simple and modular CLI application
/* 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());
}
}
}
/* 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();
}
}
}
/* 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...");
}
}
}
/* 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");
}
}
}
/* ICommand.cs
* License: NCSA Open Source
* Author: Merijn Hendriks
*/
namespace App
{
public interface ICommand
{
public bool IsMatch(string[] input);
public void Execute(string[] input);
}
}
/* ISystem.cs
* License: NCSA Open Source
* Author: Merijn Hendriks
*/
namespace App
{
public interface ISystem
{
public void OnUpdate();
}
}
/* 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();
}
}
}
}
/* 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