diff --git a/ExeToBat/BatGen.cs b/ExeToBat/BatGen.cs index dabfbec..f3588a7 100644 --- a/ExeToBat/BatGen.cs +++ b/ExeToBat/BatGen.cs @@ -25,12 +25,12 @@ namespace ExeToBat Console.WriteLine("ExeToBat > Main"); } - void DisplayOption(List Options, string o, int index, int i) + void DisplayEntry(List Options, string o, int index, int i) { Console.WriteLine("[{0}] {1}", i, o); } - bool ChoiceMethod(List Options, int index) + bool HandleEntry(List Options, int index) { switch (Options[index]) { @@ -49,7 +49,7 @@ namespace ExeToBat return false; } - ListToMenu(options, DisplayTitle, DisplayOption, ChoiceMethod, BackString:"Exit"); + ListToMenu(options, HandleEntry, DisplayTitle, DisplayEntry, ExitEntry:"Exit"); } static void ChooseSource() @@ -60,18 +60,18 @@ namespace ExeToBat Console.WriteLine("[{0}] ({1})", Convert.ToString(0).PadLeft(Convert.ToString(sources.Count).Length, ' '), "Add Files"); } - void DisplayOption(List sources, SourceFile source, int index, int i) + void DisplayEntry(List sources, SourceFile source, int index, int i) { Console.WriteLine("[{0}] {1}", Convert.ToString(i).PadLeft(Convert.ToString(sources.Count).Length, ' '), Path.GetFileName(source.File)); } - bool ZeroMethod() + bool ZeroMethod(List sources) { AddSource(); return false; } - bool ChoiceMethod(List sources, int index) + bool HandleEntry(List sources, int index) { ManageSource(sources[index]); return false; @@ -82,7 +82,7 @@ namespace ExeToBat return Sources; } - ListToMenu(Sources, DisplayTitle, DisplayOption, ChoiceMethod, ZeroMethod, UpdateObjects); + ListToMenu(Sources, HandleEntry, DisplayTitle, DisplayEntry, ZeroMethod, UpdateObjects); } static void AddSource() @@ -138,12 +138,12 @@ namespace ExeToBat Console.WriteLine("ExeToBat > Main > Files > {0}", Path.GetFileName(source.File)); } - void DisplayOption(List Options, string o, int index, int i) + void DisplayEntry(List Options, string o, int index, int i) { Console.WriteLine("[{0}] {1}", i, o); } - bool ChoiceMethod(List Options, int index) + bool HandleEntry(List Options, int index) { switch (Options[index]) { @@ -166,7 +166,7 @@ namespace ExeToBat return false; } - ListToMenu(options, DisplayTitle, DisplayOption, ChoiceMethod); + ListToMenu(options, HandleEntry, DisplayTitle, DisplayEntry); } static void ModifySource(SourceFile source) @@ -195,14 +195,12 @@ namespace ExeToBat } int MaxLength = options().Select(x => x.Length).Max(); - void DisplayOption(List Options, string o, int index, int i) + void DisplayEntry(List Options, string o, int index, int i) { Console.WriteLine("[{0}] {1} | {2}", i, o.PadRight(MaxLength, ' '), source.GetType().GetProperty(ValueMap[o]).GetValue(source).ToString()); } - bool ZeroMethod() { ResetInput(); return false; } - - bool ChoiceMethod(List Options, int index) + bool HandleEntry(List Options, int index) { switch (Options[index]) { @@ -237,12 +235,7 @@ namespace ExeToBat return false; } - List UpdateObjects(List Options) - { - return options(); - } - - ListToMenu(options(), DisplayTitle, DisplayOption, ChoiceMethod, ZeroMethod:ZeroMethod, UpdateObjects:UpdateObjects); + ListToMenu(options(), HandleEntry, DisplayTitle, DisplayEntry); } @@ -467,157 +460,170 @@ namespace ExeToBat } - public static void ListToMenu(List Objects, Action> DisplayTitle, Action, T, int, int> DisplayOptions, Func, int, bool> ChoiceMethod, bool UserCanAbort = true, string BackString = "Back") + /// + /// A function that displays a list as an enumerated menu on the Cli. Items can be chosen and will be processed by passed functions. + /// Rest in peace, Cloe. + /// + /// A list of objects that will be displayed as choices. + /// The function that displays the title. It should include displaying the 0th entry if you want to use it. + /// The function that displays an entry. The default function can display strings, for any other objects you will have to pass a custom one. + /// The function that handles the chosen entry. + /// The 0th entry. It is different from the passed list and can be used for example to create new entries. + /// Pass a function that will handle updating the list of objects here. + /// Defines if the user can exit the menu. + /// The string that is displayed for the entry that closes the menu. + public static void ListToMenu(List Entries, Func, int, bool> HandleEntry, Action> DisplayTitle = null, Action, T, int, int> DisplayEntry = null, Func, bool> ZeroEntry = null, Func, List> RefreshEntries = null, bool UserCanAbort = true, string ExitEntry = null) { - ListToMenu(Objects, DisplayTitle, DisplayOptions, ChoiceMethod, () => { ResetInput(); return false; }, (List List) => { return List; }, UserCanAbort, BackString); - } - public static void ListToMenu(List Objects, Action> DisplayTitle, Action, T, int, int> DisplayOptions, Func, int, bool> ChoiceMethod, Func ZeroMethod, Func, List> UpdateObjects, bool UserCanAbort = true, string BackString = "Back") - { - int index = -1; - string InputString = ""; - bool IsMenuExitPending = false; - while (!IsMenuExitPending) + DisplayTitle = DisplayTitle ?? ((List entries) => { }); + DisplayEntry = DisplayEntry ?? ((List entries, T entry, int index_, int num) => { Console.WriteLine("[{0}] {1}", Convert.ToString(num).PadLeft(Convert.ToString(entries.Count).Length, ' '), entry); }); + RefreshEntries = RefreshEntries ?? ((List entries) => { return entries; }); + ZeroEntry = ZeroEntry ?? ((List entries) => { ResetInput(); return false; }); + ExitEntry = ExitEntry ?? "Back"; + + char ExitKey = 'q'; + string Prompt = "Choose"; + + + string readInput = string.Empty; + bool MenuExitIsPending = false; + while (!MenuExitIsPending) { Console.Clear(); int printedEntries = 0; - Objects = UpdateObjects(Objects); - DisplayTitle(Objects); - if (Objects.Any()) + Entries = RefreshEntries(Entries); + DisplayTitle(Entries); + if (Entries.Any()) { - int i = 0; - foreach (T x in Objects) + int num = 0; + foreach (T entry in Entries) { - i++; - if (InputString == "") + num++; + if (string.IsNullOrEmpty(readInput) || Convert.ToString(num).StartsWith(readInput)) { - DisplayOptions(Objects, x, i - 1, i); + DisplayEntry(Entries, entry, Entries.IndexOf(entry), num); printedEntries++; } - else - { - if (Convert.ToString(i).StartsWith(InputString) || Convert.ToString(i) == InputString) - { - DisplayOptions(Objects, x, i - 1, i); - printedEntries++; - } - } - if (Objects.Count > Console.WindowHeight - 5) + if (Entries.Count > Console.WindowHeight - 5) { - if (printedEntries == Console.WindowHeight - 6) + if (printedEntries >= Console.WindowHeight - (5 + 1)) { - Console.WriteLine("[{0}]", ".".PadLeft(Convert.ToString(Objects.Count).Length, '.')); + Console.WriteLine("[{0}] +{1}", ".".PadLeft(Convert.ToString(Entries.Count).Length, '.'), Entries.Count); break; } } - else { if (printedEntries == Console.WindowHeight - 5) { break; } } + else + { + if (printedEntries == Console.WindowHeight - 5) + { + break; + } + } + } } if (UserCanAbort) { - Console.WriteLine("[{0}] {1}", "q".PadLeft(Convert.ToString(Objects.Count).Length, ' '), BackString); + Console.WriteLine("[{0}] {1}", Convert.ToString(ExitKey).PadLeft(Convert.ToString(Entries.Count).Length, ' '), ExitEntry); } - Console.Write("\n"); - bool IsInputValid = false; - while (!IsInputValid) + Console.WriteLine(); + + bool InputIsValid = false; + while (!InputIsValid) { - Console.Write("{0}> {1}", "", InputString); - string input = Console.ReadKey().KeyChar.ToString(); + Console.Write("{0}> {1}", Prompt, readInput); + ConsoleKeyInfo input = Console.ReadKey(); new System.Threading.ManualResetEvent(false).WaitOne(20); + int choiceNum = -1; switch (input) { - case "q": + case var key when key.KeyChar.Equals(ExitKey): if (UserCanAbort) { - Console.Write("\n"); - IsInputValid = true; - IsMenuExitPending = true; + Console.WriteLine(); + InputIsValid = true; + MenuExitIsPending = true; } else { - Console.Write("\n"); + Console.WriteLine(); ResetInput(); } break; - case "\b": - if (!(InputString == "")) + case var key when key.Key.Equals(ConsoleKey.Backspace): + if (!string.IsNullOrEmpty(readInput)) { Console.Write("\b"); - InputString = InputString.Remove(InputString.Length - 1); + readInput = readInput.Remove(readInput.Length - 1); } - IsInputValid = true; + InputIsValid = true; break; - case "\n": - case "\r": - if (InputString != "") + case var key when key.Key.Equals(ConsoleKey.Enter): + if (!string.IsNullOrEmpty(readInput)) { - index = Convert.ToInt32(InputString) - 1; - InputString = ""; - IsInputValid = true; - if (ChoiceMethod(Objects, index)) + if (HandleEntry(Entries, (Convert.ToInt32(readInput) - 1))) { - IsMenuExitPending = true; + MenuExitIsPending = true; + } + readInput = string.Empty; + } + InputIsValid = true; + break; + + case var key when int.TryParse(key.KeyChar.ToString(), out choiceNum): + Console.WriteLine(); + if (string.IsNullOrEmpty(readInput) && choiceNum.Equals(0)) + { + InputIsValid = true; + if (ZeroEntry(Entries)) + { + MenuExitIsPending = true; + } + } + else + { + if (Convert.ToInt32(readInput + Convert.ToString(choiceNum)) <= Entries.Count) + { + InputIsValid = true; + int matchingEntries = 0; + readInput = readInput + Convert.ToString(choiceNum); + for (int i = 0; i < Entries.Count; i++) + { + if (Convert.ToString(i + 1).StartsWith(readInput) || Convert.ToString(i + 1) == readInput) { matchingEntries++; } + } + if ((readInput.Length == Convert.ToString(Entries.Count).Length) || (matchingEntries == 1)) + { + if (HandleEntry(Entries, (Convert.ToInt32(readInput) - 1))) + { + MenuExitIsPending = true; + } + readInput = string.Empty; + } + } + else + { + ResetInput(); } } break; default: - Console.Write("\n"); - int choice; - if ((int.TryParse(input, out choice))) - { - if ((InputString == "") && (choice == 0)) - { - IsInputValid = true; - if (ZeroMethod()) - { - IsMenuExitPending = true; - } - } - else - { - if (Convert.ToInt32(InputString + Convert.ToString(choice)) <= Objects.Count) - { - int MatchingItems = 0; - InputString = InputString + Convert.ToString(choice); - for (int i = 0; i < Objects.Count; i++) { if (Convert.ToString(i + 1).StartsWith(InputString) || Convert.ToString(i + 1) == InputString) { MatchingItems++; } } - if ((InputString.Length == Convert.ToString(Objects.Count).Length) || (MatchingItems == 1)) - { - index = Convert.ToInt32(InputString) - 1; - InputString = ""; - IsInputValid = true; - if (ChoiceMethod(Objects, index)) - { - IsMenuExitPending = true; - } - } - else - { - IsInputValid = true; - } - } - else - { - ResetInput(); - } - } - } - else - { - ResetInput(); - } + Console.WriteLine(); + ResetInput(); break; } } + } - Console.Clear(); } + + public static void YesNoMenu(string title, Action Yes, Action No) { bool IsInputValid = false;