Add support for remapping emojis

Closes #94
This commit is contained in:
Patrik Svensson
2020-10-07 17:26:16 +02:00
committed by Patrik Svensson
parent 68e92f3365
commit ae92c606bb
4 changed files with 71 additions and 4 deletions

View File

@ -1,3 +1,5 @@
using System;
using System.Collections.Generic;
using System.Text.RegularExpressions;
namespace Spectre.Console
@ -8,6 +10,35 @@ namespace Spectre.Console
public static partial class Emoji
{
private static readonly Regex _emojiCode = new Regex(@"(:(\S*?):)", RegexOptions.Compiled);
private static readonly Dictionary<string, string> _remappings;
static Emoji()
{
_remappings = new Dictionary<string, string>(StringComparer.OrdinalIgnoreCase);
}
/// <summary>
/// Remaps a specific emoji tag with a new emoji.
/// </summary>
/// <param name="tag">The emoji tag.</param>
/// <param name="emoji">The emoji.</param>
public static void Remap(string tag, string emoji)
{
if (tag is null)
{
throw new ArgumentNullException(nameof(tag));
}
if (emoji is null)
{
throw new ArgumentNullException(nameof(emoji));
}
tag = tag.TrimStart(':').TrimEnd(':');
emoji = emoji.TrimStart(':').TrimEnd(':');
_remappings[tag] = emoji;
}
/// <summary>
/// Replaces emoji markup with corresponding unicode characters.
@ -19,6 +50,12 @@ namespace Spectre.Console
static string ReplaceEmoji(Match match)
{
var key = match.Groups[2].Value;
if (_remappings.Count > 0 && _remappings.TryGetValue(key, out var remappedEmoji))
{
return remappedEmoji;
}
if (_emojis.TryGetValue(key, out var emoji))
{
return emoji;