/* Get the filename to use for a new wallet */ private static string GetNewWalletName() { while (true) { YellowMsg.Write( "What filename would you like to give your new wallet?: " ); string filename = Console.ReadLine(); string appended = filename + ".wallet"; if (string.IsNullOrWhiteSpace(filename)) { RedMsg.WriteLine( "Wallet name cannot be empty! Try again.\n" ); continue; } if (GeneralUtilities.FilenameInUse(filename) || GeneralUtilities.FilenameInUse(appended)) { RedMsg.Write("A file with the name "); YellowMsg.Write(filename); RedMsg.Write(" or "); YellowMsg.Write(appended); RedMsg.WriteLine(" already exists! Try again.\n"); } /* If the file already ends with .wallet, return it. */ else if (filename.EndsWith(".wallet")) { return(filename); } /* Else, append .wallet to the filename */ else { return(appended); } } }
/* Get the filename of an already existing wallet */ private static string GetExistingWalletName() { while (true) { YellowMsg.Write( "What wallet filename would you like to open?: " ); string filename = Console.ReadLine(); string appended = filename + ".wallet"; if (string.IsNullOrWhiteSpace(filename)) { RedMsg.WriteLine("Wallet name cannot be empty! Try again."); continue; } if (File.Exists(filename)) { return(filename); } /* Automatically add the .wallet extension for users */ else if (File.Exists(appended)) { return(appended); } else { RedMsg.Write("A file with the name "); YellowMsg.Write(filename); RedMsg.Write(" or "); YellowMsg.Write(appended); RedMsg.WriteLine(" doesn't exist!"); Console.WriteLine( "Ensure you entered your wallet name correctly.\n" ); } } }
/* printableCommands = the commands to print on bad input. * availableCommands = the commands that the inputted string is * checked against. * * For example, we print basic commands, but can input both basic * and advanced commands */ private static string ParseCommand( IEnumerable <Command> printableCommands, IEnumerable <Command> availableCommands, string prompt) { string selection = null; while (true) { /* Write the prompt message */ YellowMsg.Write(prompt); selection = Console.ReadLine().ToLower(); /* \n == no-op */ if (selection == "") { continue; } /* Check if they entered a command or an number */ if (int.TryParse(selection, out int selectionNum)) { /* We print 1, 2, 3, 4 etc to be user friendly but we want * to access it with 0 indexing */ selectionNum--; if (selectionNum < 0 || selectionNum >= availableCommands.Count()) { RedMsg.Write("Bad input, expected a command " + "name, or number from "); YellowMsg.Write("1 "); RedMsg.Write("to "); YellowMsg.WriteLine(availableCommands.Count().ToString()); PrintCommands(printableCommands); continue; } /* Set the selection to the command name chosen via * the previously printed numbers */ selection = availableCommands.ElementAt(selectionNum) .commandName; } else { /* Does the inputted command exist? */ if (!availableCommands.Any(c => c.commandName == selection)) { Console.Write("Unknown command: "); RedMsg.Write($"{selection}\n"); PrintCommands(printableCommands); continue; } } return(selection); } }