Browse Source

Implement Quote Formatting

Adds support for block quote text formatting. This feature is currently only implemented in the Canary client.
This formatting adds a "> " to each new line from the input text.
pull/1348/head
Chris Johnston 6 years ago
parent
commit
be7b5f9e0d
2 changed files with 55 additions and 0 deletions
  1. +38
    -0
      src/Discord.Net.Core/Format.cs
  2. +17
    -0
      test/Discord.Net.Tests.Unit/FormatTests.cs

+ 38
- 0
src/Discord.Net.Core/Format.cs View File

@@ -1,3 +1,5 @@
using System.Text;

namespace Discord namespace Discord
{ {
/// <summary> A helper class for formatting characters. </summary> /// <summary> A helper class for formatting characters. </summary>
@@ -37,5 +39,41 @@ namespace Discord
text = text.Replace(unsafeChar, $"\\{unsafeChar}"); text = text.Replace(unsafeChar, $"\\{unsafeChar}");
return text; return text;
} }

/// <summary>
/// Formats a string as a quote.
/// </summary>
/// <param name="text">The text to format.</param>
/// <returns>Gets the formatted quote text.</returns> // TODO: better xmldoc
public static string Quote(string text)
{
if (text == null)
return null;

StringBuilder result = new StringBuilder();

int startIndex = 0;
int newLineIndex;
do
{
newLineIndex = text.IndexOf('\n', startIndex);
if (newLineIndex == -1)
{
// read the rest of the string
var str = text.Substring(startIndex);
result.Append($"> {str}");
}
else
{
// read until the next newline
var str = text.Substring(startIndex, newLineIndex - startIndex);
result.Append($"> {str}\n");
}
startIndex = newLineIndex + 1;
}
while (newLineIndex != -1 && startIndex != text.Length);

return result.ToString();
}
} }
} }

+ 17
- 0
test/Discord.Net.Tests.Unit/FormatTests.cs View File

@@ -28,5 +28,22 @@ namespace Discord
Assert.Equal("```cs\ntest\n```", Format.Code("test", "cs")); Assert.Equal("```cs\ntest\n```", Format.Code("test", "cs"));
Assert.Equal("```cs\nanother\none\n```", Format.Code("another\none", "cs")); Assert.Equal("```cs\nanother\none\n```", Format.Code("another\none", "cs"));
} }
[Fact]
public void QuoteNullString()
{
Assert.Null(Format.Quote(null));
}
[Theory]
[InlineData("", "> ")]
[InlineData("\n", "> \n")]
[InlineData("\n ", "> \n> ")]
[InlineData("input", "> input")] // single line
// should work with CR or CRLF
[InlineData("inb4\ngreentext", "> inb4\n> greentext")]
[InlineData("inb4\r\ngreentext", "> inb4\r\n> greentext")]
public void Quote(string input, string expected)
{
Assert.Equal(expected, Format.Quote(input));
}
} }
} }

Loading…
Cancel
Save