/// <summary> /// Reads masked keystrokes from the system's <see cref="Console"/>. /// </summary> /// <param name="console">The used <see cref="SmartConsole"/> instance.</param> /// <param name="message">The optional prompt message to display.</param> /// <param name="useMask">true to write out an asterisk on every keystroke, otherwise, false.</param> /// <returns></returns> public static string GetSecureInput(this SmartConsole console, string message = null, bool useMask = false) { if (!string.IsNullOrWhiteSpace(message)) { console.Write(message); } return(ReadSecureString(useMask)); }
/// <summary> /// Writes out a line using a specified Unicode character repeated a /// specified number of times. /// </summary> /// <param name="console">The used <see cref="SmartConsole"/>.</param> /// <param name="s">A series of Unicode characters to repeat.</param> /// <param name="count">The number of times <paramref name="s"/> occurs.</param> /// <returns>A reference to the current <see cref="SmartConsole" /> instance.</returns> /// <exception cref="ArgumentOutOfRangeException"><paramref name="count"/> is less than zero.</exception> public static SmartConsole Repeat(this SmartConsole console, string s, int count) { if (count < 0) { throw new ArgumentOutOfRangeException(nameof(count), $"{nameof(count)} must be greater than or equal to 0."); } var sb = new StringBuilder(); for (int i = 0; i < count; i++) { sb.Append(s); } console.Write(sb.ToString()); sb.Clear(); return(console); }
/// <summary> /// Writes out a specified Unicode character repeated a specified /// number of times. /// </summary> /// <param name="console">The used <see cref="SmartConsole"/>.</param> /// <param name="c">A Unicode character.</param> /// <param name="count">The number of times <paramref name="c"/> occurs.</param> /// <returns>A reference to the current <see cref="SmartConsole" /> instance.</returns> /// <exception cref="ArgumentOutOfRangeException"><paramref name="count"/> is less than zero.</exception> public static SmartConsole Repeat(this SmartConsole console, char c, int count) { return(console.Write(new string(c, count))); }
/// <summary> /// Writes out a message, attempts to collect user input as a strongly-typed /// value, and passes it to the specified <paramref name="action"/>, /// or reports an error of type <typeparamref name="TException"/> if /// the method fails. Other exception types are rethrown. /// </summary> /// <typeparam name="T">The conversion type.</typeparam> /// <typeparam name="TException">The type of exception to handle.</typeparam> /// <param name="console">The used <see cref="SmartConsole"/>.</param> /// <param name="message">The message to write.</param> /// <param name="action">The action that the retrieved value is passed to.</param> /// <param name="onError">A callback delegate to invoke when an exception is catched.</param> /// <param name="validator">A function that further restricts or validates user input.</param> /// <param name="validationMessage">A message to display if the user enters an invalid response.</param> /// <returns>A reference to the current <see cref="SmartConsole" /> instance.</returns> public static SmartConsole TrySetResponse <T, TException>(this SmartConsole console, string message, Action <T> action, Action <TException> onError = null, Func <T, bool> validator = null, string validationMessage = null) where TException : Exception => console.Write(message).TrySetResponse(action, onError, validator, validationMessage);
/// <summary> /// Writes out a message, attempts to collect user input as a string /// value, and passes it to the specified <paramref name="action"/>, /// or reports an error if the method fails. /// </summary> /// <param name="console">The used <see cref="SmartConsole"/>.</param> /// <param name="message">The message to write.</param> /// <param name="action">The action that the retrieved value is passed to.</param> /// <param name="validator">A function that further restricts or validates user input.</param> /// <param name="validationMessage">A message to display if the user enters an invalid response.</param> /// <param name="onError">A callback delegate to invoke when an exception is catched.</param> /// <returns>A reference to the current <see cref="SmartConsole" /> instance.</returns> public static SmartConsole TrySetResponse(this SmartConsole console, string message, Action <string> action, Func <string, bool> validator = null, string validationMessage = null, Action <Exception> onError = null) => console.Write(message).TrySetResponse(action, validator, validationMessage, onError);
/// <summary> /// Writes out a message, collects user input as a strongly-typed /// value and passes it to the specified <paramref name="action"/>. /// </summary> /// <typeparam name="T">The conversion type.</typeparam> /// <param name="console">The used <see cref="SmartConsole"/>.</param> /// <param name="message">The message to write.</param> /// <param name="action">The action that the retrieved value is passed to.</param> /// <param name="validator">A function that further restricts or validates user input.</param> /// <param name="validationMessage">A message to display if the user enters an invalid response.</param> /// <returns>A reference to the current <see cref="SmartConsole" /> instance.</returns> public static SmartConsole SetResponse <T>(this SmartConsole console, string message, Action <T> action, Func <T, bool> validator = null, string validationMessage = null) => console.Write(message).SetResponse(action, validator, validationMessage);