Skip to content
Merged
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
8 changes: 0 additions & 8 deletions docs/formatting.md
Original file line number Diff line number Diff line change
Expand Up @@ -116,14 +116,6 @@ The following global settings can be set to change formatting behaviors:

An optional type-specific list expansion limit

* `PlainTextFormatter.MaxProperties` = 20

Indicates the maximum number of properties to show in the default plaintext display of arbitrary objects. If set to zero no properties are shown.

* `HtmlFormatter.MaxProperties` = 20

Indicates the maximum number of properties to show in HTML table displays of arbitrary objects. If set to zero no properties are shown.

## HTML Formatting

### The `CSS` function
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,6 @@ Microsoft.DotNet.Interactive.Formatting
public class JsonString : Microsoft.AspNetCore.Html.HtmlString, Microsoft.AspNetCore.Html.IHtmlContent
.ctor(System.String json)
public static class PlainTextFormatter
public static System.Int32 MaxProperties { get; set;}
public static ITypeFormatter GetPreferredFormatterFor(System.Type type)
public static ITypeFormatter GetPreferredFormatterFor<T>()
public static System.Void WriteStartProperty(FormatContext context)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,67 +50,7 @@ public void It_emits_the_property_names_and_values_for_a_specific_type()

s.Should().Contain("Name: Bob");
}

[Fact]
public void It_emits_a_default_maximum_number_of_properties()
{
var formatter = PlainTextFormatter.GetPreferredFormatterFor<ClassWithManyProperties>();

var writer = new StringWriter();
formatter.Format(new ClassWithManyProperties(), writer);

var s = writer.ToString();
s.Should().Be(
@$"{nameof(ClassWithManyProperties)}
X1: 1
X2: 2
X3: 3
X4: 4
X5: 5
X6: 6
X7: 7
X8: 8
X9: 9
X10: 10
X11: 11
X12: 12
X13: 13
X14: 14
X15: 15
X16: 16
X17: 17
X18: 18
X19: 19
X20: 20
...".ReplaceLineEndings());
}

[Fact]
public void It_emits_a_configurable_maximum_number_of_properties()
{
var formatter = PlainTextFormatter.GetPreferredFormatterFor<ClassWithManyProperties>();
PlainTextFormatter.MaxProperties = 1;

var writer = new StringWriter();
formatter.Format(new ClassWithManyProperties(), writer);

var s = writer.ToString();
s.Should().Be($"ClassWithManyProperties{Environment.NewLine} X1: 1{Environment.NewLine} ...");
}

[Fact]
public void When_Zero_properties_chosen_just_ToString_is_used()
{
var formatter = PlainTextFormatter.GetPreferredFormatterFor<ClassWithManyPropertiesAndCustomToString>();
PlainTextFormatter.MaxProperties = 0;

var writer = new StringWriter();
formatter.Format(new ClassWithManyPropertiesAndCustomToString(), writer);

var s = writer.ToString();
s.Should().Be($"{typeof(ClassWithManyPropertiesAndCustomToString)} custom ToString value");
}


[Fact]
public void When_Zero_properties_available_to_choose_just_ToString_is_used()
{
Expand Down
25 changes: 4 additions & 21 deletions src/Microsoft.DotNet.Interactive.Formatting/PlainTextFormatter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,6 @@ namespace Microsoft.DotNet.Interactive.Formatting;

public static class PlainTextFormatter
{
static PlainTextFormatter()
{
Formatter.Clearing += Initialize;

void Initialize() => MaxProperties = DefaultMaxProperties;
}

public const string MimeType = "text/plain";

public static ITypeFormatter GetPreferredFormatterFor(Type type) =>
Expand All @@ -36,14 +29,6 @@ public static ITypeFormatter GetPreferredFormatterFor(Type type) =>
public static ITypeFormatter GetPreferredFormatterFor<T>() =>
GetPreferredFormatterFor(typeof(T));

/// <summary>
/// Indicates the maximum number of properties to show in the default plaintext display of arbitrary objects.
/// If set to zero no properties are shown.
/// </summary>
public static int MaxProperties { get; set; } = DefaultMaxProperties;

private const int DefaultMaxProperties = 20;

private const int NumberOfSpacesToIndent = 2;

internal static ITypeFormatter GetDefaultFormatterForAnyObject(Type type, bool includeInternals = false) =>
Expand Down Expand Up @@ -72,10 +57,8 @@ internal static FormatDelegate<T> CreateFormatDelegate<T>(MemberInfo[] forMember

bool FormatObject(T target, FormatContext context)
{
var reducedAccessors = accessors.Take(Math.Max(0, MaxProperties)).ToArray();

// If we haven't got any members to show, just resort to ToString()
if (reducedAccessors.Length == 0)
if (accessors.Length == 0)
{
context.Writer.Write(target.ToString());
return true;
Expand All @@ -90,9 +73,9 @@ bool FormatObject(T target, FormatContext context)
context.Writer.WriteLine();
}

for (var i = 0; i < reducedAccessors.Length; i++)
for (var i = 0; i < accessors.Length; i++)
{
var accessor = reducedAccessors[i];
var accessor = accessors[i];

object value = accessor.GetValueOrException(target);

Expand All @@ -107,7 +90,7 @@ bool FormatObject(T target, FormatContext context)
}
}

if (reducedAccessors.Length < accessors.Length)
if (accessors.Length < accessors.Length)
{
WriteIndent(context);
context.Writer.Write("...");
Expand Down