minhhungit / consoletableext Goto Github PK
View Code? Open in Web Editor NEWA fluent library to print out a nicely formatted table in a console application C#
License: MIT License
A fluent library to print out a nicely formatted table in a console application C#
License: MIT License
Can you help me display only those rows that I want to
For example: there is the following code:
public class TestClass1
{
public string A { get; set; }
public string B { get; set; }
public string C { get; set; }
}
List<TestClass1> testClass = new List<TestClass1>();
testClass.Add(new TestClass1 { A = "a0", B = "b0", C = "c0" });
testClass.Add(new TestClass1 { A = "a1", B = "b1", C = "c1" });
ConsoleTableBuilder
.From(testClass)
.WithColumn("A", "B")
.ExportAndWriteLine();
I would like to display only the "A" and "B" columns
How can I do this ?
So the question is: how to display specific columns ?
I've tried a few permutations, but I cant get it to accept a list of the same type objects.
like
public class MyClass
{
public string StringValue1 {get;set;}
public string StringValue2 {get;set;}
public string StringValue3 {get;set;}
}
var t1 = new MyClass {StringValue1 = "1", StringValue2 = "2", StringValue3 = "3"};
var t2 = new MyClass {StringValue1 = "4", StringValue2 = "5", StringValue3 = "6"};
var tList = new List<MyClass>{t1, t2}
ConsoleTableBuilder.From(tList) //<-- wont compile.
The following code returns nothing. Seems similar to the Sample code you have but not sure yet what the problem is with this code.
public class Dog
{
public string Name = "Fido";
public string Breed = "Doberman";
public int Age = 4;
}
List<Dog> dogList = new List<Dog>();
dogList.Add(new Dog()
{ Name = "spike", Breed = "Poodle", Age = 3 });
dogList.Add(new Dog()
{ Name = "george", Breed = "Spaniel", Age = 8 });
dogList.Add(new Dog()
{ Name = "sammy", Breed = "Weimaraner", Age = 13 });
ConsoleTableBuilder
.From(dogList)
.WithFormat(ConsoleTableBuilderFormat.Minimal).ExportAndWriteLine();
I like the minimal format, except I find the solid bar for the headers to be difficult to parse the columns.
DefaultString DefaultInt32 HiddenString
----------------------------------------
Row 1 1 Hidden 1
Row 2 10 Hidden 2
Row 3 100 Hidden 3
So, I thought I would try to make a version that had spaces in it:
.WithHeaderCharMapDefinition(
new Dictionary<HeaderCharMapPositions, char>
{
{ HeaderCharMapPositions.BottomCenter, ' ' },
{ HeaderCharMapPositions.Divider, ' ' },
{ HeaderCharMapPositions.BorderBottom, '-' },
})
But what I found was if both BottomCenter
and Divider
are spaces (' '
), then the space is collapsed between the two of them which squishes the columns together. I ended up needing to put something between the two to make it work:
.WithCharMapDefinition(
new Dictionary<CharMapPositions, char>
{
{ CharMapPositions.DividerY, ' ' },
})
.WithHeaderCharMapDefinition(
new Dictionary<HeaderCharMapPositions, char>
{
{ HeaderCharMapPositions.BottomCenter, '+' },
{ HeaderCharMapPositions.Divider, ' ' },
{ HeaderCharMapPositions.BorderBottom, '-' },
});
This produces a table like this:
DefaultString DefaultInt32 HiddenString
---------------+--------------+--------------
Row 1 1 Hidden 1
Row 2 10 Hidden 2
Row 3 100 Hidden 3
Adding that adds extra columns to the right and left of the headers, which isn't ideal but workable. But, being able to have spaces between the columns would be greatly appreciated. Is there a way of doing that?
Can we implement Colour Support in the tables?
Making use of something like:
Console.ForegroundColor = ConsoleColor.Green
Maybe I missed it inside your code, but is there a way to center the tables inside the console?
Just have a use case for it at the moment...
THX for any comment on this.
Great job with the package BTW, luv it!
Greetings from Belgium,
Guy
I like the MarkDown format but can you add a title bar and a footer option? You can do it as an extension method to your Builder pattern .WithTitle(...)
, .WithFooter(...)
.
ConsoleTableBuilder.From(GenerateResults(results))
.WithFormat(ConsoleTableBuilderFormat.MarkDown)
.ExportAndWriteLine();
========== CONTACTS ==========
| Id | First Name | Surname |
|----------------------------|
| 1 | Isaac | Asimov |
| 2 | Robert | Heinlein |
| 3 | Frank | Herbert |
| 4 | Aldous | Huxley |
==============================
The ConsoleTables has a nice feature, where .Configure(o => o.NumberAlignment = Alignment.Right)
sets up automatic right alignment of numbers in the table.
Would you consider adding similar mechanism to your project?
Sorry, I can't figure out, do ConsoleTableExt have any support of multiple lines in cell? Just now as I see, when I try to use the "first" + Environment.NewLine + "second"
text in cell, the line wraps not inside the cell, but on the new line of whole formatted table output. Is it possible to format table like in https://github.com/seleznevae/libfort? The libfort unfortunately is in C (
Maybe there is a way to do it in ConsoleTableExt?
This code:
public static string GetAsciiTable()
{
var tableData = new List<List<object>>
{
new List<object>{ "Sakura Yamamoto", "Support Engineer", "London", 46},
new List<object>{ "Serge Baldwin", "Data Coordinator", "San Francisco", 28, "something else" },
new List<object>{ "Shad Decker", "Regional Director", "Edinburgh"},
};
return ConsoleTableBuilder.From(tableData)
.WithColumn("AAA BBB", "CCC" + Environment.NewLine + "DDD", "EEE FFF")
.Export().ToString();
}
Results in this output:
And the C libfort library multiline output for example:
There was a bug when int
is replaced by string
//Worked
ConsoleTableBuilder.From(new List<int> { 1, 2, 3, 4, 5, 6 })
.WithFormat(ConsoleTableBuilderFormat.Alternative).WithColumn("I'm a custom name").ExportAndWrite();
//Crashed
ConsoleTableBuilder.From(new List<string> { "1", "2", "3", "4", "5", "6" }).WithFormat(ConsoleTableBuilderFormat.Alternative).WithColumn("I'm a custom name").ExportAndWrite();
Unhandled exception. System.Reflection.TargetParameterCountException: Parameter count mismatch.
at System.Reflection.RuntimeMethodInfo.InvokeArgumentsCheck(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture)
at System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture)
at System.Reflection.RuntimePropertyInfo.GetValue(Object obj, BindingFlags invokeAttr, Binder binder, Object[] index, CultureInfo culture)
at System.Reflection.RuntimePropertyInfo.GetValue(Object obj, Object[] index)
at ConsoleTableExt.ConsoleTableBuilder.From[T](List`1 list)
If multiple rows of data contain UTF8 characters, the output is still misformatted.
I think the final solution should be to override the 'string.format' method to calculate the final number of spaces that should be added in real time.
var results = builder.FormattedRows.Select(row => string.Format(tableRowContentFormatNoUtf8Charas, row.ToArray())).ToList();
change to
string.FormatEx(tableRowContentFormatNoUtf8Charas, row.ToArray()))
Chinese is not supported
Special control characters such as ANSI escape codes are included in calculation of row length (which is determined by string length). This results in table rows appearing larger than they should be.
This is used for the row (outlined); the ANSI escape sequence is red
This results in the following output:
This is because the length of the ANSI escape characters in the example is 7
, which results in 7
additional characters in the row cell:
A possible solution would be using regex to filter such characters and escape sequences from the row content string, and using the resulting string length to determine the row length.
Can you solve it?
As title, the column should be adjusted from the current view length if it's possible, otherwise, define an MaxLength to replace the outer limit by ...
or allowing to break new line should be considered.
Hi, thanks for your job.
On VSC, I tried to install this extension from the NuGet tool console
Install-Package ConsoleTableExt
And I got this:
Error Package restore failed. Rolling back package changes for 'ConsoleTableExt'.
Severity Code Description Project File Line Suppression State
Error NU1108 Cycle detected.
ConsoleTableExt -> ConsoleTableExt (>= 3.2.0). ConsoleTableExt D:.NET FOLDER\C# ACADEMY\Projects\ConsoleTableExt\ConsoleTableExt.csproj 1
Thanks :D!
It could print the table using box-drawing characters.
┌───────┬─────┐
│ Name │ Age │
├───────┼─────┤
│ Alice │ 10 │
│ Bob │ 20 │
│ Clare │ 30 │
└───────┴─────┘
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.