diff --git a/table.lib/Base.cs b/table.lib/Base.cs index 9d482d9..6eb62b7 100644 --- a/table.lib/Base.cs +++ b/table.lib/Base.cs @@ -32,16 +32,16 @@ public class Base public Dictionary ColumnNameOverrides { get; set; } = new Dictionary(); public Dictionary ColumnFilter { get; set; } = new Dictionary(); 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 Items { get; set; } public HighlightOperator Operation { get; set; } + public Options Options { get; set; } = new Options(); public Dictionary ColumnTextJustification { get; set; } = new Dictionary(); - public static string GetValue(T item, PropertyName property) + public string GetValue(T item, PropertyName property) { if (string.IsNullOrEmpty(property.Name)) return null; object value; @@ -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() : "") }; } diff --git a/table.lib/Options.cs b/table.lib/Options.cs new file mode 100644 index 0000000..b6dc65f --- /dev/null +++ b/table.lib/Options.cs @@ -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"; + } +} diff --git a/table.lib/Table.cs b/table.lib/Table.cs index 513e8fa..1189396 100644 --- a/table.lib/Table.cs +++ b/table.lib/Table.cs @@ -35,12 +35,15 @@ public class Table : Base /// Retrieve the entire content of the collection using reflection /// /// - /// - public Table(List list, string overrideDynamicName = null) + /// + public Table(List list, Options options = null) { if (list.Count == 0) return; - if (!string.IsNullOrEmpty(overrideDynamicName)) - DynamicName = overrideDynamicName; + if (options != null) + { + Options = options; + } + PropertyNames = new List(); MaxWidth = new Dictionary(); Items = list; @@ -76,7 +79,7 @@ public Table(List 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)) { @@ -117,9 +120,9 @@ public static Table Add(List list) return new Table(list); } - public static Table Add(List list, string overrideDynamic) + public static Table Add(List list, Options options) { - return new Table(list, overrideDynamic); + return new Table(list, options); } public Table HighlightValue(HighlightOperator operation) diff --git a/table.lib/TableDic.cs b/table.lib/TableDic.cs index a066e30..f8f7e5e 100644 --- a/table.lib/TableDic.cs +++ b/table.lib/TableDic.cs @@ -28,15 +28,11 @@ namespace table.lib { public class TableDic : Base { - public string KeyName { get; set; } = "Key_Id"; - - public TableDic(Dictionary dictionary, string overrideDynamicName = null, string overrideKeyName = null) + public TableDic(Dictionary 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(); MaxWidth = new Dictionary(); Keys = dictionary.Select(x => x.Key).ToList(); @@ -44,8 +40,8 @@ public TableDic(Dictionary dictionary, string overrideDynamicName = null, 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) { @@ -58,8 +54,8 @@ public TableDic(Dictionary 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) @@ -87,7 +83,7 @@ public TableDic(Dictionary 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)) { @@ -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]); @@ -261,14 +257,9 @@ public static TableDic Add(Dictionary dictionary) return new TableDic(dictionary); } - public static TableDic Add(Dictionary dictionary, string overrideDynamic) - { - return new TableDic(dictionary, overrideDynamic); - } - - public static TableDic Add(Dictionary dictionary, string overrideDynamic, string overrideKey) + public static TableDic Add(Dictionary dictionary, Options options) { - return new TableDic(dictionary, overrideDynamic, overrideKey); + return new TableDic(dictionary, options); } } } \ No newline at end of file diff --git a/table.runner/Samples.cs b/table.runner/Samples.cs index 607fa45..da1ed47 100644 --- a/table.runner/Samples.cs +++ b/table.runner/Samples.cs @@ -60,7 +60,7 @@ public static List 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; @@ -175,6 +175,12 @@ public static Dictionary GetSimpleDictionaryDecimal() public static void SimpleConsoleOutputForList() { Table.Add(GetSampleOutput()).ToConsole(); + Table.Add(GetSampleOutput(), new Options() + { + DateFormat = "dd-MM-yy", + DecimalFormat = "#,##0.########" + }) + .ToConsole(); } public static void SimpleConsoleOutputWithHighlighterForList() @@ -268,7 +274,10 @@ public static void ComplexMarkDownOutputFilteringForList() public static void ComplexConsoleMatrix() { - Table>.Add(GetIntMatrix(), "T") + Table>.Add(GetIntMatrix(), new Options() + { + DynamicName = "T" + }) .ToConsole(); } @@ -277,7 +286,11 @@ public static void SimpleConsoleForDictionary() TableDic.Add(GetSimpleDictionary()) .ToConsole(); - TableDic.Add(GetSimpleDictionary(), "D", "Id") + TableDic.Add(GetSimpleDictionary(), new Options() + { + DynamicName = "D", + KeyName = "Id" + }) .ToConsole(); TableDic.Add(GetSimpleDictionary())