teamlead-dashboard/src/TUI.Engine/Extensions.cs

57 lines
1.6 KiB
C#
Raw Normal View History

2023-08-29 04:40:28 +00:00
using System.Globalization;
2024-03-17 19:57:31 +00:00
using System.Text;
2023-08-03 19:21:06 +00:00
using System.Text.RegularExpressions;
2024-03-15 19:13:09 +00:00
namespace TUI.Engine;
2023-08-03 19:21:06 +00:00
public static class Extensions
{
2024-03-15 19:13:09 +00:00
public static int Max(this int value, int maxValue)
{
return value <= maxValue ? value : maxValue;
}
public static int Min(this int value, int minValue)
{
return value > minValue ? value : minValue;
}
2023-08-03 19:21:06 +00:00
public static bool Have(this IEnumerable<string> array, string findValue)
{
return array.Any(item => item == findValue);
}
2024-03-17 19:57:31 +00:00
public static string Repeat(this string value, int count)
2023-08-03 19:21:06 +00:00
{
2024-03-17 19:57:31 +00:00
return count < 0 ? string.Empty : new StringBuilder(value.Length * count).Insert(0, value, count).ToString();
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
2024-03-08 09:41:29 +00:00
public static int GetWidth(this string text)
2023-08-03 19:21:06 +00:00
{
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
public static string RemoveVersionPrefix(this string version)
{
return version.Replace("^", "").Replace("v", "").Replace("~", "");
}
2024-03-06 21:49:54 +00:00
public static Version ToVersion(this string textVersion)
2023-08-06 20:12:22 +00:00
{
var version = textVersion.RemoveVersionPrefix().Split(".");
2023-08-06 20:12:22 +00:00
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);
}
2024-12-06 14:30:58 +00:00
}