teamlead-dashboard/src/TUI/Extensions.cs

43 lines
1.2 KiB
C#
Raw Normal View History

2023-08-29 04:40:28 +00:00
using System.Globalization;
2023-08-03 19:21:06 +00:00
using System.Text.RegularExpressions;
namespace TUI;
public static class Extensions
{
public static bool Have(this IEnumerable<string> array, string findValue)
{
return array.Any(item => item == findValue);
}
public static string Repeat(this char symbol, int repeatCount)
{
2023-08-08 04:28:20 +00:00
return repeatCount < 0 ? "" : new string(symbol, repeatCount);
2023-08-03 19:21:06 +00:00
}
2023-08-10 13:55:22 +00:00
public static string RemoveColors(this string text)
{
2023-08-29 04:40:28 +00:00
return Regex.Replace(text, @"\S\[(\d{0,3}[;m]_?){0,5}", "");
2023-08-10 13:55:22 +00:00
}
2023-08-29 04:40:28 +00:00
2023-08-03 19:21:06 +00:00
public static int Width(this string text)
{
2023-08-29 04:40:28 +00:00
if (string.IsNullOrEmpty(text)) return 0;
2023-08-06 20:12:22 +00:00
2023-08-10 13:55:22 +00:00
var clearText = text.RemoveColors();
2023-08-29 04:40:28 +00:00
var stringInfo = new StringInfo(clearText);
2023-08-03 19:21:06 +00:00
return stringInfo.LengthInTextElements;
}
2023-08-06 20:12:22 +00:00
2023-08-08 04:28:20 +00:00
public static Version? ToVersion(this string textVersion)
2023-08-06 20:12:22 +00:00
{
var version = textVersion.Replace("^", "").Replace("~", "").Split(".");
if (version.Length != 3)
return null;
var major = Convert.ToInt32(version[0]);
var minor = Convert.ToInt32(version[1]);
var patch = Convert.ToInt32(version[2].Split('-')[0]);
return new Version(major, minor, patch);
}
2023-08-03 19:21:06 +00:00
}