Пример #1
0
        /// <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);
        }
Пример #2
0
        /// <summary>
        /// Writes the comobined string representation of all <see cref="Prompt"/>
        /// elements contained in the <see cref="SmartConsole.Prompts"/> collection.
        /// </summary>
        /// <param name="console">The used <see cref="SmartConsole"/> instance.</param>
        /// <param name="stream">A writable <see cref="Stream"/> to write to.</param>
        /// <param name="encoding">The encoding to use. If null, <see cref="Encoding.UTF8"/> will be used.</param>
        /// <returns></returns>
        public static SmartConsole WriteTo(this SmartConsole console, Stream stream, Encoding encoding = null)
        {
            if (stream == null)
            {
                throw new ArgumentNullException(nameof(stream));
            }
            if (!stream.CanWrite)
            {
                throw new InvalidOperationException("Cannot write to the provided stream.");
            }

            if (encoding == null)
            {
                encoding = Encoding.UTF8;
            }

            var bytes = encoding.GetBytes(console.Prompts.AsString());

            stream.Write(bytes, 0, bytes.Length);
            return(console);
        }
Пример #3
0
 /// <summary>
 /// 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="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, Action <T> action,
                                                           Action <TException> onError = null,
                                                           Func <T, bool> validator    = null,
                                                           string validationMessage    = null) where TException : Exception
 {
     try
     {
         return(console.SetResponse(action, validator, validationMessage));
     }
     catch (Exception ex)
     {
         if (ex is TException error)
         {
             onError?.Invoke(error);
             return(console);
         }
         else
         {
             throw;
         }
     }
 }
Пример #4
0
 /// <summary>
 /// Invokes the specified delegate function and returns its result.
 /// </summary>
 /// <typeparam name="T">The delegate's return type.</typeparam>
 /// <param name="_">The used <see cref="SmartConsole"/>. Is not used.</param>
 /// <param name="func">The delegate to invoke.</param>
 /// <returns><typeparamref name="T"/> which represents the result of the delegate <paramref name="func"/>.</returns>
 public static T Result <T>(this SmartConsole _, Func <T> func) => func.Invoke();
Пример #5
0
 /// <summary>
 /// Disposes off all timers previously created with either of the methods
 /// <see cref="SetTimeout(SmartConsole, Action{TimerEventArgs}, double, string)"/>
 /// and <see cref="SetInterval(SmartConsole, Action{TimerEventArgs}, double, string)"/>.
 /// </summary>
 /// <param name="console">The used <see cref="SmartConsole"/>.</param>
 /// <returns>A reference to the current <see cref="SmartConsole" /> instance.</returns>
 public static SmartConsole ClearTimers(this SmartConsole console)
 {
     TimerManager.Clear();
     return(console);
 }
Пример #6
0
 /// <summary>
 /// Disposes off a timer previously created with the method
 /// <see cref="SetTimeout(SmartConsole, Action{TimerEventArgs}, double, string)"/>.
 /// </summary>
 /// <param name="console">The used <see cref="SmartConsole"/>.</param>
 /// <param name="name">The name of the associated timer to dispose.</param>
 /// <returns>A reference to the current <see cref="SmartConsole" /> instance.</returns>
 public static SmartConsole ClearTimeout(this SmartConsole console, string name)
 {
     TimerManager.Remove(name);
     return(console);
 }
Пример #7
0
 /// <summary>
 /// Creates a timer that executes the specified <paramref name="callback"/>
 /// once after the delay specified by <paramref name="millisecondsDelay"/>.
 /// </summary>
 /// <param name="console">The used <see cref="SmartConsole"/>.</param>
 /// <param name="callback">The action to invoke on each timer tick.</param>
 /// <param name="millisecondsDelay">The number of milliseconds to wait
 /// before calling the callback.</param>
 /// <param name="name">The name of the associated timer. Useful when
 /// calling <see cref="ClearTimeout(SmartConsole, string)"/>.</param>
 /// <returns>A reference to the current <see cref="SmartConsole" /> instance.</returns>
 public static SmartConsole SetTimeout(this SmartConsole console, Action <TimerEventArgs> callback, double millisecondsDelay, string name = null)
 {
     TimerManager.Add(console, callback, millisecondsDelay, name, repeat: false);
     return(console);
 }
Пример #8
0
 /// <summary>
 /// Writes out all collected prompts with their respective responses to
 /// a file. If the target file already exists, it is overwritten.
 /// </summary>
 /// <param name="console">The used <see cref="SmartConsole"/> instance.</param>
 /// <param name="path">The file to write to.</param>
 /// <param name="encoding">The encoding to apply to the string. Can be
 /// null, which then resolves to <see cref="Encoding.UTF8"/>.</param>
 /// <returns>A reference to the specified <see cref="SmartConsole" /> instance.</returns>
 public static SmartConsole WriteFile(this SmartConsole console, string path, Encoding encoding = null)
 {
     File.WriteAllText(path, console.Prompts.AsString(), encoding ?? Encoding.UTF8);
     return(console);
 }
Пример #9
0
 /// <summary>
 /// Writes out a lspecified Unicode character repeated a specified
 /// number of times, and appends a line terminator.
 /// </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 RepeatLine(this SmartConsole console, char c, int count)
 => console.Repeat(c, count).WriteLine();
Пример #10
0
 /// <summary>
 /// Writes out a formatted, colored object and a new line, using the
 /// same semantics as <see cref="string.Format(string, object[])"/>.
 /// </summary>
 /// <param name="console">The used <see cref="SmartConsole"/>.</param>
 /// <param name="message">The message to write.</param>
 /// <param name="color">The <see cref="ConsoleColor"/> to use.</param>
 /// <returns>A reference to the current <see cref="SmartConsole" /> instance.</returns>
 public static SmartConsole WriteLine(this SmartConsole console, object message, ConsoleColor color)
 => console.SetForegroundColor(color).WriteLine(message).RestoreForegroundColor();
