New Commands & piped interpreter!

This commit is contained in:
2025-07-09 00:22:59 +03:00
parent e3dc8d9d42
commit 8927637048
19 changed files with 239 additions and 76 deletions

View File

@@ -1,54 +0,0 @@
using System;
using System.Collections.Generic;
using System.Runtime.CompilerServices;
public class CommandInterpreter
{
public readonly Dictionary<string, ICommand> _commands = new();
public CommandInterpreter()
{
RegisterCommand(new GreetCommand());
RegisterCommand(new HelpCommand(this));
}
public void RegisterCommand(ICommand command)
{
_commands[command.Name.ToLower()] = command;
}
public void Execute(string input)
{
string[] parts = input.Split(" ", StringSplitOptions.RemoveEmptyEntries);
if (parts.Length == 0) return;
string commandName = parts[0].ToLower();
string[] args = parts.Length > 1 ? parts[1..] : new string[0];
if (_commands.TryGetValue(commandName, out var command))
{
command.Execute(args);
}
else
{
Console.WriteLine("Err - uknown Command : '{commandName}'. Type help For a list of commands");
}
}
public IEnumerable<ICommand> GetAllCommands()
{
return _commands.Values;
}
}

View File

@@ -0,0 +1,46 @@
using System;
using System.ComponentModel;
using TermApp.Core;
namespace TermApp.Commands
{
public class AddCommand : ICommand
{
public string Name => "add";
public string Description => "Adds two integers together args : a + b";
public void Execute(string[] args, string input = null)
{
if (args.Length < 1)
{
Console.WriteLine(Description);
return;
}
if (int.TryParse(args[0], out int a) && int.TryParse(args[1], out int b))
{
int result = a + b;
Console.WriteLine(result);
return;
}
else
{
Console.WriteLine("Make sure Both Args are integers!");
}
}
}
}

View File

@@ -0,0 +1,26 @@
using TermApp.Core;
namespace TermApp.Commands
{
public class ClearCommand : ICommand
{
public string Name => "clear";
public string Description => "Clears the Terminal";
public void Execute(string[] args, string input = null)
{
Console.Clear();
if (args.Length != 0)
{
if (args[0] == "log") Console.WriteLine("Cleared :)");
}
}
}
}

View File

@@ -0,0 +1,31 @@
using TermApp.Core;
using System;
using System.ComponentModel;
namespace TermApp.Commands
{
public class EchoCommand : ICommand
{
public string Name => "echo";
public string Description => "Prints Input";
public void Execute(string[] args, string input = null)
{
if (args.Length > 0)
Console.WriteLine(String.Join(" ", args));
else if (input != null)
Console.WriteLine(input);
else
Console.WriteLine();
}
}
}

View File

@@ -1,11 +1,15 @@
using System;
using TermApp.Core;
public class GreetCommand : ICommand
namespace TermApp.Commands
{
public class GreetCommand : ICommand
{
public string Name => "greet";
public string Description => "greets someone.. args [name]";
public void Execute(string[] args)
public void Execute(string[] args, string input = null)
{
if (args.Length == 0)
@@ -19,4 +23,7 @@ public class GreetCommand : ICommand
Console.WriteLine("Greetings , " + args[0]);
}
}
}
}

View File

@@ -1,4 +1,10 @@
using System;
using TermApp.Core;
namespace TermApp.Commands
{
public class HelpCommand : ICommand
{
@@ -13,10 +19,10 @@ public class HelpCommand : ICommand
public string Name => "help";
public String Description => "Shows the Help pop Up";
public void Execute(string[] args)
public void Execute(string[] args, string input = null)
{
Console.WriteLine("Avaliable Commands:");
foreach (var cmd in _interpreter.GetAllCommands())
foreach (var cmd in _interpreter.GetAllCommands())
{
Console.WriteLine($"Command : {cmd.Name} | {cmd.Description}");
@@ -24,4 +30,6 @@ public class HelpCommand : ICommand
}
}
}
}

View File

