这是indexloc提供的服务,不要输入任何密码
Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 8 additions & 8 deletions Microsoft.DotNet.Interactive.Jupyter/ExecuteRequestHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public async Task Handle(JupyterRequestContext context)
}

protected override void OnKernelEventReceived(
IKernelEvent @event,
IKernelEvent @event,
JupyterRequestContext context)
{
switch (@event)
Expand All @@ -53,7 +53,7 @@ protected override void OnKernelEventReceived(
OnCommandHandled(context.JupyterMessageSender);
break;
case CommandFailed commandFailed:
OnCommandFailed(commandFailed, context.JupyterMessageSender);
OnCommandFailed(commandFailed, context.JupyterMessageSender);
break;
}
}
Expand All @@ -78,11 +78,11 @@ private void OnCommandFailed(
default:
traceBack.Add("Unhandled Exception");
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does the output look correct if the traceback is added as a single string with line breaks?

traceBack.Add(commandFailed.Message);
traceBack.AddRange(commandFailed.Exception?.StackTrace?.Split(new[] { Environment.NewLine }, StringSplitOptions.None) ?? Enumerable.Empty<string>());
traceBack.Add(commandFailed.Exception?.StackTrace);
break;
}

var errorContent = new Error (
var errorContent = new Error(
eName: "Unhandled Exception",
eValue: commandFailed.Message,
traceback: traceBack
Expand Down Expand Up @@ -115,7 +115,7 @@ private void OnDisplayEvent(DisplayEventBase displayEvent,
.ToDictionary(k => k.MimeType, v => v.Value);

var value = displayEvent.Value;
PubSubMessage dataMessage = null;
PubSubMessage dataMessage;

CreateDefaultFormattedValueIfEmpty(formattedValues, value);

Expand Down Expand Up @@ -143,7 +143,6 @@ private void OnDisplayEvent(DisplayEventBase displayEvent,
case StandardOutputValueProduced _:
dataMessage = Stream.StdOut(GetPlainTextValueOrDefault(formattedValues, value?.ToString() ?? string.Empty));
break;

case StandardErrorValueProduced _:
case ErrorProduced _:
dataMessage = Stream.StdErr(GetPlainTextValueOrDefault(formattedValues, value?.ToString() ?? string.Empty));
Expand All @@ -153,6 +152,7 @@ private void OnDisplayEvent(DisplayEventBase displayEvent,
throw new ArgumentException("Unsupported event type", nameof(displayEvent));
}


var isSilent = ((ExecuteRequest)request.Content).Silent;

if (!isSilent)
Expand All @@ -162,7 +162,7 @@ private void OnDisplayEvent(DisplayEventBase displayEvent,
}
}

private string GetPlainTextValueOrDefault(Dictionary<string, object> formattedValues, string defaultText)
private static string GetPlainTextValueOrDefault(IReadOnlyDictionary<string, object> formattedValues, string defaultText)
{
if (formattedValues.TryGetValue(PlainTextFormatter.MimeType, out var text))
{
Expand All @@ -188,7 +188,7 @@ private void OnCommandHandled(IJupyterMessageSender jupyterMessageSender)
var executeReplyPayload = new ExecuteReplyOk(executionCount: _executionCount);

// send to server
jupyterMessageSender.Send(executeReplyPayload);
jupyterMessageSender.Send(executeReplyPayload);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
// 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 FluentAssertions;
using Xunit;


namespace Microsoft.DotNet.Interactive.Rendering.Tests
{
public class TerminalFormatterTests
{
[Fact]
public void Non_generic_Create_creates_generic_formatter()
{
TerminalFormatter.Create(typeof(Widget))
.Should()
.BeOfType<TerminalFormatter<Widget>>();
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
// 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.

namespace Microsoft.DotNet.Interactive.Rendering
{
internal class DefaultTerminalFormatterSet : FormatterSetBase
{

}
}
1 change: 0 additions & 1 deletion Microsoft.DotNet.Interactive.Rendering/HtmlFormatter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@

using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.AspNetCore.Html;
using static Microsoft.DotNet.Interactive.Rendering.PocketViewTags;

Expand Down
2 changes: 1 addition & 1 deletion Microsoft.DotNet.Interactive.Rendering/HtmlFormatter{T}.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public override void Format(T instance, TextWriter writer)
_format(instance, writer);
}

public override string MimeType => "text/html";
public override string MimeType => HtmlFormatter.MimeType;

public static ITypeFormatter<T> Create(bool includeInternals = false)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.Html.Abstractions" Version="2.2.0" />
<PackageReference Include="microsoft.csharp" Version="4.5.0" />
<PackageReference Include="System.CommandLine.Rendering" Version="0.3.0-alpha.19420.2" />
</ItemGroup>

<ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ public static ITypeFormatter<T> Create(bool includeInternals = false)
return Default;
}

public override string MimeType => "text/plain";
public override string MimeType => PlainTextFormatter.MimeType;

public override void Format(T instance, TextWriter writer)
{
Expand Down
31 changes: 31 additions & 0 deletions Microsoft.DotNet.Interactive.Rendering/TerminalFormatter.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
// 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;

namespace Microsoft.DotNet.Interactive.Rendering
{
public class TerminalFormatter
{
public static ITypeFormatter Create(
Type type,
bool includeInternals = false)
{
var genericCreateForAllMembers = typeof(TerminalFormatter<>)
.MakeGenericType(type)
.GetMethod(nameof(TerminalFormatter<object>.Create), new[]
{
typeof(bool)
});

return (ITypeFormatter)genericCreateForAllMembers.Invoke(null, new object[]
{
includeInternals
});
}

public const string MimeType = "text/vt100";
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is probably not the correct name for this mime type, since VT-100 is really a subset of the ANSI escape sequences that might be used.


internal static readonly IFormatterSet DefaultFormatters = new DefaultTerminalFormatterSet();
}
}
85 changes: 85 additions & 0 deletions Microsoft.DotNet.Interactive.Rendering/TerminalFormatter{T}.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
// 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.Collections;
using System.CommandLine.Rendering.Views;
using System.IO;

namespace Microsoft.DotNet.Interactive.Rendering
{
public class TerminalFormatter<T> : TypeFormatter<T>
{
private readonly Action<T, TextWriter> _format;

public TerminalFormatter(Action<T, TextWriter> format)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Null check.

{
_format = format;
}

public override void Format(T instance, TextWriter writer)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Null check.

{
_format(instance, writer);
}

public override string MimeType => TerminalFormatter.MimeType;

public static ITypeFormatter<T> Create(bool includeInternals = false)
{
if (TerminalFormatter.DefaultFormatters.TryGetFormatterForType(typeof(T), out var formatter) &&
formatter is ITypeFormatter<T> ft)
{
return ft;
}

if (typeof(IEnumerable).IsAssignableFrom(typeof(T)))
{
return CreateForSequence(includeInternals);
}

return CreateForObject(includeInternals);
}

private static ITypeFormatter<T> CreateForObject(bool includeInternals)
{
var members = typeof(T).GetAllMembers(includeInternals)
.GetMemberAccessors<T>();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A better approach here would be to use the Destructurer class, which is what the HtmlFormatter uses to build up the table.


if (members.Length == 0)
{
return new TerminalFormatter<T>((value, writer) => writer.Write(value));
}

return new TerminalFormatter<T>((instance, writer) =>
{
var tableView = new TableView<object>();
foreach (var m in members)
{
tableView.AddColumn(_ => Value(m, instance), m.Member.Name);
}

throw new NotImplementedException();

});

}

private static ITypeFormatter<T> CreateForSequence(bool includeInternals)
{
throw new NotImplementedException();
}

private static string Value(MemberAccessor<T> m, T instance)
{
try
{
var value = m.GetValue(instance);
return value.ToDisplayString();
}
catch (Exception exception)
{
return exception.ToDisplayString(PlainTextFormatter.MimeType);
}
}
}
}