/// <summary> /// Asks for string. /// </summary> /// <param name="question">The question.</param> /// <param name="defans">The defans.</param> /// <param name="loger">The loger.</param> /// <returns></returns> public static String askForString(String question, String defans, ILogBuilder loger = null) { String output = defans; Console.CursorVisible = true; String helpLine = "[enter] to leave current"; Console.WriteLine(question); Console.WriteLine(helpLine); Console.WriteLine(); String input = Console.ReadLine(); if (loger != null) { loger.AppendSection(helpLine, question, "User response: " + input); } if (String.IsNullOrWhiteSpace(input)) { return(defans); } else { return(input); } Console.CursorVisible = false; return(output); }
/* * /// <summary> * /// Pita za vrednost opcije * /// </summary> * /// <typeparam name="T"></typeparam> * /// <param name="question"></param> * /// <param name="defans"></param> * /// <returns></returns> * public static T askForOption<T>(String question, T defans) where T:class * { * T output = defans; * }*/ /// <summary> /// Asks for string input - in inline mode /// </summary> /// <param name="question">The question.</param> /// <param name="defans">Default answer</param> /// <param name="loger">The loger.</param> /// <returns></returns> public static String askForStringInline(String question, String defans, ILogBuilder loger = null) { Console.WriteLine(); Console.CursorVisible = true; String line = question; if (!String.IsNullOrEmpty(defans)) { line += " (enter for [" + defans + "])"; } line += " : "; Console.Write(line); String output = Console.ReadLine(); if (String.IsNullOrEmpty(output)) { output = defans; } if (loger != null) { loger.AppendSection(line, question, "User response: " + output); } Console.CursorVisible = false; return(output); }
public static Object askForOption(String question, Object defans, IList options, ILogBuilder loger = null) { Object output = defans; String helpLine = "Leave blank for default value (" + defans.ToString() + ")"; Console.WriteLine(question); Console.WriteLine(helpLine); List <String> msgOut = new List <string>(); Int32 c = 0; foreach (String op in options) { String msg = " [" + c.ToString() + "] " + op + ""; if (op == defans.ToString()) { msg += " (default)"; } Console.WriteLine(msg); msgOut.Add(msg); c++; } String input = Console.ReadLine(); input = input.Trim(); if (String.IsNullOrEmpty(input)) { return(defans); } Int32 ninput = 0; if (Int32.TryParse(input, out ninput)) { output = options[ninput]; } else { Console.WriteLine("--- invalid input ---"); return(askForOption(question, defans, options, loger)); } if (loger != null) { loger.AppendSection(helpLine, question, "User response: " + output, msgOut); } return(output); }
/// <summary> /// Postavlja Y/N pitanje korisniku. Ako pritisne neorgovarajuc taster - postavice pitanje ponovo /// </summary> /// <param name="question">The question.</param> /// <param name="defans">if set to <c>true</c> [defans].</param> /// <returns>TRUE on yes</returns> public static Boolean askYesNo(String question, Boolean defans = true, ILogBuilder loger = null) { Boolean output = defans; // = new Boolean(); String helpLine = "[Y] yes [N] no [enter] default"; if (defans) { helpLine += " (default is Y)"; } else { helpLine += " (default is N)"; } Console.WriteLine(question); Console.WriteLine(helpLine); Console.WriteLine(); ConsoleKeyInfo keyInfo = Console.ReadKey(true); Console.WriteLine(keyInfo.Key.ToString()); if (loger != null) { loger.AppendSection(helpLine, question, "User response: " + keyInfo.Key.ToString()); } switch (keyInfo.Key) { case ConsoleKey.Y: return(true); break; case ConsoleKey.Enter: return(defans); break; case ConsoleKey.N: return(false); break; default: return(askYesNo(question, defans)); break; } return(output); }