Пример #11
0
 /// <summary>
 /// Attempts to remove an instance of <see cref="IFormatProvider"/>
 /// of type <typeparamref name="T"/> from the dictionary.
 /// </summary>
 /// <typeparam name="T">The conversion type.</typeparam>
 /// <returns>A reference to the current <see cref="SmartConsole" /> instance.</returns>
 public static SmartConsole RemoveFormatProvider <T>(this SmartConsole console)
 {
     console.Formatters.TryRemove(typeof(T), out _);
     return(console);
 }
Пример #12
0
 /// <summary>
 /// Adds a format provider for the type <typeparamref name="T"/> used
 /// when converting a user input.
 /// </summary>
 /// <typeparam name="T">The conversion type.</typeparam>
 /// <param name="console">The used <see cref="SmartConsole"/>.</param>
 /// <param name="provider">An object that supplies culture-specific formatting information.</param>
 /// <returns>A reference to the current <see cref="SmartConsole" /> instance.</returns>
 public static SmartConsole AddFormatProvider <T>(this SmartConsole console, IFormatProvider provider)
 {
     console.Formatters.TryAdd(typeof(T), provider);
     return(console);
 }
Пример #13
0
 /// <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);
Пример #14
0
 /// <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);
Пример #15
0
 /// <summary>
 /// Collects user input as a string and passes it to the specified
 /// <paramref name="action"/>.
 /// </summary>
 /// <param name="console">The used <see cref="SmartConsole"/>.</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(this SmartConsole console, Action <string> action,
                                        Func <string, bool> validator = null, string validationMessage = null)
 => console.SetResponse(action, validator, validationMessage);
Пример #16
0
 /// <summary>
 /// Writes out an object's string representation in the system's
 /// <see cref="Console"/> using the specified <paramref name="color"/>.
 /// </summary>
 /// <param name="console">The used <see cref="SmartConsole"/>.</param>
 /// <param name="obj">The object to write.</param>
 /// <param name="color">The <see cref="ConsoleColor"/> to use.</param>
 /// <returns>A reference to the current <see cref="SmartConsole" /> instance.</returns>
 public static SmartConsole Write(this SmartConsole console, object obj, ConsoleColor color)
 => console.SetForegroundColor(color).Write(obj).RestoreForegroundColor();
Пример #17
0
 /// <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)));
 }
Пример #18
0
 /// <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);
Пример #19
0
 /// <summary>
 /// Writes out a formatted, colored string and a new line, using the
 /// same semantics as <see cref="string.Format(string, object[])"/>.
 /// </summary>
 /// <param name="console">The used <see cref="SmartConsole"/>.</param>
 /// <param name="color">The <see cref="ConsoleColor"/> to use.</param>
 /// <param name="format">A composite format string.</param>
 /// <param name="args">
 /// An object array that contains zero or more objects to format and write.
 /// </param>
 /// <returns>A reference to the current <see cref="SmartConsole" /> instance.</returns>
 public static SmartConsole WriteLine(this SmartConsole console, ConsoleColor color, string format, params object[] args)
 => console.SetForegroundColor(color).WriteLine(format, args).RestoreForegroundColor();
Пример #20
0
 /// <summary>
 /// Invokes the specified <paramref name="action"/> and returns a reference
 /// to the current <see cref="SmartConsole"/> instance.
 /// </summary>
 /// <param name="console">The used <see cref="SmartConsole"/>.</param>
 /// <param name="action">The delegate to invoke.</param>
 /// <returns>A reference to the current <see cref="SmartConsole" /> instance.</returns>
 public static SmartConsole Then(this SmartConsole console, Action <SmartConsole> action)
 {
     action.Invoke(console);
     return(console);
 }
Пример #21
0
 /// <summary>
 /// Creates a timer that executes the specified <paramref name="callback"/>
 /// at a regular interval specified by <paramref name="millisecondsInterval"/>.
 /// </summary>
 /// <param name="console">The used <see cref="SmartConsole"/>.</param>
 /// <param name="callback">The action to invoke on each timer tick.</param>
 /// <param name="millisecondsInterval">The number of milliseconds that
 /// should elapse between two consecutive ticks.</param>
 /// <param name="name">The name of the associated timer. Useful when
 /// calling <see cref="ClearInterval(SmartConsole, string)"/>.</param>
 /// <returns>A reference to the current <see cref="SmartConsole" /> instance.</returns>
 public static SmartConsole SetInterval(this SmartConsole console, Action <TimerEventArgs> callback, double millisecondsInterval, string name = null)
 {
     TimerManager.Add(console, callback, millisecondsInterval, name, repeat: true);
     return(console);
 }
Пример #22
0
 /// <summary>
 /// Writes out a line using a specified formatted Unicode string
 /// repeated a specified number of times.
 /// </summary>
 /// <param name="console">The used <see cref="SmartConsole"/>.</param>
 /// <param name="count">The number of times the formatted string occurs.</param>
 /// <param name="format">A composite format string.</param>
 /// <param name="args">An object array that contains zero or more objects to format and write.</param>
 /// <returns>A reference to the current <see cref="SmartConsole" /> instance.</returns>
 public static SmartConsole Repeat(this SmartConsole console, int count, string format, params object[] args)
 {
     return(console.Repeat(string.Format(format, args), count));
 }