Example #1
0
        public void menuSelection(string input, bool isBatch)
        {
            string prefix = input.Split(' ')[0];
            string action;

            switch (prefix)
            {
            case "help":
                Console.WriteLine("===================================================================");
                Console.WriteLine("You have selected help. The usable commands are as follows:");
                Console.WriteLine("     help      - You're using it! come on, this is serious business :)");
                Console.WriteLine("     time      - Gives the current system time");
                Console.WriteLine("     date      - Gives you the current system date");
                Console.WriteLine("     dir       - lists files in the current directory");
                Console.WriteLine("     run       - runs the appropriate BATCH file USE <filename>.<bat>");
                Console.WriteLine("     create    - creates the appropriate file USE <filename>.<ext>");
                Console.WriteLine("     mkdir     - creates a new directory inside the current directory");
                Console.WriteLine("     cd        - changes to the specified directory if it exists");
                Console.WriteLine("===================================================================");
                break;

            case "date":
                PrintDate();
                break;

            case "time":
                PrintTime();
                break;

            case "dir":
                PrintDirContents();
                break;

            case "create":
                if (!isBatch)
                {
                    action = input.Split(' ')[1];
                    string filename = action.Split('.')[0];
                    string ext      = action.Split('.')[1];
                    File   newFile  = new File(filename, ext);
                    if (ext == "txt")
                    {
                        string line  = "";
                        int    count = 0;
                        while (line != "save")
                        {
                            Console.Write(count + ". ");
                            line = Console.ReadLine();
                            if (line != "save")
                            {
                                newFile.addLine(line);
                            }
                            count++;
                        }
                    }
                    else if (ext == "bat")
                    {
                        string line  = "";
                        int    count = 0;
                        while (line != "save")
                        {
                            Console.Write(count + ". ");
                            line = Console.ReadLine();
                            if (line != "save")
                            {
                                newFile.addLine(line);
                            }
                            count++;
                        }
                    }
                    documents.Add(newFile);
                }
                else
                {
                    Console.WriteLine("Creating a batch file is not supported inside a batch file..");
                }
                break;

            default:
                string[] var = new string[3];
                if (prefix == "set")
                {
                    action = input.Split(' ')[1];
                    var    = Kernel.command.EvaluateCommand(input, Kernel.variables);
                    if (var[0] != null)
                    {
                        Kernel.variables.AddVar((string)var[0], var[1], Int32.Parse(var[2]));
                    }
                }
                else if (prefix == "out")
                {
                    action = input.Split(' ')[1];
                    object a = Kernel.variables.GetVarValue(action);
                    Console.WriteLine(a);
                    break;
                }
                else if (prefix == "run")
                {
                    action = input.Split(' ')[1];
                    if (action == "all")
                    {
                        List <List <string> > batchList = new List <List <String> >();
                        string[]      batches           = input.Split(' ');
                        string[]      file;
                        List <string> commandList = new List <string>();
                        for (int i = 2; i < batches.Length; i++)
                        {
                            file = batches[i].Split('.');
                            if (file[1] == "bat")
                            {
                                for (int j = 0; j < documents.Count; j++)
                                {
                                    if (documents[j].name == file[0])
                                    {
                                        batchList.Add(documents[j].content);
                                    }
                                }
                            }
                        }
                        int longestList = 0;
                        for (int i = 0; i < batchList.Count; i++)
                        {
                            if (batchList[i].Count > longestList)
                            {
                                longestList = batchList[i].Count;
                            }
                        }

                        for (int i = 0; i < longestList; i++)
                        {
                            for (int j = 0; j < batchList.Count; j++)
                            {
                                if (i < batchList[j].Count)
                                {
                                    commandList.Add(batchList[j][i]);
                                }
                            }
                        }

                        for (int i = 0; i < commandList.Count; i++)
                        {
                            menuSelection(commandList[i], true);
                        }
                    }
                    else
                    {
                        string[] fileslice = action.Split('.');
                        if (fileslice[1] == "bat")
                        {
                            for (int i = 0; i < documents.Count; i++)
                            {
                                if (documents[i].name == fileslice[0])
                                {
                                    List <string> lines = documents[i].content;
                                    for (int j = 0; j < lines.Count; j++)
                                    {
                                        menuSelection(lines[j], true);
                                    }
                                }
                            }
                        }
                    }
                }
                else if (prefix == "cls")
                {
                    Console.WriteLine("\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n");
                }
                else if (prefix == "echo")
                {
                    string[] arr = input.Split(' ');
                    for (int i = 1; i < arr.Length; i++)
                    {
                        Console.Write(arr[i] + " ");
                    }
                    Console.WriteLine();
                }
                else
                {
                    Console.WriteLine("That is an unknown command. Please try again!");
                }
                break;
            }
        }