Skip to content

Commit

Permalink
#5 include options as part of the argument
Browse files Browse the repository at this point in the history
  • Loading branch information
JordiCorbilla committed Mar 25, 2021
1 parent 9181792 commit 8987895
Show file tree
Hide file tree
Showing 5 changed files with 75 additions and 36 deletions.
12 changes: 6 additions & 6 deletions table.lib/Base.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,16 +32,16 @@ public class Base<T>
public Dictionary<string, string> ColumnNameOverrides { get; set; } = new Dictionary<string, string>();
public Dictionary<string, bool> ColumnFilter { get; set; } = new Dictionary<string, bool>();
public FilterAction ColumnAction { get; set; } = FilterAction.Exclude;
public string DynamicName { get; set; } = "Dynamic";
public ConsoleColor BackgroundColor { get; set; } = ConsoleColor.Black;
public ConsoleColor ForegroundColor { get; set; } = ConsoleColor.Green;
public List<T> Items { get; set; }
public HighlightOperator Operation { get; set; }
public Options Options { get; set; } = new Options();

public Dictionary<string, TextJustification> ColumnTextJustification { get; set; } =
new Dictionary<string, TextJustification>();

public static string GetValue(T item, PropertyName property)
public string GetValue(T item, PropertyName property)
{
if (string.IsNullOrEmpty(property.Name)) return null;
object value;
Expand All @@ -59,16 +59,16 @@ public static string GetValue(T item, PropertyName property)
return ObjectToString(value);
}

public static string ObjectToString(object value)
public string ObjectToString(object value)
{
return value switch
{
string s => s,
int _ => value.ToString(),
bool _ => value.ToString(),
DateTime time => time.ToString("dd-MMM-yyyy"),
decimal value1 => value1.ToString("#,##0.00"),
double value1 => value1.ToString("#,##0.00"),
DateTime time => time.ToString(Options.DateFormat),
decimal value1 => value1.ToString(Options.DecimalFormat),
double value1 => value1.ToString(Options.DecimalFormat),
_ => (value != null ? value.ToString() : "")
};
}
Expand Down
32 changes: 32 additions & 0 deletions table.lib/Options.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
//MIT License

//Copyright (c) 2020-2021 Jordi Corbilla

//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.

