Skip to content

Commit

Permalink
add CRLF encoding and configurable text clipping
Browse files Browse the repository at this point in the history
  • Loading branch information
goblinfactory committed Dec 26, 2020
1 parent 0fd4403 commit 8cd2da3
Show file tree
Hide file tree
Showing 9 changed files with 61 additions and 27 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,9 @@ new PrettyPrinter(PrettyConfig.CreateDefault()).PrintJson(myData);
- Configuration is `per printer`, and not global.
- Useful `.Dump()` extensions for objects and strings that will dump the object or json to console.
- Configurable styling based on property name.
- Automatic clipping of strings longer than 200 characters. (Configurable in Config.)

![dark background theme](docs/dark-background-style.PNG)
![dark background theme](docs/dark-background-style2.PNG)

## Automatically detect dark and light background

Expand Down Expand Up @@ -102,7 +103,6 @@ printer.PrintJson(data);

![easy read setting](docs/easy-read.PNG)


## Limitations and final thoughts

<img align="left" width="300" margin="5px" src="docs/easy-read-2.PNG">
Expand Down
Binary file removed docs/dark-background-style.PNG
Binary file not shown.
Binary file added docs/dark-background-style2.PNG
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,11 @@ static void Main(string[] args)
// change background to white, pretty printer will use a white background style
BackgroundColor = White;
Clear();

// disable clipping
printer.Config.ClipStringMaxLen = null;
printer.PrintJson(data);

ReadLine();

BackgroundColor = Black;
Expand Down Expand Up @@ -85,12 +89,22 @@ private static object GetData()
{
return new
{
someObject = new
demo_object = new
{
name = "example " + '\\' + " " + '"' + "quoted" + '"' + " / 'text'",
LongTextFieldWithQuotesAndSlashes = "quotes string example " + '\\' + " " + '"' + "hasquotes" + '"' + " / 'text'",
data = new
{
shortString = "S",
longString =
@" I'm not wearing hockey pads. No guns, no killing. Does it come in black?
It's not who I am underneath but what I do that defines me.
Accomplice ? I'm gonna tell them the whole thing was your idea. The first time I stole so that I wouldn't starve,
yes.I lost many assumptions about the simple nature of right and wrong.And when I traveled I learned the fear before a crime and the thrill of success.But I never became one of them.
I seek the means to fight injustice.To turn fear against those who prey on the fearful.It's ends here. The first time I stole so that I wouldn't starve,
yes.I lost many assumptions about the simple nature of right and wrong.And when I traveled I learned the fear before a crime and the thrill of success.But I never became one of them.",
nested = new
{
nested = new
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ namespace Goblinfactory.PrettyJson
{
public interface IPrettyConfig
{
/// <summary>
/// set to null to disable clipping of strings.
/// </summary>
int? ClipStringMaxLen { get; set; }
int EasyReadPropWidth { get; set; }
bool EasyRead { get; set; }
IPrettyStyle LightStyle { get; set; }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,17 +38,20 @@ public class PrettyConfig : IPrettyConfig
// if you override the default serializer please remember to ensure that the json
// is pretty printed, ie indented, not single line.
public IPrettyJsonSerializer Serializer { get; set; } = new DefaultMicrosoftSerializer();

public static PrettyConfig CreateDefault()
{
return new PrettyConfig();
}

public IPrettyStyle LightStyle { get; set; } = PrettyStyle.LightStyle;
public IPrettyStyle DarkStyle { get; set; } = PrettyStyle.DarkStyle;

public bool EasyRead { get; set; } = false;
public int EasyReadPropWidth { get; set; } = 10;

/// <summary>
/// set to null to disable clipping of strings.
/// </summary>
public int? ClipStringMaxLen { get; set; } = 200;

public static PrettyConfig CreateDefault()
{
return new PrettyConfig();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -180,14 +180,14 @@ private void _Print(string json)
{
if (isArray) printWithMargin("");
ForegroundColor = GetStringColor(style, propName);
Write(reader.GetString().ToJsonEncode());
Write(reader.GetString().ToJsonEncode(Config));
ForegroundColor = style.Quotes;
}
else
{
if (isArray) printWithMargin("\""); else Write("\"");
ForegroundColor = GetStringColor(style, propName);
Write(reader.GetString().ToJsonEncode());
Write(reader.GetString().ToJsonEncode(Config));
ForegroundColor = style.Quotes;
Write("\"");
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
namespace Goblinfactory.PrettyJson.Internal
{
internal static class StringExtensions
{
// this does not do url encoding
// further reading
// https://coder.today/tech/2017-10-20_encoding-in-web-development.-why-how-url-json-base64-beyond/
// and https://www.ietf.org/rfc/rfc4627.txt
public static string ToJsonEncode(this string src, IPrettyConfig config)
{
return src
.Replace(@"\", @"\\")
.Replace("\"", "\\\"")
.Replace("\n", "\\n")
.Replace("\r", "\\r")
.Clip(config.ClipStringMaxLen);
}

public static string Clip(this string src, int? maxLen)
{
if (maxLen == null) return src;
if (src == null) return null;
if (src.Length <= maxLen) return src;
if(maxLen<10) return src.Substring(0, maxLen.Value);
return $"{src.Substring(0, maxLen.Value - 3)}...";
}
}
}

0 comments on commit 8cd2da3

Please sign in to comment.