forked from dotnet/interactive
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProgram.cs
57 lines (51 loc) · 1.54 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
using Microsoft.DotNet.Interactive;
using Microsoft.DotNet.Interactive.Commands;
using Microsoft.DotNet.Interactive.Events;
using Microsoft.DotNet.Interactive.Formatting;
using Microsoft.DotNet.Interactive.FSharp;
using System;
using System.Linq;
var kernel = new FSharpKernel()
.UseDefaultFormatting()
.UseNugetDirective()
.UseKernelHelpers()
.UseWho();
Formatter.SetPreferredMimeTypeFor(typeof(object), "text/plain");
Formatter.Register<object>(o => o.ToString());
while (true)
{
if (ReadLine() is not { } request)
continue;
var toSubmit = new SubmitCode(request);
var response = await kernel.SendAsync(toSubmit);
response.KernelEvents.Subscribe(
e =>
{
switch (e)
{
case CommandFailed failed:
WriteLineError(failed.Message);
break;
case DisplayEvent display:
WriteLine(display.FormattedValues.First().Value);
break;
}
});
}
static string? ReadLine()
{
Console.Write("\nInput: ");
return Console.ReadLine();
}
static void WriteLine(string input)
{
Console.ForegroundColor = ConsoleColor.DarkGreen;
Console.WriteLine($"\nOutput: {input}");
Console.ForegroundColor = ConsoleColor.Gray;
}
static void WriteLineError(string input)
{
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine($"\nError: {input}");
Console.ForegroundColor = ConsoleColor.Gray;
}