Skip to content

Commit

Permalink
Merge pull request #38 from jonfuller/customizable_output_stream
Browse files Browse the repository at this point in the history
Adds option for setting the output TextWriter
  • Loading branch information
khalidabuhakmeh authored Dec 17, 2019
2 parents 1e9ad1e + 27ea990 commit 43d2631
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 4 deletions.
33 changes: 33 additions & 0 deletions ConsoleTables.Tests/ConsoleTableTest.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
using System;
using System.Collections.Generic;
using System.IO;
using Xunit;

namespace ConsoleTables.Tests
Expand Down Expand Up @@ -86,6 +88,37 @@ public void NumberShouldBeRightAlignedOnMarkDown()
", table);
}

[Fact]
public void OutputShouldDefaultToConsoleOut()
{
var users = new List<User>
{
new User { Name = "Alexandre" , Age = 36 }
};

var table = ConsoleTable.From(users);

Assert.Equal(table.Options.OutputTo, Console.Out);
}

[Fact]
public void OutputShouldGoToConfiguredOutputWriter()
{
var users = new List<User>
{
new User { Name = "Alexandre" , Age = 36 }
};

var testWriter = new StringWriter();

ConsoleTable
.From(users)
.Configure(o => o.OutputTo = testWriter)
.Write();

Assert.NotEmpty(testWriter.ToString());
}

class User
{
public string Name { get; set; }
Expand Down
14 changes: 10 additions & 4 deletions src/ConsoleTables/ConsoleTable.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text;
Expand Down Expand Up @@ -241,16 +242,16 @@ public void Write(Format format = ConsoleTables.Format.Default)
switch (format)
{
case ConsoleTables.Format.Default:
Console.WriteLine(ToString());
Options.OutputTo.WriteLine(ToString());
break;
case ConsoleTables.Format.MarkDown:
Console.WriteLine(ToMarkDownString());
Options.OutputTo.WriteLine(ToMarkDownString());
break;
case ConsoleTables.Format.Alternative:
Console.WriteLine(ToStringAlternative());
Options.OutputTo.WriteLine(ToStringAlternative());
break;
case ConsoleTables.Format.Minimal:
Console.WriteLine(ToMinimalString());
Options.OutputTo.WriteLine(ToMinimalString());
break;
default:
throw new ArgumentOutOfRangeException(nameof(format), format, null);
Expand Down Expand Up @@ -282,6 +283,11 @@ public class ConsoleTableOptions
/// Enable only from a list of objects
/// </summary>
public Alignment NumberAlignment { get; set; } = Alignment.Left;

/// <summary>
/// The <see cref="TextWriter"/> to write to. Defaults to <see cref="Console.Out"/>.
/// </summary>
public TextWriter OutputTo { get; set; } = Console.Out;
}

public enum Format
Expand Down

0 comments on commit 43d2631

Please sign in to comment.