From 6a7f1efe4332f40e2c05056a3f0bb2b59bd0f612 Mon Sep 17 00:00:00 2001 From: JordiCorbilla Date: Tue, 30 Mar 2021 11:08:59 +0100 Subject: [PATCH] #6 Adding highlighter for multiple cells --- table.lib/Base.cs | 65 +++++++++++++++++++++++---------- table.lib/HighlightOperation.cs | 30 +++++++++++++++ table.lib/HighlightOperator.cs | 1 + table.runner/Samples.cs | 5 ++- 4 files changed, 80 insertions(+), 21 deletions(-) create mode 100644 table.lib/HighlightOperation.cs diff --git a/table.lib/Base.cs b/table.lib/Base.cs index 81c13b6..f868b47 100644 --- a/table.lib/Base.cs +++ b/table.lib/Base.cs @@ -35,7 +35,10 @@ public class Base public ConsoleColor BackgroundColor { get; set; } = ConsoleColor.Black; public ConsoleColor ForegroundColor { get; set; } = ConsoleColor.Green; public List Items { get; set; } - public Dictionary Operation { get; set; } = new Dictionary(); + + public Dictionary Operation { get; set; } = + new Dictionary(); + public Options Options { get; set; } = new Options(); public Dictionary ColumnTextJustification { get; set; } = @@ -106,7 +109,6 @@ public void ConsoleRender(string value, string column) Console.ForegroundColor = ForegroundColor; if (Operation != null) - { if (Operation.ContainsKey(column)) switch (Operation[column].Type) { @@ -114,15 +116,28 @@ public void ConsoleRender(string value, string column) try { var parsed = decimal.Parse(value.Trim()); - if (parsed != Operation[column].DecimalValue) - { - Console.BackgroundColor = Operation[column].BackgroundColorIfDifferent; - Console.ForegroundColor = Operation[column].ForegroundColorIfDifferent; - } - else + switch (Operation[column].Operation) { - Console.BackgroundColor = Operation[column].BackgroundColorIfEqual; - Console.ForegroundColor = Operation[column].ForegroundColorIfEqual; + case HighlightOperation.Differences when parsed != Operation[column].DecimalValue: + Console.BackgroundColor = Operation[column].BackgroundColorIfDifferent; + Console.ForegroundColor = Operation[column].ForegroundColorIfDifferent; + break; + case HighlightOperation.Differences: + Console.BackgroundColor = Operation[column].BackgroundColorIfEqual; + Console.ForegroundColor = Operation[column].ForegroundColorIfEqual; + break; + case HighlightOperation.Equality: + { + if (parsed == Operation[column].DecimalValue) + { + Console.BackgroundColor = Operation[column].BackgroundColorIfEqual; + Console.ForegroundColor = Operation[column].ForegroundColorIfEqual; + } + + break; + } + default: + throw new ArgumentOutOfRangeException(); } } catch @@ -134,15 +149,28 @@ public void ConsoleRender(string value, string column) case HighlightType.String: try { - if (value.Trim() != Operation[column].StringValue) - { - Console.BackgroundColor = Operation[column].BackgroundColorIfDifferent; - Console.ForegroundColor = Operation[column].ForegroundColorIfDifferent; - } - else + switch (Operation[column].Operation) { - Console.BackgroundColor = Operation[column].BackgroundColorIfEqual; - Console.ForegroundColor = Operation[column].ForegroundColorIfEqual; + case HighlightOperation.Differences when value.Trim() != Operation[column].StringValue: + Console.BackgroundColor = Operation[column].BackgroundColorIfDifferent; + Console.ForegroundColor = Operation[column].ForegroundColorIfDifferent; + break; + case HighlightOperation.Differences: + Console.BackgroundColor = Operation[column].BackgroundColorIfEqual; + Console.ForegroundColor = Operation[column].ForegroundColorIfEqual; + break; + case HighlightOperation.Equality: + { + if (value.Trim() == Operation[column].StringValue) + { + Console.BackgroundColor = Operation[column].BackgroundColorIfEqual; + Console.ForegroundColor = Operation[column].ForegroundColorIfEqual; + } + + break; + } + default: + throw new ArgumentOutOfRangeException(); } } catch @@ -154,7 +182,6 @@ public void ConsoleRender(string value, string column) default: throw new ArgumentOutOfRangeException(); } - } Console.Write(value); Console.ResetColor(); diff --git a/table.lib/HighlightOperation.cs b/table.lib/HighlightOperation.cs new file mode 100644 index 0000000..68a918b --- /dev/null +++ b/table.lib/HighlightOperation.cs @@ -0,0 +1,30 @@ +//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 enum HighlightOperation + { + Differences = 0, + Equality = 1 + } +} \ No newline at end of file diff --git a/table.lib/HighlightOperator.cs b/table.lib/HighlightOperator.cs index eba17d7..d07366a 100644 --- a/table.lib/HighlightOperator.cs +++ b/table.lib/HighlightOperator.cs @@ -56,5 +56,6 @@ public class HighlightOperator public ConsoleColor ForegroundColorIfDifferent { get; set; } = ConsoleColor.White; public ConsoleColor BackgroundColorIfDifferent { get; set; } = ConsoleColor.DarkRed; public HighlightType Type { get; set; } = HighlightType.String; + public HighlightOperation Operation { get; set; } = HighlightOperation.Differences; } } \ No newline at end of file diff --git a/table.runner/Samples.cs b/table.runner/Samples.cs index f838266..247d6d9 100644 --- a/table.runner/Samples.cs +++ b/table.runner/Samples.cs @@ -305,12 +305,13 @@ public static void SimpleConsoleOutputWithHighlighterForDictionary() { TableDic.Add(GetSimpleDictionary()) .HighlightValue(new HighlightOperator - {Field = "Field3", Type = HighlightType.Decimal, DecimalValue = 2121.32m}) + {Field = "Field3", Type = HighlightType.Decimal, DecimalValue = 2121.32m, Operation = HighlightOperation.Equality}) + .HighlightValue(new HighlightOperator + { Field = "Field6", Type = HighlightType.Decimal, DecimalValue = 34.43m, Operation = HighlightOperation.Equality }) .ToConsole(); TableDic.Add(GetSimpleDictionary()) .ToCsv(@"C:\temp\testDic.csv"); - } } } \ No newline at end of file