-
Notifications
You must be signed in to change notification settings - Fork 0
/
ObjectList.cs
102 lines (85 loc) · 3.38 KB
/
ObjectList.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
//============================================================
// Student Number : S10203296, S10205301
// Student Name : Benedict Woo, Melvin Kee
// Module Group : T06
//============================================================
using System;
using System.Collections.Generic;
using System.Text;
using System.Text.RegularExpressions;
using COVIDMonitoringSystem.ConsoleApp.Utilities;
using COVIDMonitoringSystem.Core.Utilities;
namespace COVIDMonitoringSystem.ConsoleApp.Display.Elements
{
public class ObjectList<T> : Element where T : class
{
private const int ColumnGap = 4;
public string[] PropertyToInclude { get; set; }
public Func<List<T>> ListGetter { get; set; }
public ObjectList(string name = null) : base(name)
{
}
protected override int WriteToScreen()
{
var objList = ListGetter?.Invoke();
if (objList == null)
{
CHelper.WriteLine("NULL");
return 1;
}
var columnWidths = new int[PropertyToInclude.Length];
var headerItems = new string[PropertyToInclude.Length];
for (var i = 0; i < PropertyToInclude.Length; i++)
{
headerItems[i] = SplitPascalCase(PropertyToInclude[i]);
}
for (var i = 0; i < headerItems.Length; i++)
{
columnWidths[i] = headerItems[i].Length;
}
var valuesArray = new Dictionary<string, string>[objList.Count];
var x = 0;
foreach (var item in objList)
{
var propertyValues = ReflectHelper.GetAllPropertyValues(item);
for (var index = 0; index < PropertyToInclude.Length; index++)
{
var textLength = propertyValues[PropertyToInclude[index]].Length;
if (textLength > columnWidths[index])
{
columnWidths[index] = textLength;
}
}
valuesArray[x++] = propertyValues;
}
var headerBuilder = new StringBuilder();
for (var index = 0; index < columnWidths.Length; index++)
{
var format = $"{{0,-{columnWidths[index] + ColumnGap}}}";
headerBuilder.Append(string.Format(format, headerItems[index]));
}
ColourSelector.Selected();
CHelper.WriteEmpty();
CHelper.WriteLine(headerBuilder.ToString());
ColourSelector.Default();
CHelper.WriteEmpty();
foreach (var propertyValues in valuesArray)
{
var contentBuilder = new StringBuilder();
for (var index = 0; index < columnWidths.Length; index++)
{
contentBuilder.Append(string.Format($"{{0,-{columnWidths[index] + ColumnGap}}}",
propertyValues[PropertyToInclude[index]]));
}
CHelper.WriteLine(contentBuilder.ToString());
CHelper.WriteEmpty();
}
//TODO: Use builder and return correct lines written
return 1;
}
private static string SplitPascalCase(string text)
{
return Regex.Replace(text, @"(?<=[A-Za-z])(?=[A-Z][a-z])|(?<=[a-z0-9])(?=[0-9]?[A-Z])", " ");
}
}
}