Skip to content

Commit c1d8aab

Browse files
committed
Added new IFormatter interface
1 parent 620c5a5 commit c1d8aab

File tree

3 files changed

+32
-10
lines changed

3 files changed

+32
-10
lines changed

BooleanExpressionParser/Formatter.cs renamed to BooleanExpressionParser/Formatters/DisplayFormatter.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
using System.Text;
33
using Spectre.Console;
44

5-
namespace BooleanExpressionParser;
5+
namespace BooleanExpressionParser.Formatter;
66

77
enum ColourMode
88
{
@@ -12,7 +12,7 @@ enum ColourMode
1212
}
1313

1414

15-
class Formatter
15+
class DisplayFormatter : IFormatter
1616
{
1717
private static int FinalPadding = 2;
1818

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
namespace BooleanExpressionParser.Formatter
2+
{
3+
interface IFormatter
4+
{
5+
string FormatTokens(IEnumerable<Token> tokens);
6+
7+
string FormatTruthTable(Ast ast, List<bool[]> table, string label);
8+
9+
string JoinTruthTables(params string[] tables);
10+
}
11+
}

BooleanExpressionParser/Program.cs

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using System.CommandLine;
22
using System.Text;
3+
using BooleanExpressionParser.Formatter;
34
using Spectre.Console;
45

56
namespace BooleanExpressionParser;
@@ -68,14 +69,17 @@ private static void TableHandler(OutputType outputType, string @true, string @fa
6869
var expressions = ParseExpressions(args);
6970

7071
var tables = new List<string>();
71-
var formatter = new Formatter
72+
73+
var formatter = GetFormatter(outputType);
74+
75+
if (formatter is DisplayFormatter displayFormatter)
7276
{
73-
True = @true,
74-
False = @false,
75-
ColourMode = colourMode,
76-
TrueColour = trueColour,
77-
FalseColour = falseColour
78-
};
77+
displayFormatter.True = @true;
78+
displayFormatter.False = @false;
79+
displayFormatter.ColourMode = colourMode;
80+
displayFormatter.TrueColour = trueColour;
81+
displayFormatter.FalseColour = falseColour;
82+
}
7983

8084
foreach (var expression in expressions)
8185
{
@@ -118,7 +122,7 @@ private static void TableHandler(OutputType outputType, string @true, string @fa
118122
private static void ConvertHandler(OutputType outputType, string[] args)
119123
{
120124
var expressions = ParseExpressions(args);
121-
var formatter = new Formatter();
125+
var formatter = GetFormatter(outputType);
122126

123127
foreach (var expression in expressions)
124128
{
@@ -135,6 +139,13 @@ private static void ConvertHandler(OutputType outputType, string[] args)
135139

136140
static List<ExpressionWrapper> ParseExpressions(string[] args) => args.Length == 0 ? QueryExpressions() : args.Select(arg => new ExpressionWrapper(arg)).ToList();
137141

142+
static IFormatter GetFormatter(OutputType outputType) => outputType switch
143+
{
144+
OutputType.Display => new DisplayFormatter(),
145+
OutputType.Basic => new BasicFormatter(),
146+
_ => throw new ArgumentOutOfRangeException(nameof(outputType), outputType, null)
147+
};
148+
138149

139150
static List<ExpressionWrapper> QueryExpressions()
140151
{

0 commit comments

Comments
 (0)