// Returns whether or not the number of parameters entered is valid for a specific usage. If it isn't, the usage // is printed private static bool VerifyUsage(string[] entry, string[] usage) { if (entry.Length != usage.Length) { CrammerIO.WriteGutter("&rImproper syntax; try " + string.Join(" ", usage)); return(false); } return(true); }
// Takes command entries in array form, where the first index is the command keyword and the following indices // are any parameters public static void Enter(string[] entry) { try { if (!VerifyUsage(entry, commandDirectory[entry[0]].usage)) { return; } } catch (KeyNotFoundException) { CrammerIO.WriteGutter("&rCommand not recognized."); return; } // Scratch references used in multiple cases Studyset sourceSet; string input = ""; bool success; switch (entry[0]) { // ? - help command case "?": foreach (string str in commandDirectory.Keys.ToList()) { CrammerIO.Write("&y" + string.Join(" ", commandDirectory[str].usage) + " - " + commandDirectory[str].help + "\n"); } CrammerIO.Write("\n"); break; // new - create a new studyset case "new": // Verify new studyset name availability success = true; foreach (Studyset set in CrammerDB.studysets) { if (set.name == entry[1]) { success = false; CrammerIO.WriteGutter("&rThat name is already in use!"); break; } } if (!success) { break; } // Add terms to new studyset Studyset newSet = new Studyset(entry[1]); CrammerIO.WriteGutter("&c" + entry[1] + " studyset created.&w Now it needs some contents.\n" + "&yAdd notecards by entering \"<term> - <definition>\". Enter \"done\" when finished."); do { input = Console.ReadLine(); if (input.ToLower() != "done") { // Enforce syntax if (!input.Contains("-")) { CrammerIO.Write("&rImproper syntax; try <term> - <definition>\n"); continue; } // Syntax was OK--add new notecard string term = input.Substring(0, input.IndexOf("-")).Trim(); string def = input.Substring(input.IndexOf("-") + 1, input.Length - input.IndexOf("-") - 1).Trim(); newSet.AddTerm(term, def); CrammerIO.Write("&cAdded " + term + ".\n"); } } while (input.ToLower() != "done"); CrammerDB.AddStudyset(newSet); CrammerIO.SaveStudysets(); CrammerIO.WriteGutter("\n&c" + newSet.GetLength() + " notecards added to " + newSet.name + "."); break; // study - study terms from a studyset case "study": // Verify that the studyset specified for studying exists sourceSet = null; success = false; foreach (Studyset set in CrammerDB.studysets) { if (set.name == entry[1]) { sourceSet = set; success = true; break; } } if (!success) { CrammerIO.WriteGutter("&rStudyset not found."); break; } // Verify that the specified studyset has contents if (sourceSet.GetLength() == 0) { CrammerIO.WriteGutter("&rThat studyset has no notecards."); break; } // Begin studying Console.Clear(); CrammerIO.WriteGutter("&cOpened " + entry[1] + " for studying.\n" + "&yEnter \"done\" when finished."); List <Notecard> pool = sourceSet.CloneTerms(); do { if (input.ToLower() != "done") { // Display a random notecard and remove it from the pool Notecard note = pool[new Random().Next(pool.Count)]; pool.Remove(note); CrammerIO.Write("&c" + note.term + " "); // If the pool is empty, refill it if (pool.Count == 0) { pool = sourceSet.CloneTerms(); } // Wait for user to continue input = Console.ReadLine(); // Display the answer if (input.ToLower() != "done") { string defCol = input == note.definition ? "l" : "w"; CrammerIO.Write(" &l> &" + defCol + note.definition + "\n"); } } } while (input != "done"); CrammerIO.Write("\n"); break; // edit - edit a studyset case "edit": // Verify that the specified studyset exists sourceSet = null; success = false; foreach (Studyset set in CrammerDB.studysets) { if (set.name == entry[1]) { sourceSet = set; success = true; break; } } if (!success) { CrammerIO.WriteGutter("&rStudyset not found."); break; } // Enter studyset editor CrammerIO.WriteGutter("&cOpened " + entry[1] + " for editing.\n" + "&yEnter \"add <term> - <definition>\" to add a notecard, or \"remove <term>\" to delete one. Return with \"done\"."); do { input = Console.ReadLine(); if (input != "done") { string[] inputEntry = Regex.Split(input.TrimEnd('\r', '\n'), @"\s+"); // Add notecard to pre-existing studyset if (inputEntry[0] == "add") { // Enforce syntax if (inputEntry.Length < 4) { CrammerIO.Write("&rImproper syntax; try add <term> - <definition>\n"); continue; } // Syntax was OK -- add new notecard string flatParing = ""; for (int i = 1; i < inputEntry.Length; i++) { flatParing += inputEntry[i] + " "; } string newTerm = flatParing.Substring(0, flatParing.IndexOf("-")).Trim(); string newDef = flatParing.Substring(flatParing.IndexOf("-") + 1, flatParing.Length - flatParing.IndexOf("-") - 1).Trim(); sourceSet.AddTerm(newTerm, newDef); CrammerIO.Write("&cAdded " + newTerm + ".\n"); // Remove notecard from pre-existing studyset } else if (inputEntry[0] == "remove") { // Enforce syntax if (inputEntry.Length < 2) { CrammerIO.Write("&rImproper syntax; try remove <term>\n"); continue; } // Verify that the specified notecard exists success = false; string flatName = ""; for (int i = 1; i < inputEntry.Length; i++) { flatName += inputEntry[i] + " "; } flatName = flatName.Trim(); foreach (Notecard n in sourceSet.GetTerms()) { if (n.term == flatName) { success = true; break; } } if (!success) { CrammerIO.Write("&rNotecard not found.\n"); continue; } // Remove specified notecard from the list sourceSet.RemoveTerm(flatName); CrammerIO.Write("&rRemoved " + flatName + ".\n"); // Studyset editor command not found } else { CrammerIO.Write("&rCommand not found.\n"); } } } while (input != "done"); CrammerIO.SaveStudysets(); CrammerIO.WriteGutter("\n&cChanges saved."); break; // list - view a studyset case "list": // Verify that the specified studyset exists sourceSet = null; success = false; foreach (Studyset set in CrammerDB.studysets) { if (set.name == entry[1]) { sourceSet = set; success = true; break; } } if (!success) { CrammerIO.WriteGutter("&rStudyset not found."); break; } // Verify that the specified studyset for listing has contents if (sourceSet.GetLength() == 0) { CrammerIO.WriteGutter("&rThat studyset has no notecards."); break; } // List notecards foreach (Notecard n in sourceSet.GetTerms()) { CrammerIO.Write("&b" + n.term + "&c - &w" + n.definition + "\n"); } CrammerIO.Write("\n"); break; // sets - list all studyset names case "sets": // Verify that there are any studysets if (CrammerDB.studysets.Count == 0) { CrammerIO.WriteGutter("&rYou have no studysets."); break; } // Print all studyset names foreach (Studyset set in CrammerDB.studysets) { CrammerIO.Write("&c" + set.name + "\n"); } CrammerIO.Write("\n"); break; } }