Skip to content

Commit

Permalink
update ClockExtension to use the new magic command API
Browse files Browse the repository at this point in the history
  • Loading branch information
jonsequitur committed Sep 26, 2024
1 parent 6455eaa commit ae75495
Show file tree
Hide file tree
Showing 6 changed files with 119 additions and 194 deletions.
204 changes: 67 additions & 137 deletions samples/extensions/ClockExtension.ipynb

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions samples/extensions/ClockExtension/ClockExtension.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@
</ItemGroup>

<ItemGroup>
<PackageReference Include="microsoft.dotnet.interactive" Version="1.0.0-beta.23562.1" />
<PackageReference Include="microsoft.dotnet.interactive.csharp" Version="1.0.0-beta.23562.1" />
<PackageReference Include="microsoft.dotnet.interactive" Version="1.0.0-beta.24475.1" />
<PackageReference Include="microsoft.dotnet.interactive.csharp" Version="1.0.0-beta.24475.1" />
</ItemGroup>

<ItemGroup>
Expand Down
42 changes: 20 additions & 22 deletions samples/extensions/ClockExtension/ClockKernelExtension.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@
// Licensed under the MIT license. See LICENSE file in the project root for full license information.

using System;
using System.CommandLine;
using System.Threading.Tasks;
using Microsoft.DotNet.Interactive;
using Microsoft.DotNet.Interactive.Directives;
using Microsoft.DotNet.Interactive.Formatting;
using static Microsoft.DotNet.Interactive.Formatting.PocketViewTags;

Expand All @@ -19,27 +20,26 @@ public static void Load(Kernel kernel)
Formatter.Register<DateTimeOffset>((date, writer) => writer.Write(date.DrawSvgClock()), "text/html");

// Next, define a magic command that will render a clock.
var hourOption = new Option<int>(new[] { "-o", "--hour" },
"The position of the hour hand");
var minuteOption = new Option<int>(new[] { "-m", "--minute" },
"The position of the minute hand");
var secondOption = new Option<int>(new[] { "-s", "--second" },
"The position of the second hand");

var clockCommand = new Command("#!clock", "Displays a clock showing the current or specified time.")
var hourParameter = new KernelDirectiveParameter("--hour", "The position of the hour hand");
var minuteParameter = new KernelDirectiveParameter("--minute", "The position of the minute hand");
var secondParameter = new KernelDirectiveParameter("--second", "The position of the second hand");

var clockDirective = new KernelActionDirective("#!clock")
{
hourOption,
minuteOption,
secondOption
Description = "Displays a clock showing the current or specified time.",
Parameters =
[
hourParameter,
minuteParameter,
secondParameter
]
};

clockCommand.SetHandler(
(hour, minute, second) => KernelInvocationContext.Current.Display(SvgClock.DrawSvgClock(hour, minute, second)),
hourOption,
minuteOption,
secondOption);

kernel.AddDirective(clockCommand);
kernel.AddDirective<DisplayClock>(clockDirective, (displayClock, context) =>
{
context.Display(SvgClock.DrawSvgClock(displayClock.Hour, displayClock.Minute, displayClock.Second));
return Task.CompletedTask;
});

// Finally, display some information to the user so they can see how to use the extension.
PocketView view = div(
Expand All @@ -49,9 +49,7 @@ public static void Load(Kernel kernel)
" and ",
code(typeof(DateTimeOffset)),
". Try it by running: ",
code("DateTime.Now"),
" or ",
code("#!clock -h")
code("DateTime.Now")
);

KernelInvocationContext.Current?.Display(view);
Expand Down
10 changes: 10 additions & 0 deletions samples/extensions/ClockExtension/DisplayClock.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
using Microsoft.DotNet.Interactive.Commands;

namespace ClockExtension;

public class DisplayClock : KernelDirectiveCommand
{
public int Hour { get; set; }
public int Minute { get; set; }
public int Second { get; set; }
}
49 changes: 18 additions & 31 deletions samples/extensions/SampleExtensions.Tests/ClockExtensionTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,61 +25,48 @@ public ClockExtensionTests()
};

ClockKernelExtension.Load(_kernel);

KernelEvents = _kernel.KernelEvents.ToSubscribedList();
}

public SubscribedList<KernelEvent> KernelEvents { get; }

public void Dispose()
{
_kernel.Dispose();
KernelEvents.Dispose();
}

[Fact]
public async Task It_formats_DateTime()
{
using var events = _kernel.KernelEvents.ToSubscribedList();
var result = await _kernel.SubmitCodeAsync("DateTime.Now");

await _kernel.SubmitCodeAsync("DateTime.Now");

AssertThatClockWasRendered();
AssertThatClockWasRendered(result);
}

[Fact]
public async Task It_formats_DateTimeOffset()
{
using var events = _kernel.KernelEvents.ToSubscribedList();

await _kernel.SubmitCodeAsync("DateTimeOffset.Now");
var result = await _kernel.SubmitCodeAsync("DateTimeOffset.Now");

AssertThatClockWasRendered();
AssertThatClockWasRendered(result);
}

[Fact]
public async Task It_adds_a_clock_magic_command()
{
using var events = _kernel.KernelEvents.ToSubscribedList();
var result = await _kernel.SubmitCodeAsync("#!clock");

await _kernel.SubmitCodeAsync("#!clock");

AssertThatClockWasRendered();
AssertThatClockWasRendered(result);
}

private void AssertThatClockWasRendered()
{
KernelEvents
.Should()
.ContainSingle<DisplayEvent>()
.Which
.FormattedValues
.Should()
.ContainSingle(v => v.MimeType == "text/html")
.Which
.Value
.Should()
.Contain("<circle");
}
private void AssertThatClockWasRendered(KernelCommandResult result) =>
result.Events
.Should()
.ContainSingle<DisplayEvent>()
.Which
.FormattedValues
.Should()
.ContainSingle(v => v.MimeType == "text/html")
.Which
.Value
.Should()
.Contain("<circle");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@
</ItemGroup>

<ItemGroup>
<PackageReference Include="microsoft.dotnet.interactive.csharp" Version="1.0.0-beta.23562.1" />
<PackageReference Include="microsoft.dotnet.interactive.fsharp" Version="1.0.0-beta.23562.1" />
<PackageReference Include="microsoft.dotnet.interactive.csharp" Version="1.0.0-beta.24475.1" />
<PackageReference Include="microsoft.dotnet.interactive.fsharp" Version="1.0.0-beta.24475.1" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.10.0" />
<PackageReference Include="Newtonsoft.Json" Version="13.0.3" />
<PackageReference Include="xunit" Version="2.4.2" />
Expand Down

0 comments on commit ae75495

Please sign in to comment.