forked from coverlet-coverage/coverlet
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix strong name validation failure in ConsoleTables
For now, ConsoleTables is referenced as a source copy. It can be replaced by a PackageReference as soon as the original adopts a strong name. See khalidabuhakmeh/ConsoleTables#30
- Loading branch information
Showing
4 changed files
with
308 additions
and
2 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
coverlet uses third-party libraries or other resources that may be | ||
distributed under licenses different than the coverlet software. | ||
|
||
In the event that we accidentally failed to list a required notice, please | ||
bring it to our attention by posting an issue. | ||
|
||
The attached notices are provided for information only. | ||
|
||
|
||
License notice for ConsoleTables | ||
-------------------------------- | ||
|
||
The MIT License (MIT) | ||
|
||
Copyright (c) 2012 Khalid Abuhakmeh | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining a copy | ||
of this software and associated documentation files (the "Software"), to deal | ||
in the Software without restriction, including without limitation the rights | ||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
copies of the Software, and to permit persons to whom the Software is | ||
furnished to do so, subject to the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be included in all | ||
copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
SOFTWARE. |
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,271 @@ | ||
// The MIT License (MIT) | ||
// | ||
// Copyright (c) 2012 Khalid Abuhakmeh | ||
// | ||
// Permission is hereby granted, free of charge, to any person obtaining a copy | ||
// of this software and associated documentation files (the "Software"), to deal | ||
// in the Software without restriction, including without limitation the rights | ||
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
// copies of the Software, and to permit persons to whom the Software is | ||
// furnished to do so, subject to the following conditions: | ||
// | ||
// The above copyright notice and this permission notice shall be included in all | ||
// copies or substantial portions of the Software. | ||
// | ||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
// SOFTWARE. | ||
|
||
/////////////////////////////////////////////////////////////////////////////////////////////////////////////// | ||
// ⚠ Do not modify this file. It will be replaced by a package reference once ConsoleTables has a strong name. | ||
/////////////////////////////////////////////////////////////////////////////////////////////////////////////// | ||
|
||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Reflection; | ||
using System.Text; | ||
using System.Text.RegularExpressions; | ||
|
||
namespace ConsoleTables | ||
{ | ||
public class ConsoleTable | ||
{ | ||
public IList<object> Columns { get; set; } | ||
public IList<object[]> Rows { get; protected set; } | ||
|
||
public ConsoleTableOptions Options { get; protected set; } | ||
|
||
public ConsoleTable(params string[] columns) | ||
:this(new ConsoleTableOptions { Columns = new List<string>(columns) }) | ||
{ | ||
} | ||
|
||
public ConsoleTable(ConsoleTableOptions options) | ||
{ | ||
Options = options ?? throw new ArgumentNullException("options"); | ||
Rows = new List<object[]>(); | ||
Columns = new List<object>(options.Columns); | ||
} | ||
|
||
public ConsoleTable AddColumn(IEnumerable<string> names) | ||
{ | ||
foreach (var name in names) | ||
Columns.Add(name); | ||
return this; | ||
} | ||
|
||
public ConsoleTable AddRow(params object[] values) | ||
{ | ||
if (values == null) | ||
throw new ArgumentNullException(nameof(values)); | ||
|
||
if (!Columns.Any()) | ||
throw new Exception("Please set the columns first"); | ||
|
||
if (Columns.Count != values.Length) | ||
throw new Exception( | ||
$"The number columns in the row ({Columns.Count}) does not match the values ({values.Length}"); | ||
|
||
Rows.Add(values); | ||
return this; | ||
} | ||
|
||
public static ConsoleTable From<T>(IEnumerable<T> values) | ||
{ | ||
var table = new ConsoleTable(); | ||
|
||
var columns = GetColumns<T>(); | ||
|
||
table.AddColumn(columns); | ||
|
||
foreach (var propertyValues in values.Select(value => columns.Select(column => GetColumnValue<T>(value, column) ))) | ||
table.AddRow(propertyValues.ToArray()); | ||
|
||
return table; | ||
} | ||
|
||
public override string ToString() | ||
{ | ||
var builder = new StringBuilder(); | ||
|
||
// find the longest column by searching each row | ||
var columnLengths = ColumnLengths(); | ||
|
||
// create the string format with padding | ||
var format = Enumerable.Range(0, Columns.Count) | ||
.Select(i => " | {" + i + ",-" + columnLengths[i] + "}") | ||
.Aggregate((s, a) => s + a) + " |"; | ||
|
||
// find the longest formatted line | ||
var maxRowLength = Math.Max(0, Rows.Any() ? Rows.Max(row => string.Format(format, row).Length) : 0); | ||
var columnHeaders = string.Format(format, Columns.ToArray()); | ||
|
||
// longest line is greater of formatted columnHeader and longest row | ||
var longestLine = Math.Max(maxRowLength, columnHeaders.Length); | ||
|
||
// add each row | ||
var results = Rows.Select(row => string.Format(format, row)).ToList(); | ||
|
||
// create the divider | ||
var divider = " " + string.Join("", Enumerable.Repeat("-", longestLine - 1)) + " "; | ||
|
||
builder.AppendLine(divider); | ||
builder.AppendLine(columnHeaders); | ||
|
||
foreach (var row in results) | ||
{ | ||
builder.AppendLine(divider); | ||
builder.AppendLine(row); | ||
} | ||
|
||
builder.AppendLine(divider); | ||
|
||
if (Options.EnableCount) | ||
{ | ||
builder.AppendLine(""); | ||
builder.AppendFormat(" Count: {0}", Rows.Count); | ||
} | ||
|
||
return builder.ToString(); | ||
} | ||
|
||
public string ToMarkDownString() | ||
{ | ||
return ToMarkDownString('|'); | ||
} | ||
|
||
private string ToMarkDownString(char delimiter) | ||
{ | ||
var builder = new StringBuilder(); | ||
|
||
// find the longest column by searching each row | ||
var columnLengths = ColumnLengths(); | ||
|
||
// create the string format with padding | ||
var format = Format(columnLengths, delimiter); | ||
|
||
// find the longest formatted line | ||
var columnHeaders = string.Format(format, Columns.ToArray()); | ||
|
||
// add each row | ||
var results = Rows.Select(row => string.Format(format, row)).ToList(); | ||
|
||
// create the divider | ||
var divider = Regex.Replace(columnHeaders, @"[^|]", "-"); | ||
|
||
builder.AppendLine(columnHeaders); | ||
builder.AppendLine(divider); | ||
results.ForEach(row => builder.AppendLine(row)); | ||
|
||
return builder.ToString(); | ||
} | ||
|
||
public string ToMinimalString() | ||
{ | ||
return ToMarkDownString(char.MinValue); | ||
} | ||
|
||
public string ToStringAlternative() | ||
{ | ||
var builder = new StringBuilder(); | ||
|
||
// find the longest column by searching each row | ||
var columnLengths = ColumnLengths(); | ||
|
||
// create the string format with padding | ||
var format = Format(columnLengths); | ||
|
||
// find the longest formatted line | ||
var columnHeaders = string.Format(format, Columns.ToArray()); | ||
|
||
// add each row | ||
var results = Rows.Select(row => string.Format(format, row)).ToList(); | ||
|
||
// create the divider | ||
var divider = Regex.Replace(columnHeaders, @"[^|]", "-"); | ||
var dividerPlus = divider.Replace("|", "+"); | ||
|
||
builder.AppendLine(dividerPlus); | ||
builder.AppendLine(columnHeaders); | ||
|
||
foreach (var row in results) | ||
{ | ||
builder.AppendLine(dividerPlus); | ||
builder.AppendLine(row); | ||
} | ||
builder.AppendLine(dividerPlus); | ||
|
||
return builder.ToString(); | ||
} | ||
|
||
private string Format(List<int> columnLengths, char delimiter = '|') | ||
{ | ||
var delimiterStr = delimiter == char.MinValue ? string.Empty : delimiter.ToString(); | ||
var format = (Enumerable.Range(0, Columns.Count) | ||
.Select(i => " "+ delimiterStr + " {" + i + ",-" + columnLengths[i] + "}") | ||
.Aggregate((s, a) => s + a) + " " + delimiterStr).Trim(); | ||
return format; | ||
} | ||
|
||
private List<int> ColumnLengths() | ||
{ | ||
var columnLengths = Columns | ||
.Select((t, i) => Rows.Select(x => x[i]) | ||
.Union(new[] { Columns[i] }) | ||
.Where(x => x != null) | ||
.Select(x => x.ToString().Length).Max()) | ||
.ToList(); | ||
return columnLengths; | ||
} | ||
|
||
public void Write(Format format = ConsoleTables.Format.Default) | ||
{ | ||
switch (format) | ||
{ | ||
case ConsoleTables.Format.Default: | ||
Console.WriteLine(ToString()); | ||
break; | ||
case ConsoleTables.Format.MarkDown: | ||
Console.WriteLine(ToMarkDownString()); | ||
break; | ||
case ConsoleTables.Format.Alternative: | ||
Console.WriteLine(ToStringAlternative()); | ||
break; | ||
case ConsoleTables.Format.Minimal: | ||
Console.WriteLine(ToMinimalString()); | ||
break; | ||
default: | ||
throw new ArgumentOutOfRangeException(nameof(format), format, null); | ||
} | ||
} | ||
|
||
private static IEnumerable<string> GetColumns<T>() | ||
{ | ||
return typeof(T).GetProperties().Select(x => x.Name).ToArray(); | ||
} | ||
|
||
private static object GetColumnValue<T>(object target, string column) | ||
{ | ||
return typeof(T).GetProperty(column).GetValue(target, null); | ||
} | ||
} | ||
|
||
public class ConsoleTableOptions | ||
{ | ||
public IEnumerable<string> Columns { get; set; } = new List<string>(); | ||
public bool EnableCount { get; set; } = true; | ||
} | ||
|
||
public enum Format | ||
{ | ||
Default = 0, | ||
MarkDown = 1, | ||
Alternative = 2, | ||
Minimal = 3 | ||
} | ||
} |
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