namespace table.lib
{
public class Options
{
public string DateFormat { get; set; } = "dd-MMM-yyyy";
public string DecimalFormat { get; set; } = "#,##0.00";
public string DynamicName { get; set; } = "Dynamic";
public string KeyName { get; set; } = "Key_Id";
}
}
17 changes: 10 additions & 7 deletions table.lib/Table.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,15 @@ public class Table<T> : Base<T>
/// Retrieve the entire content of the collection using reflection
/// </summary>
/// <param name="list"></param>
/// <param name="overrideDynamicName"></param>
public Table(List<T> list, string overrideDynamicName = null)
/// <param name="options"></param>
public Table(List<T> list, Options options = null)
{
if (list.Count == 0) return;
if (!string.IsNullOrEmpty(overrideDynamicName))
DynamicName = overrideDynamicName;
if (options != null)
{
Options = options;
}

PropertyNames = new List<PropertyName>();
MaxWidth = new Dictionary<string, int>();
Items = list;
Expand Down Expand Up @@ -76,7 +79,7 @@ public Table(List<T> list, string overrideDynamicName = null)
while (reading)
try
{
var prop = $"{DynamicName}{index}";
var prop = $"{Options.DynamicName}{index}";
var res = propertyInfo.GetValue(row, new object[] {index});
if (!MaxWidth.ContainsKey(prop))
{
Expand Down Expand Up @@ -117,9 +120,9 @@ public static Table<T> Add(List<T> list)
return new Table<T>(list);
}

public static Table<T> Add(List<T> list, string overrideDynamic)
public static Table<T> Add(List<T> list, Options options)
{
return new Table<T>(list, overrideDynamic);
return new Table<T>(list, options);
}

public Table<T> HighlightValue(HighlightOperator operation)
Expand Down
31 changes: 11 additions & 20 deletions table.lib/TableDic.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,24 +28,20 @@ namespace table.lib
{
public class TableDic<TV, T> : Base<T>
{
public string KeyName { get; set; } = "Key_Id";

public TableDic(Dictionary<TV, T> dictionary, string overrideDynamicName = null, string overrideKeyName = null)
public TableDic(Dictionary<TV, T> dictionary, Options options = null)
{
if (dictionary.Count == 0) return;
if (!string.IsNullOrEmpty(overrideDynamicName))
DynamicName = overrideDynamicName;
if (!string.IsNullOrEmpty(overrideKeyName))
KeyName = overrideKeyName;
if (options != null)
Options = options;
PropertyNames = new List<PropertyName>();
MaxWidth = new Dictionary<string, int>();
Keys = dictionary.Select(x => x.Key).ToList();
Items = dictionary.Select(x => x.Value).ToList();
var properties = typeof(T).GetProperties();

//Add the additional Key
PropertyNames.Add(new PropertyName(KeyName));
MaxWidth.Add(KeyName, KeyName.Length);
PropertyNames.Add(new PropertyName(Options.KeyName));
MaxWidth.Add(Options.KeyName, Options.KeyName.Length);

foreach (var property in properties)
{
Expand All @@ -58,8 +54,8 @@ public TableDic(Dictionary<TV, T> dictionary, string overrideDynamicName = null,
{
var subValueLength = ObjectToString(row);

if (subValueLength.Length > MaxWidth[KeyName])
MaxWidth[KeyName] = subValueLength.Length;
if (subValueLength.Length > MaxWidth[Options.KeyName])
MaxWidth[Options.KeyName] = subValueLength.Length;
}

foreach (var row in Items)
Expand Down Expand Up @@ -87,7 +83,7 @@ public TableDic(Dictionary<TV, T> dictionary, string overrideDynamicName = null,
while (reading)
try
{
var value = $"{DynamicName}{index}";
var value = $"{Options.DynamicName}{index}";
var res = propertyInfo.GetValue(row, new object[] {index});
if (!MaxWidth.ContainsKey(value))
{
Expand Down Expand Up @@ -192,7 +188,7 @@ public void ToConsole()
var row = Items[index];
Console.Write("|");
foreach (var property in filteredPropertyNames)
if (property.Name == KeyName)
if (property.Name == Options.KeyName)
{
var keyValueParsed = ObjectToString(Keys[index]);

Expand Down Expand Up @@ -261,14 +257,9 @@ public static TableDic<TV, T> Add(Dictionary<TV, T> dictionary)
return new TableDic<TV, T>(dictionary);
}

public static TableDic<TV, T> Add(Dictionary<TV, T> dictionary, string overrideDynamic)
{
return new TableDic<TV, T>(dictionary, overrideDynamic);
}

public static TableDic<TV, T> Add(Dictionary<TV, T> dictionary, string overrideDynamic, string overrideKey)
public static TableDic<TV, T> Add(Dictionary<TV, T> dictionary, Options options)
{
return new TableDic<TV, T>(dictionary, overrideDynamic, overrideKey);
return new TableDic<TV, T>(dictionary, options);
}
}
}
19 changes: 16 additions & 3 deletions table.runner/Samples.cs
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ public static List<TestClass> GetSampleOutput()
new TestClass
{
Field1 = 13, Field2 = "Hi \"very\" long\n text", Field3 = 21111121.32m, Field4 = true,
Field5 = new DateTime(1970, 1, 1), Field6 = 34.43
Field5 = new DateTime(1970, 1, 1), Field6 = 34.4300001
}
};
return list;
Expand Down Expand Up @@ -175,6 +175,12 @@ public static Dictionary<decimal, TestClass> GetSimpleDictionaryDecimal()
public static void SimpleConsoleOutputForList()
{
Table<TestClass>.Add(GetSampleOutput()).ToConsole();
Table<TestClass>.Add(GetSampleOutput(), new Options()
{
DateFormat = "dd-MM-yy",
DecimalFormat = "#,##0.########"
})
.ToConsole();
}

public static void SimpleConsoleOutputWithHighlighterForList()
Expand Down Expand Up @@ -268,7 +274,10 @@ public static void ComplexMarkDownOutputFilteringForList()

public static void ComplexConsoleMatrix()
{
Table<IEnumerable<int>>.Add(GetIntMatrix(), "T")
Table<IEnumerable<int>>.Add(GetIntMatrix(), new Options()
{
DynamicName = "T"
})
.ToConsole();
}

Expand All @@ -277,7 +286,11 @@ public static void SimpleConsoleForDictionary()
TableDic<string, TestClass>.Add(GetSimpleDictionary())
.ToConsole();

TableDic<string, TestClass>.Add(GetSimpleDictionary(), "D", "Id")
TableDic<string, TestClass>.Add(GetSimpleDictionary(), new Options()
{
DynamicName = "D",
KeyName = "Id"
})
.ToConsole();

TableDic<string, TestClass>.Add(GetSimpleDictionary())
Expand Down

0 comments on commit 8987895

Please sign in to comment.