Have you ever just wanted to output .NET object out to console?
FluentTextTable makes it easy to use a text table that also supports full-width characters!
var users = new[]
{
new User {Id = 1, EnglishName = "Bill Gates", JapaneseName = "ビル・ゲイツ", Birthday = DateTime.Parse("1955/10/28")},
new User {Id = 2, EnglishName = "Steven Jobs", JapaneseName = "スティーブ・ジョブズ", Birthday = DateTime.Parse("1955/2/24")}
};
Build
.TextTable<User>()
.WriteLine(users);
The complex formatting of tables can be changed flexibly and fluently.
Build
.TextTable<User>(builder =>
{
builder
.Borders.Horizontals.AllStylesAs("-")
.Borders.HeaderHorizontal.AllStylesAs("=")
.Columns.Add(x => x.Id).HorizontalAlignmentAs(HorizontalAlignment.Right)
.Columns.Add(x => x.Name).VerticalAlignmentAs(VerticalAlignment.Center)
.Columns.Add(x => x.Birthday).VerticalAlignmentAs(VerticalAlignment.Bottom).FormatAs("{0:yyyy/MM/dd}")
.Columns.Add(x => x.Occupations).FormatAs("- {0}");
})
.WriteLine(users);
Horizontal and vertical alignment, multi-line cells, string formatting, border styles, margins, etc.
And this supports markdowns as well.
Build
.MarkdownTable<User>()
.WriteLine(users);
NET Framework 4.0 (or higher) and .NET Standard 2.0 (or higher) are supported. Install and use it from NuGet.
> Install-Package FluentTextTable
Define the class to be output.
public class User
{
public int Id { get; set; }
public string EnglishName { get; set; }
public string JapaneseName { get; set; }
public DateTime Birthday;
}
Use the Build class to create a table for the output class.
By default, all public properties and fields are included in the output.
var table = Build.TextTable<User>();
Create and output an object corresponding to a row.
var users = new[]
{
new User {Id = 1, EnglishName = "Bill Gates", JapaneseName = "ビル・ゲイツ", Birthday = DateTime.Parse("1955/10/28")},
new User {Id = 2, EnglishName = "Steven Jobs", JapaneseName = "スティーブ・ジョブズ", Birthday = DateTime.Parse("1955/2/24")}
};
Build
.TextTable<User>()
.WriteLine(users);
FluentTextTable supports the popular Markdown format.
Build
.MarkdownTable<User>()
.WriteLine(users);
You can also align to the center or right. See Column format for details.
Supports line breaks in a single cell.
A new line is output in one of the following cases
- The string properties and field containing the line feed code
- Properties and fields defined in IEnumerable, such as Array and List
private class User { public int Id { get; set; } public string Name { get; set; } public DateTime Birthday; public string Parents { get; set; } public string[] Occupations { get; set; } } var users = new[] { new User { Id = 1, Name = "Bill Gates", Birthday = DateTime.Parse("1955/10/28"), Parents = $"Bill Gates Sr.{Environment.NewLine}Mary Maxwell Gates", Occupations = new []{"Software developer", "Investor", "Entrepreneur", "Philanthropist"} } }; var table = Build.TextTable<User>(builder => { builder .Columns.Add(x => x.Id).NameAs("ID").HorizontalAlignmentAs(HorizontalAlignment.Right) .Columns.Add(x => x.Name).VerticalAlignmentAs(VerticalAlignment.Center) .Columns.Add(x => x.Birthday).VerticalAlignmentAs(VerticalAlignment.Bottom).FormatAs("{0:yyyy/MM/dd}") .Columns.Add(x => x.Parents).VerticalAlignmentAs(VerticalAlignment.Center).FormatAs("- {0}") .Columns.Add(x => x.Occupations).HorizontalAlignmentAs(HorizontalAlignment.Center); }); table.WriteLine(users);
See Column Format for details of the format.
In the case of markdown, it is output as a br tag.
var table = Build.MarkdownTable<User>(builder => { builder .Columns.Add(x => x.Id).NameAs("ID").HorizontalAlignmentAs(HorizontalAlignment.Right) .Columns.Add(x => x.Name).VerticalAlignmentAs(VerticalAlignment.Center) .Columns.Add(x => x.Birthday).VerticalAlignmentAs(VerticalAlignment.Bottom).FormatAs("{0:yyyy/MM/dd}") .Columns.Add(x => x.Parents).VerticalAlignmentAs(VerticalAlignment.Center).FormatAs("- {0}") }); table.WriteLine(users);
Here's how it would look like
ID Name Birthday Parents 1 Bill Gates 1955/10/28 - Bill Gates Sr.
- Mary Maxwell GatesIn Markdown, vertical alignment is not enabled.
By default, all public properties and fields are included in the output.
static void Main() { var users = new[] { new User {Id = 1, Name = "ビル ゲイツ", Birthday = DateTime.Parse("1955/10/28")}, new User {Id = 2, Name = "Steven Jobs", Birthday = DateTime.Parse("1955/2/24")} }; Build .TextTable<User>() .WriteLine(users); }
Output columns can also be specified.
Build .TextTable<User>(builder => { builder .Columns.Add(x => x.Name) .Columns.Add(x => x.Birthday); }) .WriteLine(users);
The columns can be formatted as follows
- Horizontal Alignment
- Vertical Alignment
- Header name
- Format
var table = Build.TextTable<User>(builder => { builder .Columns.Add(x => x.Id).NameAs("ID").HorizontalAlignmentAs(HorizontalAlignment.Right) .Columns.Add(x => x.Name).VerticalAlignmentAs(VerticalAlignment.Center) .Columns.Add(x => x.Birthday).VerticalAlignmentAs(VerticalAlignment.Bottom).FormatAs("{0:yyyy/MM/dd}") .Columns.Add(x => x.Parents).VerticalAlignmentAs(VerticalAlignment.Center).FormatAs("- {0}") .Columns.Add(x => x.Occupations).HorizontalAlignmentAs(HorizontalAlignment.Center); }); table.WriteLine(users);
Note that the cells are formatted row by row.
All borders can be changed to any style (Markdown format is not supported).
var table = Build.TextTable<User>(builder => { builder .Borders.Top .LeftStyleAs("-") .IntersectionStyleAs("-") .RightStyleAs("-") .Borders.HeaderHorizontal .LineStyleAs("=") .Borders.InsideHorizontal .AsDisable() .Borders.Bottom .LeftStyleAs("*") .IntersectionStyleAs("*") .RightStyleAs("*"); });
Borders can also be applied collectively as a style.
Build .TextTable<User>(builder => { builder .Borders.Horizontals.AllStylesAs("-") .Borders.InsideHorizontal.AllStylesAs("=") .Borders.Verticals.LineStyleAs("$"); }) .WriteLine(users);
The following areas are defined for the borders
- Top
- HeaderHorizontal
- InsideHorizontal
- Bottom
- Left
- InsideVertical
- Right
The horizontal border can be changed in the following styles
- LeftStyle
- IntersectionStyle
- RightStyle
- LineStyle
The vertical border can only be changed by LineStyle. Intersections are determined by the horizontal border.
You can specify the left and right margins of the table.
Build .TextTable<User>(builder => { builder .Margins.Left.As(4) .Margins.Right.As(2); }) .WriteLine(users);
The right margin is not needed in the console, but is useful for text.
var text = Build .TextTable<User>(builder => { builder .Margins.Left.As(4) .Margins.Right.As(2); }) .ToString(users);
You can set the left and right padding on the inside of the cell to any width.
Build .TextTable<User>(builder => { builder .Paddings.Left.As(4) .Paddings.Right.As(2); }) .WriteLine(users);