Skip to content

Commit 0f8ca24

Browse files
committed
Add Spectre.Console to add coloured output
1 parent 12f2eb3 commit 0f8ca24

File tree

4 files changed

+24
-10
lines changed

4 files changed

+24
-10
lines changed

BooleanExpressionParser/BooleanExpressionParser.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
</PropertyGroup>
99

1010
<ItemGroup>
11+
<PackageReference Include="Spectre.Console" Version="0.45.0" />
1112
<PackageReference Include="System.CommandLine" Version="2.0.0-beta4.22272.1" />
1213
<PackageReference Include="System.CommandLine.DragonFruit" Version="0.4.0-alpha.22272.1" />
1314
</ItemGroup>

BooleanExpressionParser/Formatter.cs

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,10 @@ namespace BooleanExpressionParser;
44

55
class Formatter
66
{
7-
public static string FormatTokens(IEnumerable<Token> tokens)
7+
public string True { get; set; } = "1";
8+
public string False { get; set; } = "0";
9+
10+
public string FormatTokens(IEnumerable<Token> tokens)
811
{
912
var sb = new StringBuilder();
1013

@@ -18,7 +21,7 @@ public static string FormatTokens(IEnumerable<Token> tokens)
1821
return sb.ToString();
1922
}
2023

21-
public static string FormatTruthTable(Ast ast, List<bool[]> table, string label = "Result")
24+
public string FormatTruthTable(Ast ast, List<bool[]> table, string label = "Result")
2225
{
2326
var sb = new StringBuilder();
2427

@@ -44,12 +47,12 @@ public static string FormatTruthTable(Ast ast, List<bool[]> table, string label
4447
{
4548
string pad1 = Repeat(' ', (int)Math.Ceiling(ast.Variables[i].Length / 2.0f));
4649
string pad2 = Repeat(' ', (int)Math.Floor(ast.Variables[i].Length / 2.0f));
47-
sb.Append($"{pad1}{(row[i] ? '1' : '0')}{pad2} ");
50+
sb.Append($"{pad1}{(row[i] ? $"[green]{True}[/]" : $"[red]{False}[/]")}{pad2} ");
4851
}
4952

5053
string pad3 = Repeat(' ', (int)Math.Ceiling(label.Length / 2.0f));
5154
string pad4 = Repeat(' ', (int)Math.Floor(label.Length / 2.0f));
52-
sb.AppendLine($" ┃ {pad3}{(row[^1] ? '1' : '0')}{pad4} ┃");
55+
sb.AppendLine($" ┃ {pad3}{(row[^1] ? $"[green]{True}[/]" : $"[red]{False}[/]")}{pad4} ┃");
5356
}
5457

5558
sb.Append("┗━");
@@ -61,7 +64,7 @@ public static string FormatTruthTable(Ast ast, List<bool[]> table, string label
6164

6265
static string Repeat(char c, int count) => new string(c, count);
6366

64-
public static String JoinTruthTables(params string[] tables)
67+
public String JoinTruthTables(params string[] tables)
6568
{
6669
var sb = new StringBuilder();
6770

BooleanExpressionParser/Parser.cs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,8 @@ public IEnumerable<Token> ParseTokens(IEnumerable<Token> tokens)
2020
break;
2121

2222
case OperatorToken op:
23-
while ((stack.Count > 0 && stack.Peek() is OperatorToken && stack.Peek() is not OpenParenToken) && ((stack.Peek() as OperatorToken)!.Precedence >= op!.Precedence))
23+
while ((stack.Count > 0 && stack.Peek() is OperatorToken && stack.Peek() is not OpenParenToken) &&
24+
((stack.Peek() as OperatorToken)!.Precedence >= op!.Precedence))
2425
{
2526
output.Enqueue(stack.Pop());
2627
}
@@ -62,7 +63,7 @@ public IEnumerable<Token> ParseTokens(IEnumerable<Token> tokens)
6263
return output;
6364
}
6465

65-
public Ast GrowAst(IEnumerable<Token> tokens)
66+
public Ast GrowAst(IEnumerable<Token> tokens, string[] variableOrder)
6667
{
6768
var stack = new Stack<Node>();
6869
var variables = new List<string>();
@@ -109,6 +110,8 @@ public Ast GrowAst(IEnumerable<Token> tokens)
109110

110111
var root = stack.Pop();
111112

113+
variables = variables.OrderBy(v => Array.IndexOf(variableOrder, v)).ToList();
114+
112115
return new Ast(root, variables);
113116
}
114117
}

BooleanExpressionParser/Program.cs

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using System.Text;
2+
using Spectre.Console;
23

34
namespace BooleanExpressionParser;
45

@@ -25,6 +26,10 @@ static void Main(params string[] args)
2526
}
2627

2728
var tables = new List<string>();
29+
var formatter = new Formatter {
30+
True = "T",
31+
False = "F"
32+
};
2833

2934
foreach (var expression in expressions)
3035
{
@@ -49,16 +54,18 @@ static void Main(params string[] args)
4954
table.Add(values.Append(result).ToArray());
5055
}
5156

52-
tables.Add(Formatter.FormatTruthTable(ast, table, label: expression.Expression));
57+
tables.Add(formatter.FormatTruthTable(ast, table, label: expression.Expression));
5358
}
5459

5560
if (tables.Count > 1)
5661
{
57-
Console.WriteLine(Formatter.JoinTruthTables(tables.ToArray()));
62+
Console.WriteLine(formatter.JoinTruthTables(tables.ToArray()));
63+
5864
}
5965
else
6066
{
61-
Console.WriteLine(tables[0]);
67+
// Console.WriteLine(tables[0]);
68+
AnsiConsole.Markup(tables[0]);
6269
}
6370
}
6471

0 commit comments

Comments
 (0)