@@ -0,0 +1,91 @@
using System;
using System.Collections.Generic;
using System.Runtime.CompilerServices;
using TermApp.Commands;
namespace TermApp.Core
{
public class CommandInterpreter
{
public readonly Dictionary<string, ICommand> _commands = new();
public CommandInterpreter()
{
RegisterCommand(new GreetCommand());
RegisterCommand(new HelpCommand(this));
RegisterCommand(new AddCommand());
RegisterCommand(new EchoCommand());
RegisterCommand(new ClearCommand());
}
public void RegisterCommand(ICommand command)
{
_commands[command.Name.ToLower()] = command;
}
public void Execute(string input)
{
string[] pipedCommands = input.Split("|", StringSplitOptions.RemoveEmptyEntries);
string pipedOutput = null;
foreach (var cmdInput in pipedCommands)
{
string trimmed = cmdInput.Trim();
string[] parts = input.Split(" ", StringSplitOptions.RemoveEmptyEntries);
if (parts.Length == 0) continue;
string commandName = parts[0].ToLower();
string[] args = parts.Length > 1 ? parts[1..] : new string[0];
if (_commands.TryGetValue(commandName, out var command))
{
command.Execute(args);
}
else
{
Console.WriteLine($"Err - uknown Command : '{commandName}'. Type help For a list of commands");
return;
}
}
if (!string.IsNullOrWhiteSpace(pipedOutput))
{
Console.WriteLine(pipedOutput);
}
}
public IEnumerable<ICommand> GetAllCommands()
{
return _commands.Values;
}
}
}

View File

@@ -0,0 +1,14 @@
namespace TermApp.Core
{
public interface ICommand
{
string Name { get; }
string Description { get; }
void Execute(string[] args, string input = null);
}
}

View File

@@ -1,8 +0,0 @@
public interface ICommand
{
string Name { get; }
string Description { get; }
void Execute(string[] args);
}

View File

@@ -1,5 +1,6 @@
using System;
using System.Collections.Generic;
using TermApp.Core;
namespace TermApp
{
@@ -8,20 +9,21 @@ namespace TermApp
static void Main(string[] args)
{
Console.WriteLine("Hello, Works!");
CommandInterpreter interpreter = new CommandInterpreter();
Console.WriteLine("Type help to see avaliable Commands :.) or type exit to quit");
string lastCommand = "Command Cache :)";
while (true)
{
Console.Write("Input >");
string input = Console.ReadLine();
Console.Write($"[{lastCommand}] - Input >");
string input = Console.ReadLine() ?? throw new ArgumentException();
lastCommand = input;
if (string.IsNullOrWhiteSpace(input)) continue;
if (input.ToLower() == "exit") break;
if (input.ToLower() == "exit" || input.ToLower() == "e") break;
interpreter.Execute(input);

View File

@@ -13,7 +13,7 @@ using System.Reflection;
[assembly: System.Reflection.AssemblyCompanyAttribute("TermEngine")]
[assembly: System.Reflection.AssemblyConfigurationAttribute("Debug")]
[assembly: System.Reflection.AssemblyFileVersionAttribute("1.0.0.0")]
[assembly: System.Reflection.AssemblyInformationalVersionAttribute("1.0.0+fd78d9283e191920ee4f6e700fc03391a8880f43")]
[assembly: System.Reflection.AssemblyInformationalVersionAttribute("1.0.0+e3dc8d9d420fc51fad3a8526f470c6c1f428a000")]
[assembly: System.Reflection.AssemblyProductAttribute("TermEngine")]
[assembly: System.Reflection.AssemblyTitleAttribute("TermEngine")]
[assembly: System.Reflection.AssemblyVersionAttribute("1.0.0.0")]

View File

@@ -1 +1 @@
432712ba9dc98f037959cf0668caa1627ade9f0aa69473645068c6ea52208868
3d48c29292f9b3da258d45e3bdc45d2e18ec03103ca4e7baa8461e515fbe4981

View File

@@ -1 +1 @@
4fba7de9be587e6bc37da5c47243a26c97f753def2985a170b262675dd805d5f
a2dd4e9d73438a6245251deea74b31585bcc1a2fd3c9c3f80d70811058104251