public static void Out(string s, ConsoleColor fore, ConsoleColor back = ConsoleColor.Black) { Console.ForegroundColor = fore; Console.BackgroundColor = back; CLI.Out(s); Console.ResetColor(); }
public static void Start(Commands commands) { Initialize(commands); Console.OutputEncoding = System.Text.Encoding.Unicode; CLI.Out(InputPrefix); var input = Console.ReadLine(); while (!Stopped) { if (input.Length != 0) { Run(input); CLI.Line(); } Console.Write(InputPrefix); input = Console.ReadLine(); } }
private static void Run(InputCommand cmd) { Command exec = Commands[cmd.Name.ToLower()]; if (exec == null) { //fuzzy command suggestions var suggestedCommand = FuzzyCommandSearch(cmd.Name).FirstOrDefault(); if (suggestedCommand != null) { CLI.Out("Did you mean "); CLI.Out(suggestedCommand.Name, ConsoleColor.Cyan); if (cmd.Arguments.Count > 0) { CLI.Out(" "); } foreach (var a in cmd.Arguments) { CLI.Out($"-{a.Key} {a.Value} ", ConsoleColor.DarkCyan); } CLI.Out("? "); CLI.Line("[Y/n]", ConsoleColor.Green); Console.ForegroundColor = ConsoleColor.Green; //not using CLI.Out(ConsoleColor) because I want the user input to also be Cyan CLI.Out(InputPrefix); if (Console.ReadLine() == "Y") { exec = suggestedCommand; } else { exec = Commands["help"]; } Console.ResetColor(); } else { exec = Commands["help"]; } } var missingRequiredArgs = exec.RequiredArgs.Select(x => x.Tag.ToLower()).Where(x => !cmd.Arguments.ContainsKey(x.ToLower())).ToList(); if (missingRequiredArgs.Count > 0) { //fuzzy argument suggestions if (cmd.Arguments.Count > 0) { CLI.Out("Did you mean "); CLI.Out(exec.Name + " ", ConsoleColor.DarkCyan); //input arguments that don't exist in the Command definition, mapped to include suggested alternatives var nonexistentArgs = cmd.Arguments .Where(x => !exec.RequiredArgs .Select(y => y.Tag.ToLower()) .Contains(x.Key.ToLower()) ) .Select(x => new { Original = x, Suggestion = exec.RequiredArgs.OrderBy(y => y.Tag.ToLower().DistanceFrom(x.Key.ToLower())).First() }) .ToList(); //input arguments that do exist in the Command definition var existentArgs = cmd.Arguments .Where(x => exec.RequiredArgs .Select(y => y.Tag.ToLower()) .Contains(x.Key.ToLower()) ); foreach (var n in nonexistentArgs) { CLI.Out($"-{n.Suggestion.Tag} {n.Original.Value} ", ConsoleColor.Cyan); } foreach (var e in existentArgs) { CLI.Out($"-{e.Key} {e.Value} ", ConsoleColor.DarkCyan); } CLI.Out("? "); CLI.Line("[Y/n]", ConsoleColor.Green); Console.ForegroundColor = ConsoleColor.Green; //not using CLI.Out(ConsoleColor) because I want the user input to also be Cyan CLI.Out(InputPrefix); if (Console.ReadLine() == "Y") { foreach (var arg in nonexistentArgs) { var inArg = cmd.Arguments.First(x => x.Key == arg.Original.Key); cmd.Arguments.Remove(inArg.Key); cmd.Arguments.Add(arg.Suggestion.Tag, inArg.Value); } } else { exec = Commands["help"]; } Console.ResetColor(); } else { exec = Commands["help"]; } } Stopwatch sw = new Stopwatch(); sw.Start(); try { exec.Function.Invoke(cmd.Arguments); sw.Stop(); CLI.Line(sw.ElapsedMilliseconds + "ms"); } catch (Exception e) { sw.Stop(); CLI.Line(e.Message, ConsoleColor.Red); } }