Skip to content

Commit

Permalink
Update TextBuilder.cs
Browse files Browse the repository at this point in the history
  • Loading branch information
Peled-Zohar authored Nov 5, 2019
1 parent 1771bd3 commit 25e0f2d
Showing 1 changed file with 33 additions and 6 deletions.
39 changes: 33 additions & 6 deletions ExtendedConsole/TextBuilder.cs
Original file line number Diff line number Diff line change
@@ -1,25 +1,40 @@
using System;
using System;
using System.Collections.Generic;

namespace ExtendedConsole
{
/// <summary>
/// Builds the text and colors to write to Console.
/// </summary>
internal class TextBuilder
{
private readonly List<Action> _actions;
private ConsoleColor _foreground, _background;

/// <summary>
/// Initialize a new instance of the TextBuilder class.
/// </summary>
internal TextBuilder()
{
_actions = new List<Action>();
_foreground = Console.ForegroundColor;
_background = Console.BackgroundColor;
}

/// <summary>
/// Adds text to write to the console.
/// </summary>
/// <param name="text">Text to write to the console.</param>
internal void AddText(string text)
{
_actions.Add(() => Console.Write(text));
}

/// <summary>
/// Adds a foreground color change, and returns the previous foreground color.
/// </summary>
/// <param name="color">A member of the ConsoleColor enum to set the foreground color to.</param>
/// <returns>A nullable member of the ConsoleColor enum.</returns>
internal ConsoleColor? SetForegroundColor(ConsoleColor color)
{
var returnValue = _foreground;
Expand All @@ -28,6 +43,11 @@ internal void AddText(string text)
return returnValue;
}

/// <summary>
/// Adds a background color change, and returns the previous background color.
/// </summary>
/// <param name="color">A member of the ConsoleColor enum to set the background color to.</param>
/// <returns>A nullable member of the ConsoleColor enum.</returns>
internal ConsoleColor? SetBackgroundColor(ConsoleColor color)
{
var returnValue = _background;
Expand All @@ -36,6 +56,11 @@ internal void AddText(string text)
return returnValue;
}

/// <summary>
/// Adds a foreground / background color change. If an argument is null, it has no effect.
/// </summary>
/// <param name="foreground">A nullable member of the ConsoleColor enum to set as foreground.</param>
/// <param name="background">A nullable member of the ConsoleColor enum to set as background.</param>
internal void ResetColors(ConsoleColor? foreground, ConsoleColor? background)
{
if (foreground.HasValue)
Expand All @@ -50,6 +75,9 @@ internal void ResetColors(ConsoleColor? foreground, ConsoleColor? background)
}
}

/// <summary>
/// Writes the content of the TextBuilder to the Console and clears it.
/// </summary>
internal void Write()
{
foreach (var action in _actions)
Expand All @@ -59,14 +87,13 @@ internal void Write()
_actions.Clear();
}

/// <summary>
/// Writes the content of the TextBuilder to the Console, clears it and adds a new line.
/// </summary>
internal void WriteLine()
{
_actions.Add(() => Console.WriteLine());
foreach (var action in _actions)
{
action();
}
_actions.Clear();
Write();
}

}
Expand Down

0 comments on commit 25e0f2d

Please sign in to comment.