Skip to content

Commit

Permalink
chore: Removed experimental namespace
Browse files Browse the repository at this point in the history
  • Loading branch information
Snappey committed May 29, 2024
1 parent 690f374 commit 0599256
Show file tree
Hide file tree
Showing 7 changed files with 124 additions and 1 deletion.
17 changes: 17 additions & 0 deletions .idea/.idea.D2.Net/.idea/aws.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions .idea/.idea.D2.Net/.idea/indexLayout.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions .idea/.idea.D2.Net/.idea/projectSettingsUpdater.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions .idea/.idea.D2.Net/.idea/vcs.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions D2.Net.sln.DotSettings.user
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<wpf:ResourceDictionary xml:space="preserve" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:s="clr-namespace:System;assembly=mscorlib" xmlns:ss="urn:shemas-jetbrains-com:settings-storage-xaml" xmlns:wpf="http://schemas.microsoft.com/winfx/2006/xaml/presentation">
<s:String x:Key="/Default/Environment/UnitTesting/UnitTestSessionStore/Sessions/=142944d0_002Ded1d_002D4058_002D9ccf_002Dba061051a5fb/@EntryIndexedValue">&lt;SessionState ContinuousTestingMode="0" IsActive="True" Name="All tests from Solution" xmlns="urn:schemas-jetbrains-com:jetbrains-ut-session"&gt;&#xD;
&lt;Solution /&gt;&#xD;
&lt;/SessionState&gt;</s:String></wpf:ResourceDictionary>
83 changes: 83 additions & 0 deletions D2/Writers/D2Writer.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
using System.Collections.Generic;
using System.Text;

namespace D2.Writers
{
public class D2Writer
{
private readonly StringBuilder _sb = new StringBuilder();

public override string ToString() => _sb.ToString();

private void NewLine() => _sb.AppendLine();
private void Tab() => _sb.Append('\t');
private void Tab(int count) => _sb.Append('\t', count);

private const string LABEL_SEPARATOR = ": ";
private const string TAB_CHAR = " ";
private StringBuilder AppendShape(string key) => _sb.AppendLine(key);
private StringBuilder AppendShape(string key, string label) => _sb.AppendLine(key + LABEL_SEPARATOR + label);

// Basic Elements
public void DeclareShape(string key) => AppendShape(key);
public void DeclareShape(string key, string label) => AppendShape(key, label);

// Connections
private const string CONNECTION = " -- ";
private const string CONNECTION_BIDIRECTIONAL = " <-> ";
private const string CONNECTION_FROM = " <- ";
private const string CONNECTION_TO = " -> ";

private static string BuildConnection(string connection, IEnumerable<string> keys) => string.Join(connection, keys);
private void AppendConnection(IEnumerable<string> keys, string connection, string? label, Dictionary<string, string>? attributes)
{
if (string.IsNullOrWhiteSpace(label))
_sb.AppendLine(BuildConnection(connection, keys));
else
AppendShape(BuildConnection(connection, keys), label);

if (attributes != null && attributes.Count > 0)
{
WriteContainer(attributes);
}
}

public void DeclareConnection(IEnumerable<string> keys) => AppendConnection(keys, CONNECTION, null, null);
public void DeclareConnection(string label, IEnumerable<string> keys) => AppendConnection(keys, CONNECTION, label, null);
public void DeclareConnection(string label, IEnumerable<string> keys, Dictionary<string, string> attributes) => AppendConnection(keys, CONNECTION, label, attributes);

public void DeclareToConnection(IEnumerable<string> keys) => AppendConnection(keys, CONNECTION_TO, null, null);
public void DeclareToConnection(string label, IEnumerable<string> keys) => AppendConnection(keys, CONNECTION_TO, label, null);
public void DeclareToConnection(string label, IEnumerable<string> keys, Dictionary<string, string> attributes) => AppendConnection(keys, CONNECTION_TO, label, attributes);

public void DeclareFromConnection(IEnumerable<string> keys) => AppendConnection(keys, CONNECTION_FROM, null, null);
public void DeclareFromConnection(string label, IEnumerable<string> keys) => AppendConnection(keys, CONNECTION_FROM, label, null);
public void DeclareFromConnection(string label, IEnumerable<string> keys, Dictionary<string, string> attributes) => AppendConnection(keys, CONNECTION_FROM, label, attributes);

public void DeclareBidirectionalConnection(IEnumerable<string> keys) => AppendConnection(keys, CONNECTION_BIDIRECTIONAL, null, null);
public void DeclareBidirectionalConnection(string label, IEnumerable<string> keys) => AppendConnection(keys, CONNECTION_BIDIRECTIONAL, label, null);
public void DeclareBidirectionalConnection(string label, IEnumerable<string> keys, Dictionary<string, string> attributes) => AppendConnection(keys, CONNECTION_BIDIRECTIONAL, label, attributes);

// Containers
private const string OPEN_CONTAINER = "{";
private const string CLOSE_CONTAINER = "}";

private void WriteContainer(Dictionary<string, string> attributes)
{
_sb.AppendLine(OPEN_CONTAINER);
foreach (var (key, value) in attributes)
{
_sb.AppendLine(TAB_CHAR + key + LABEL_SEPARATOR + value);
}
_sb.AppendLine(CLOSE_CONTAINER);
}

private void WriteContainerInline(string prefix, Dictionary<string, string> attributes)
{
foreach (var (key, value) in attributes)
{
_sb.AppendLine(prefix + "." + key + LABEL_SEPARATOR + value);
}
}
}
}
1 change: 0 additions & 1 deletion Examples/Program.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
using D2;
using D2.Enums;
using D2.Writers;

var A = new Shape("A", "Starting Point");
var B = new Shape("B", "Ending Point");
Expand Down

0 comments on commit 0599256

Please sign in to comment.