-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathProgram.cs
235 lines (206 loc) · 8.19 KB
/
Program.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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Linq.Expressions;
using System.Reflection;
using System.Threading;
using System.Threading.Tasks;
class Program
{
class RunnerWithExplainer
{
public RunnerWithExplainer(IRunnable runnable, Action<TextWriter> explainer)
{
this.runnable = runnable;
this.explainer = explainer;
Name = runnable.GetType().Name;
}
public string Name { get; }
public Task Run()
{
return runnable.Run();
}
public void Explain(TextWriter writer = null)
{
explainer(writer ?? Console.Out);
}
readonly IRunnable runnable;
readonly Action<TextWriter> explainer;
}
static async Task Main(string[] args)
{
Console.Clear();
var runnables = RunnablesWithExplainer(RunnableProvider.Presentation());
UpdateDescription(RunnablesWithExplainer(RunnableProvider.All()));
PrintRunnables(runnables);
string line;
while ((line = Console.ReadLine().ToLowerInvariant()) != "exit")
{
if (line == "clear")
{
Console.Clear();
PrintRunnables(runnables);
}
if (int.TryParse(line, out var itemNumber))
{
if (runnables.TryGetValue(itemNumber, out var runnable))
{
Console.Clear();
var currentColor = Console.ForegroundColor;
Console.ForegroundColor = ConsoleColor.Yellow;
bool expectionCaught = false;
Stopwatch stopWatch = null;
try
{
Console.WriteLine($"-- Running");
Console.WriteLine();
stopWatch = Stopwatch.StartNew();
await runnable.Run().ConfigureAwait(false);
}
catch (Exception ex)
{
expectionCaught = true;
Console.WriteLine();
Console.WriteLine($"-- Caught: {ex.GetType().Name}('{ex.Message}')");
}
finally
{
stopWatch?.Stop();
if (!expectionCaught)
{
Console.WriteLine();
}
Console.WriteLine($"-- Execution time: {stopWatch?.Elapsed}");
runnable.Explain();
Console.ForegroundColor = currentColor;
}
}
}
}
}
static Dictionary<int, RunnerWithExplainer> RunnablesWithExplainer(IRunnable[] runnables)
{
var allRunnersWithExplainer = (
from runnable in runnables
let explainer = CreateExplainer(runnable)
select new RunnerWithExplainer(runnable, explainer)
);
var allRunnables = new Dictionary<int, RunnerWithExplainer>();
int currentIndex = 0;
foreach (var item in allRunnersWithExplainer)
{
allRunnables.Add(currentIndex++, item);
}
return allRunnables;
}
static Action<TextWriter> CreateExplainer(IRunnable runnable)
{
var extensionType = Type.GetType($"{runnable.GetType().Name}Extensions", false);
if (extensionType != null)
{
var method = extensionType.GetMethod("Explain", BindingFlags.Public | BindingFlags.Static);
if (method != null)
{
var parameter = Expression.Parameter(typeof(TextWriter), "writer");
var block = Expression.Block(Expression.Call(null, ExplanationHeaderPrinter), Expression.Call(null, method, Expression.Constant(runnable), parameter));
return Expression.Lambda<Action<TextWriter>>(block, parameter).Compile(); ;
}
}
return writer => { };
}
static void PrintExplanationHeader()
{
if (explanationHeaderEnabled)
{
Console.ForegroundColor = ConsoleColor.DarkCyan;
Console.WriteLine();
Console.WriteLine($"|{string.Join("=", Enumerable.Repeat(string.Empty, fullWidth))}|");
Console.WriteLine($"{$"| Remember".PadRight(fullWidth)}|");
Console.WriteLine($"|{string.Join("=", Enumerable.Repeat(string.Empty, fullWidth))}|");
Console.WriteLine();
}
}
static void PrintRunnables(Dictionary<int, RunnerWithExplainer> runnables)
{
var currentThreadId = Thread.CurrentThread.ManagedThreadId;
if (threadIds.Count > 4)
{
threadIds.Clear();
}
threadIds.Push(currentThreadId);
var longest = Math.Max(runnables.Values.Max(d => d.Name.Length) + 10, 35);
fullWidth = longest * 2;
var currentColor = Console.ForegroundColor;
Console.ForegroundColor = ConsoleColor.Green;
Console.WriteLine($"|{string.Join("=", Enumerable.Repeat(string.Empty, fullWidth))}|");
Console.WriteLine($"{$"| Thread(s): {string.Join(",", threadIds)}".PadRight(fullWidth)}|");
Console.WriteLine($"|{string.Join("=", Enumerable.Repeat(string.Empty, fullWidth))}|");
Console.WriteLine();
var elements = runnables.Values.Count;
var half = (elements / 2);
for (int i = 0; i < half; i++)
{
var left = runnables.ElementAtOrDefault(i);
if (left.Equals(default))
{
break;
}
var right = runnables.ElementAtOrDefault(i + half);
if (right.Equals(default))
{
break;
}
if (left.Key == right.Key)
{
continue;
}
var leftString = $" ({PadBoth(left.Key.ToString(), 5)}) {left.Value.Name}";
var rightString = $"({PadBoth(right.Key.ToString(), 5)}) {right.Value.Name}";
Console.WriteLine($"{leftString.PadRight(longest)}{rightString}");
}
if (elements % 2 == 1)
{
var last = runnables.Last();
var lastString = $"({PadBoth(last.Key.ToString(), 5)}) {last.Value.Name}";
Console.WriteLine($"{"".PadRight(longest)}{lastString}");
}
Console.WriteLine();
Console.WriteLine($"|{string.Join("=", Enumerable.Repeat(string.Empty, fullWidth))}|");
Console.WriteLine($"{$"| github.com/danielmarbach/Async.Netcore (*)".PadRight(fullWidth)}|");
Console.WriteLine($"{$"| @danielmarbach | planetgeek.ch | LinkedIn : https://goo.gl/YyWJGf".PadRight(fullWidth)}|");
Console.WriteLine($"|{string.Join("=", Enumerable.Repeat(string.Empty, fullWidth))}|");
Console.WriteLine(" (*) don't forget to star the repo ;)");
Console.ForegroundColor = currentColor;
}
static string PadBoth(string source, int length)
{
int spaces = length - source.Length;
int padLeft = spaces / 2 + source.Length;
return source.PadLeft(padLeft).PadRight(length);
}
static void UpdateDescription(Dictionary<int, RunnerWithExplainer> runnables)
{
explanationHeaderEnabled = false;
try
{
using (var file = File.CreateText("README.MD"))
{
foreach (var item in runnables.Values)
{
file.WriteLine($"## {item.Name}");
item.Explain(file);
}
}
}
finally
{
explanationHeaderEnabled = true;
}
}
static int fullWidth;
static bool explanationHeaderEnabled = true;
static Stack<int> threadIds = new Stack<int>(7);
static MethodInfo ExplanationHeaderPrinter = typeof(Program).GetMethod(nameof(PrintExplanationHeader), BindingFlags.NonPublic | BindingFlags.Static);
}