forked from dotnet/interactive
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ClockKernelExtension.cs
69 lines (57 loc) · 2.37 KB
/
ClockKernelExtension.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
// Copyright (c) .NET Foundation and contributors. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
using System;
using System.CommandLine;
using System.CommandLine.Invocation;
using System.Threading.Tasks;
using Microsoft.DotNet.Interactive;
using Microsoft.DotNet.Interactive.Formatting;
using static Microsoft.DotNet.Interactive.Formatting.PocketViewTags;
namespace ClockExtension
{
public class ClockKernelExtension : IKernelExtension
{
public Task OnLoadAsync(Kernel kernel)
{
Formatter.Register<DateTime>((date, writer) =>
{
writer.Write(date.DrawSvgClock());
}, "text/html");
Formatter.Register<DateTimeOffset>((date, writer) =>
{
writer.Write(date.DrawSvgClock());
}, "text/html");
var clockCommand = new Command("#!clock", "Displays a clock showing the current or specified time.")
{
new Option<int>(new[]{"-o","--hour"},
"The position of the hour hand"),
new Option<int>(new[]{"-m","--minute"},
"The position of the minute hand"),
new Option<int>(new[]{"-s","--second"},
"The position of the second hand")
};
clockCommand.Handler = CommandHandler.Create(
(int hour, int minute, int second, KernelInvocationContext invocationContext) =>
{
invocationContext.Display(SvgClock.DrawSvgClock(hour, minute, second));
});
kernel.AddDirective(clockCommand);
if (KernelInvocationContext.Current is { } context)
{
PocketView view = div(
code(nameof(ClockExtension)),
" is loaded. It adds visualizations for ",
code(typeof(System.DateTime)),
" and ",
code(typeof(System.DateTimeOffset)),
". Try it by running: ",
code("DateTime.Now"),
" or ",
code("#!clock -h")
);
context.Display(view);
}
return Task.CompletedTask;
}
}
}