-
Notifications
You must be signed in to change notification settings - Fork 563
terminal formatter and mime type support #516
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| 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 | ||
| { | ||
|
|
||
| } | ||
| } |
| 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"; | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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(); | ||
| } | ||
| } | ||
| 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) | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Null check. |
||
| { | ||
| _format = format; | ||
| } | ||
|
|
||
| public override void Format(T instance, TextWriter writer) | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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>(); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. A better approach here would be to use the |
||
|
|
||
| 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); | ||
| } | ||
| } | ||
| } | ||
| } | ||
There was a problem hiding this comment.
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?