Skip to content

Commit

Permalink
#6 Adding highlighter for multiple cells
Browse files Browse the repository at this point in the history
  • Loading branch information
JordiCorbilla committed Mar 30, 2021
1 parent ee33e5f commit 6a7f1ef
Show file tree
Hide file tree
Showing 4 changed files with 80 additions and 21 deletions.
65 changes: 46 additions & 19 deletions table.lib/Base.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,10 @@ public class Base<T>
public ConsoleColor BackgroundColor { get; set; } = ConsoleColor.Black;
public ConsoleColor ForegroundColor { get; set; } = ConsoleColor.Green;
public List<T> Items { get; set; }
public Dictionary<string, HighlightOperator> Operation { get; set; } = new Dictionary<string, HighlightOperator>();

public Dictionary<string, HighlightOperator> Operation { get; set; } =
new Dictionary<string, HighlightOperator>();

public Options Options { get; set; } = new Options();

public Dictionary<string, TextJustification> ColumnTextJustification { get; set; } =
Expand Down Expand Up @@ -106,23 +109,35 @@ public void ConsoleRender(string value, string column)
Console.ForegroundColor = ForegroundColor;

if (Operation != null)
{
if (Operation.ContainsKey(column))
switch (Operation[column].Type)
{
case HighlightType.Decimal:
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
Expand All @@ -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
Expand All @@ -154,7 +182,6 @@ public void ConsoleRender(string value, string column)
default:
throw new ArgumentOutOfRangeException();
}
}

Console.Write(value);
Console.ResetColor();
Expand Down
30 changes: 30 additions & 0 deletions table.lib/HighlightOperation.cs
Original file line number Diff line number Diff line change
@@ -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
}
}
1 change: 1 addition & 0 deletions table.lib/HighlightOperator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
5 changes: 3 additions & 2 deletions table.runner/Samples.cs
Original file line number Diff line number Diff line change
Expand Up @@ -305,12 +305,13 @@ public static void SimpleConsoleOutputWithHighlighterForDictionary()
{
TableDic<string, TestClass>.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<string, TestClass>.Add(GetSimpleDictionary())
.ToCsv(@"C:\temp\testDic.csv");

}
}
}

0 comments on commit 6a7f1ef

Please sign in to comment.