/// <summary> /// Determine if verbosities is satisfied. /// </summary> protected static bool SatisfyVerbosity(IOutput output, Verbosities verbosity) { if (!VerbosityMapping.TryGetValue(verbosity, out OutputOptions options)) { return(false); } return(SatisfyVerbosity(output, options)); }
/// <summary> /// Overwrites a previous message to the output. /// </summary> /// <param name="messages">An array of string.</param> /// <param name="newLine">Whether to add a newline or not.</param> /// <param name="size">The size of line will overwrites.</param> /// <param name="stderr">Whether is stderr output.</param> /// <param name="verbosity">Verbosity level from the verbosity * constants.</param> private void DoOverwrite(string[] messages, bool newLine = false, int size = -1, bool stderr = false, Verbosities verbosity = Verbosities.Normal) { var message = string.Join(newLine ? Environment.NewLine : string.Empty, messages); if (size < 0) { // since overwrite is supposed to overwrite last message. size = AbstractHelper.StrlenWithoutDecoration( Output.Formatter, (stderr && Output is IOutputConsole) ? lastMessageError : lastMessage); } // let's fill its length with backspaces DoWrite(Str.Repeat("\x08", size), false, stderr, verbosity); DoWrite(message, false, stderr, verbosity); // In cmd.exe on Win8.1 (possibly 10?), the line can not // be cleared, so we need to track the length of previous // output and fill it with spaces to make sure the line // is cleared. var fill = size - AbstractHelper.StrlenWithoutDecoration(Output.Formatter, message); if (fill > 0) { DoWrite(Str.Repeat(fill), false, stderr, verbosity); DoWrite(Str.Repeat("\x08", fill), false, stderr, verbosity); } if (newLine) { DoWrite(string.Empty, true, stderr, verbosity); } var output = Output; if (stderr && Output is IOutputConsole consoleOutput) { output = consoleOutput.GetErrorOutput(); } if (!SatisfyVerbosity(output, verbosity)) { return; } if (stderr) { lastMessageError = message; } else { lastMessage = message; } }
/// <summary> /// Writes a message to the output. /// </summary> /// <param name="messages">An array of string.</param> /// <param name="newLine">Whether to add a newline or not.</param> /// <param name="stderr">Whether is stderr output.</param> /// <param name="verbosity">Verbosity level from the verbosity * constants.</param> private void DoWrite(string[] messages, bool newLine = false, bool stderr = false, Verbosities verbosity = Verbosities.Normal) { if (!VerbosityMapping.TryGetValue(verbosity, out OutputOptions options)) { return; } if (stopwatch != null && stopwatch.IsRunning) { var memoryUsage = AbstractHelper.FormatMemory(Environment.WorkingSet); var timeSpent = stopwatch.Elapsed; messages = Arr.Map(messages, (message) => { if (!string.IsNullOrEmpty(message)) { return($"[{memoryUsage}/{timeSpent.TotalSeconds.ToString("0.00")}s] {message}"); } return(message); }); } if (stderr && Output is IOutputConsole consoleOutput) { var errorOutput = consoleOutput.GetErrorOutput(); Array.ForEach(messages, (message) => { errorOutput.Write(message, newLine, options); }); if (SatisfyVerbosity(errorOutput, options)) { lastMessageError = string.Join(newLine ? Environment.NewLine : string.Empty, messages); } return; } Array.ForEach(messages, (message) => { Output.Write(message, newLine, options); }); if (SatisfyVerbosity(Output, options)) { lastMessage = string.Join(newLine ? Environment.NewLine : string.Empty, messages); } }
/// <summary> /// Writes a message to the error output. /// </summary> /// <param name="io">The input/output instance.</param> /// <param name="messages">An array of message.</param> /// <param name="newLine">Whether to add a newline or not.</param> /// <param name="verbosity">Verbosity level from the verbosity * constants.</param> public static void WriteError(this IIO io, IEnumerable <string> messages, bool newLine = true, Verbosities verbosity = Verbosities.Normal) { foreach (var message in messages) { io.WriteError(message, newLine, verbosity); } }
/// <inheritdoc /> public override void OverwriteError(string message, bool newLine = true, int size = -1, Verbosities verbosity = Verbosities.Normal) { // ignore. }
/// <inheritdoc /> public override void Write(string message, bool newLine = true, Verbosities verbosity = Verbosities.Normal) { // ignore. }
/// <summary> /// Writes a message to the output. /// </summary> /// <param name="message">A single string.</param> /// <param name="newLine">Whether to add a newline or not.</param> /// <param name="stderr">Whether is stderr output.</param> /// <param name="verbosity">Verbosity level from the verbosity * constants.</param> private void DoWrite(string message, bool newLine = false, bool stderr = false, Verbosities verbosity = Verbosities.Normal) { DoWrite(new string[] { message }, newLine, stderr, verbosity); }
/// <inheritdoc /> public override void OverwriteError(string message, bool newLine = true, int size = -1, Verbosities verbosity = Verbosities.Normal) { DoOverwrite(new string[] { message }, newLine, size, true, verbosity); }
/// <inheritdoc /> public override void WriteError(string message, bool newLine = true, Verbosities verbosity = Verbosities.Normal) { DoWrite(message, newLine, true, verbosity); }