Exemplo n.º 1
0
        /// <summary>
        /// Command handler. Deals with all Shell commands.
        /// </summary>
        /// <returns>Calls a function based on the command given.</returns>
        private static void Command()
        {
            // Bool to stop the command function. I don't really know why its executing the if and else. Probably
            //                            because I'm using a global variable.
            bool running = true;

            foreach (var alias in Global.ALIASES.Keys)
            {
                if (Global.INPUT.StartsWith(alias))
                {
                    // TODO: Check if the alias is in the aliased command.
                    var regex = new Regex(Regex.Escape(alias));
                    Global.INPUT = regex.Replace(Global.INPUT, Global.ALIASES[alias], 1);
                }
            }

            // BuiltIn commands.
            if (Global.INPUT.ToLower().StartsWith("hostnamectl"))
            {
                running = false;
                BuiltIn.HostnameCTL();
            }

            if (Global.INPUT.StartsWith("ls"))
            {
                running = false;
                BuiltIn.LS();
            }

            if (Global.INPUT.StartsWith("cd"))
            {
                running = false;
                BuiltIn.CD();
            }

            if (Global.INPUT.StartsWith("clear"))
            {
                running = false;
                Console.Clear();
            }

            if (Global.INPUT.StartsWith("alias"))
            {
                running = false;
                BuiltIn.Alias();
            }

            if (Global.INPUT.StartsWith("help"))
            {
                running = false;
                var help = new StringBuilder()
                           .Append(" \n")
                           .Append("Mrmagicpie C# Shell Help!")
                           .Append(" \n")
                           .Append("Work in progress!")
                           .Append(" \n");
                Console.WriteLine(help);
            }

            // For everything else
            // TODO: Create a way to add commands in external classes.
            else
            {
                if (running)
                {
                    var none = new StringBuilder()
                               .Append(" \n")
                               .Append("Unknown command! Use \"help\" to get help!")
                               .Append(" \n");
                    Console.ForegroundColor = ConsoleColor.Red;
                    Console.WriteLine(none);
                }
            }
        }