SharpTables
1.0.0
一種多功能且可自定義的控制台表格式。生成桌子準備編寫以進行控制,並能夠自定義生成器生成表的字符。
有關示例程序,請參見示例。請記住,並非所有功能都包含在其中。
using SharpTables ;
Formatting f = Formatting . Minimalist ;
object [ , ] dataset = new object [ , ]
{
{ "Name" , "Age" , "City" } ,
{ "John Doe" , 42 , "New York" } ,
{ "Jane Doe" , 36 , "Chicago" } ,
{ "Joe Bloggs" , 25 , "Los Angeles" } ,
{ "Jenny Smith" , 28 , "Miami" }
} ;
Table table = Table . FromDataSet ( dataset ) // Also supports IEnumerable<IEnumerable<T>>
. AddRow ( new Row ( "Jimmy Jones" , null , "Las Vegas" ) ) // Supports nullables and manually adding rows
. UseNullOrEmptyReplacement ( "N/A" )
. UsePreset ( cell => cell . Color = cell . IsNumeric ? ConsoleColor . Yellow : ConsoleColor . White ) ;
table . Print ( ) ;
/*
Name Age City
────────────────────────────────────────────
John Doe 42 New York
Jane Doe 36 Chicago
Joe Bloggs 25 Los Angeles
Jenny Smith 28 Miami
Jimmy Jones N/A Las Vegas
*/Formatting類已經有一些預設,您也可以使用它們with record類型或製作自己的實例,因此可以使用它們進行修改。默認情況下,使用new Formatting()將使用Formatting.Default
// Using the power of records!
Formatting format = Formatting . ASCII with
{
DividerColor = ConsoleColor . DarkGray ,
BottomLeftDivider = '@' ,
BottomRightDivider = '@' ,
TopLeftDivider = '@' ,
TopRightDivider = '@' ,
MiddleDivider = '%' ,
Header = Formatting . ASCII . Header with { Separated = true , }
} ;
/*
+----------------+--------+----------------+
|Name |Age |City |
+----------------+--------+----------------+
@----------------+--------+----------------@
|John Doe |42 |New York |
+----------------%--------%----------------+
|Jane Doe |36 |Chicago |
+----------------%--------%----------------+
|Joe Bloggs |25 |Los Angeles |
+----------------%--------%----------------+
|Jenny Smith |28 |Miami |
+----------------%--------%----------------+
|Jimmy Jones |N/A |Las Vegas |
@----------------+--------+----------------@
*/您還可以使用特定於單元格的格式,可用於更改表格中的單元格。
List < Foo > foos = new List < Foo >
{
new Foo { A = 1 , B = "Hello" , C = true } ,
new Foo { A = 2 , B = "World" , C = false } ,
new Foo { A = 3 , B = "Something" , C = true } ,
} ;
// 'c' represents any cell in the table
table . UsePreset ( c =>
{
if ( bool . TryParse ( c . Text , out bool b ) )
{
c . Text = b ? "V" : "X" ;
c . Alignment = Alignment . Center ;
c . Color = b ? ConsoleColor . Green : ConsoleColor . Red ;
}
if ( int . TryParse ( c . Text , out int i ) )
{
c . Color = ConsoleColor . Yellow ;
c . Padding = 0 ;
c . Alignment = Alignment . Right ;
}
} ) ;
/*
The result is colored on console.
╔═══════════╦══════════════╦════╗
║ Is Active ║ Name ║ ID ║
╠═══════════╬══════════════╬════╣
│ V │Hello │ 1│
├───────────┼──────────────┼────┤
│ X │World │ 2│
├───────────┼──────────────┼────┤
│ V │Something │ 3│
└───────────┴──────────────┴────┘
*/ 如果您發現一個錯誤,請有任何疑問,想以任何其他方式實施自己的添加或做出貢獻,請隨時打開拉動請求!
該項目已根據麻省理工學院許可證