-
Notifications
You must be signed in to change notification settings - Fork 64
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Prettify the results of yuniql list on console
- Loading branch information
1 parent
6fdfd44
commit eb70d8c
Showing
3 changed files
with
90 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
|
||
namespace Yuniql.CLI | ||
{ | ||
//thanks https://stackoverflow.com/users/1547699/sumudu | ||
//https://stackoverflow.com/a/54943087/3449591 | ||
public class TablePrinter | ||
{ | ||
private readonly string[] titles; | ||
private readonly List<int> lengths; | ||
private readonly List<string[]> rows = new List<string[]>(); | ||
|
||
public TablePrinter(params string[] titles) | ||
{ | ||
this.titles = titles; | ||
lengths = titles.Select(t => t.Length).ToList(); | ||
} | ||
|
||
public void AddRow(params object[] row) | ||
{ | ||
if (row.Length != titles.Length) | ||
{ | ||
throw new Exception($"Added row length [{row.Length}] is not equal to title row length [{titles.Length}]"); | ||
} | ||
rows.Add(row.Select(o => o.ToString()).ToArray()); | ||
for (int i = 0; i < titles.Length; i++) | ||
{ | ||
if (rows.Last()[i].Length > lengths[i]) | ||
{ | ||
lengths[i] = rows.Last()[i].Length; | ||
} | ||
} | ||
} | ||
|
||
public void Print() | ||
{ | ||
lengths.ForEach(l => System.Console.Write("+-" + new string('-', l) + '-')); | ||
Console.WriteLine("+"); | ||
|
||
string line = ""; | ||
for (int i = 0; i < titles.Length; i++) | ||
{ | ||
line += "| " + titles[i].PadRight(lengths[i]) + ' '; | ||
} | ||
Console.WriteLine(line + "|"); | ||
|
||
lengths.ForEach(l => System.Console.Write("+-" + new string('-', l) + '-')); | ||
Console.WriteLine("+"); | ||
|
||
foreach (var row in rows) | ||
{ | ||
line = ""; | ||
for (int i = 0; i < row.Length; i++) | ||
{ | ||
if (int.TryParse(row[i], out int n)) | ||
{ | ||
line += "| " + row[i].PadLeft(lengths[i]) + ' '; // numbers are padded to the left | ||
} | ||
else | ||
{ | ||
line += "| " + row[i].PadRight(lengths[i]) + ' '; | ||
} | ||
} | ||
Console.WriteLine(line + "|"); | ||
} | ||
|
||
lengths.ForEach(l => System.Console.Write("+-" + new string('-', l) + '-')); | ||
Console.WriteLine("+"); | ||
Console.WriteLine(); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters