Skip to content

Commit

Permalink
imp - Used wide strings for markups
Browse files Browse the repository at this point in the history
---

You can now use wide strings for markups.

---

Type: imp
Breaking: False
Doc Required: False
Backport Required: False
Part: 1/1
  • Loading branch information
AptiviCEO committed Nov 18, 2024
1 parent 8c02821 commit 1bfaa28
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 9 deletions.
22 changes: 19 additions & 3 deletions Terminaux/Writer/CyclicWriters/Renderer/Markup/Mark.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,19 +17,21 @@
// along with this program. If not, see <https://www.gnu.org/licenses/>.
//

using Textify.General.Structures;

namespace Terminaux.Writer.CyclicWriters.Renderer.Markup
{
/// <summary>
/// String with console markup support
/// </summary>
public class Mark
{
private string markup = "";
private WideString markup = (WideString)"";

/// <summary>
/// Markup representation
/// </summary>
public string Markup
public WideString Markup
{
get => markup;
set
Expand All @@ -46,6 +48,20 @@ public string Markup
public string ParseMarkup() =>
MarkupTools.ParseMarkup(Markup);

/// <summary>
/// Makes a <see cref="Mark"/> instance from a string implicitly
/// </summary>
/// <param name="markup">Markup representation</param>
public static implicit operator Mark(string markup) =>
new(markup);

/// <summary>
/// Makes a <see cref="Mark"/> instance from a wide string implicitly
/// </summary>
/// <param name="markup">Markup representation</param>
public static implicit operator Mark(WideString markup) =>
new(markup);

/// <summary>
/// Constructor without any argument to create a markup representation class without text
/// </summary>
Expand All @@ -58,7 +74,7 @@ public Mark()
/// <param name="markup">Markup representation of a text</param>
public Mark(string markup)
{
Markup = markup;
Markup = (WideString)markup;
}
}
}
35 changes: 29 additions & 6 deletions Terminaux/Writer/CyclicWriters/Renderer/Markup/MarkupTools.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
using Terminaux.Writer.CyclicWriters.Renderer.Effects;
using Terminaux.Writer.CyclicWriters.Renderer.Effects.Builtins;
using Textify.General;
using Textify.General.Structures;

namespace Terminaux.Writer.CyclicWriters.Renderer.Markup
{
Expand Down Expand Up @@ -72,7 +73,18 @@ public static string ParseMarkup(Mark? mark, Color? foregroundColor = null, Colo
/// <param name="backgroundColor">Background color of the initial markup color. Overrides the initial format argument.</param>
/// <param name="initialFormat">Initial format. Overrides the foreground and the background color arguments.</param>
/// <returns>A string that can be written to the console with the parsed markup representations</returns>
public static string ParseMarkup(string markup, Color? foregroundColor = null, Color? backgroundColor = null, string initialFormat = "")
public static string ParseMarkup(string markup, Color? foregroundColor = null, Color? backgroundColor = null, string initialFormat = "") =>
ParseMarkup((WideString)markup, foregroundColor, backgroundColor, initialFormat);

/// <summary>
/// Parses the markup representation
/// </summary>
/// <param name="markup">Markup representation</param>
/// <param name="foregroundColor">Foreground color of the initial markup color. Overrides the initial format argument.</param>
/// <param name="backgroundColor">Background color of the initial markup color. Overrides the initial format argument.</param>
/// <param name="initialFormat">Initial format. Overrides the foreground and the background color arguments.</param>
/// <returns>A string that can be written to the console with the parsed markup representations</returns>
public static string ParseMarkup(WideString markup, Color? foregroundColor = null, Color? backgroundColor = null, string initialFormat = "")
{
// Markup is inspired by BBCode and can have escaped starting and ending sequence characters.
var finalResult = new StringBuilder(markup);
Expand All @@ -83,8 +95,8 @@ public static string ParseMarkup(string markup, Color? foregroundColor = null, C
int nestLevel = -1;
for (int i = 0; i < markup.Length; i++)
{
char markupChar = markup[i];
char nextChar = i + 1 < markup.Length ? markup[i + 1] : '\0';
WideChar markupChar = markup[i];
WideChar nextChar = i + 1 < markup.Length ? markup[i + 1] : (WideChar)'\0';

// Check to see if we are escaping or not
if (((markupChar == '[' && nextChar == '[') || (markupChar == ']' && nextChar == ']')) && queuedSequences.Count == 0)
Expand Down Expand Up @@ -117,7 +129,7 @@ public static string ParseMarkup(string markup, Color? foregroundColor = null, C
}
else if (markupChar == '[' && nextChar == '/' && queuedSequences.Count > 0)
{
char nextTwoChar = i + 2 < markup.Length ? markup[i + 2] : '\0';
WideChar nextTwoChar = i + 2 < markup.Length ? markup[i + 2] : (WideChar)'\0';
if (nextTwoChar != ']')
throw new TerminauxException("Invalid end tag specifier.");
if (queuedSequences.Count > 0)
Expand All @@ -139,7 +151,7 @@ public static string ParseMarkup(string markup, Color? foregroundColor = null, C
// Add a character
if (append)
{
appended.Append(markupChar);
appended.Append((string)markupChar);
if (markupChar == ']' && queuedSequences.Count > 0)
{
queuedSequences[queuedSequences.Count - 1].represent = appended.ToString();
Expand Down Expand Up @@ -235,7 +247,18 @@ public static bool TryParseMarkup(Mark? mark, Color? foregroundColor = null, Col
/// <param name="backgroundColor">Background color of the initial markup color. Overrides the initial format argument.</param>
/// <param name="initialFormat">Initial format. Overrides the foreground and the background color arguments.</param>
/// <returns>A string that can be written to the console with the parsed markup representations</returns>
public static bool TryParseMarkup(string markup, Color? foregroundColor = null, Color? backgroundColor = null, string initialFormat = "")
public static bool TryParseMarkup(string markup, Color? foregroundColor = null, Color? backgroundColor = null, string initialFormat = "") =>
TryParseMarkup((WideString)markup, foregroundColor, backgroundColor, initialFormat);

/// <summary>
/// Parses the markup representation
/// </summary>
/// <param name="markup">Markup representation</param>
/// <param name="foregroundColor">Foreground color of the initial markup color. Overrides the initial format argument.</param>
/// <param name="backgroundColor">Background color of the initial markup color. Overrides the initial format argument.</param>
/// <param name="initialFormat">Initial format. Overrides the foreground and the background color arguments.</param>
/// <returns>A string that can be written to the console with the parsed markup representations</returns>
public static bool TryParseMarkup(WideString markup, Color? foregroundColor = null, Color? backgroundColor = null, string initialFormat = "")
{
try
{
Expand Down

0 comments on commit 1bfaa28

Please sign in to comment.