mirror of
https://github.com/nsnail/spectre.console.git
synced 2025-06-19 13:28:16 +08:00
Add net5.0 target framework
This commit is contained in:

committed by
Patrik Svensson

parent
b1da5e7ba8
commit
380c6aca45
27
src/Spectre.Console/Extensions/DayOfWeekExtensions.cs
Normal file
27
src/Spectre.Console/Extensions/DayOfWeekExtensions.cs
Normal file
@ -0,0 +1,27 @@
|
||||
using System;
|
||||
using System.Globalization;
|
||||
|
||||
namespace Spectre.Console.Internal
|
||||
{
|
||||
internal static class DayOfWeekExtensions
|
||||
{
|
||||
public static string GetAbbreviatedDayName(this DayOfWeek day, CultureInfo culture)
|
||||
{
|
||||
culture ??= CultureInfo.InvariantCulture;
|
||||
return culture.DateTimeFormat
|
||||
.GetAbbreviatedDayName(day)
|
||||
.Capitalize(culture);
|
||||
}
|
||||
|
||||
public static DayOfWeek GetNextWeekDay(this DayOfWeek day)
|
||||
{
|
||||
var next = (int)day + 1;
|
||||
if (next > (int)DayOfWeek.Saturday)
|
||||
{
|
||||
return DayOfWeek.Sunday;
|
||||
}
|
||||
|
||||
return (DayOfWeek)next;
|
||||
}
|
||||
}
|
||||
}
|
13
src/Spectre.Console/Extensions/DictionaryExtensions.cs
Normal file
13
src/Spectre.Console/Extensions/DictionaryExtensions.cs
Normal file
@ -0,0 +1,13 @@
|
||||
using System.Collections.Generic;
|
||||
|
||||
namespace Spectre.Console.Internal
|
||||
{
|
||||
internal static class DictionaryExtensions
|
||||
{
|
||||
public static void Deconstruct<T1, T2>(this KeyValuePair<T1, T2> tuple, out T1 key, out T2 value)
|
||||
{
|
||||
key = tuple.Key;
|
||||
value = tuple.Value;
|
||||
}
|
||||
}
|
||||
}
|
87
src/Spectre.Console/Extensions/EnumerableExtensions.cs
Normal file
87
src/Spectre.Console/Extensions/EnumerableExtensions.cs
Normal file
@ -0,0 +1,87 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
|
||||
namespace Spectre.Console.Internal
|
||||
{
|
||||
internal static class EnumerableExtensions
|
||||
{
|
||||
public static int GetCount<T>(this IEnumerable<T> source)
|
||||
{
|
||||
if (source is IList<T> list)
|
||||
{
|
||||
return list.Count;
|
||||
}
|
||||
|
||||
if (source is T[] array)
|
||||
{
|
||||
return array.Length;
|
||||
}
|
||||
|
||||
return source.Count();
|
||||
}
|
||||
|
||||
public static void ForEach<T>(this IEnumerable<T> source, Action<T> action)
|
||||
{
|
||||
foreach (var item in source)
|
||||
{
|
||||
action(item);
|
||||
}
|
||||
}
|
||||
|
||||
public static bool AnyTrue(this IEnumerable<bool> source)
|
||||
{
|
||||
return source.Any(b => b);
|
||||
}
|
||||
|
||||
public static IEnumerable<(int Index, bool First, bool Last, T Item)> Enumerate<T>(this IEnumerable<T> source)
|
||||
{
|
||||
if (source is null)
|
||||
{
|
||||
throw new ArgumentNullException(nameof(source));
|
||||
}
|
||||
|
||||
return Enumerate(source.GetEnumerator());
|
||||
}
|
||||
|
||||
public static IEnumerable<(int Index, bool First, bool Last, T Item)> Enumerate<T>(this IEnumerator<T> source)
|
||||
{
|
||||
if (source is null)
|
||||
{
|
||||
throw new ArgumentNullException(nameof(source));
|
||||
}
|
||||
|
||||
var first = true;
|
||||
var last = !source.MoveNext();
|
||||
T current;
|
||||
|
||||
for (var index = 0; !last; index++)
|
||||
{
|
||||
current = source.Current;
|
||||
last = !source.MoveNext();
|
||||
yield return (index, first, last, current);
|
||||
first = false;
|
||||
}
|
||||
}
|
||||
|
||||
public static IEnumerable<TResult> SelectIndex<T, TResult>(this IEnumerable<T> source, Func<T, int, TResult> func)
|
||||
{
|
||||
return source.Select((value, index) => func(value, index));
|
||||
}
|
||||
|
||||
#if !NET5_0
|
||||
public static IEnumerable<(TFirst First, TSecond Second)> Zip<TFirst, TSecond>(
|
||||
this IEnumerable<TFirst> source, IEnumerable<TSecond> first)
|
||||
{
|
||||
return source.Zip(first, (first, second) => (first, second));
|
||||
}
|
||||
#endif
|
||||
|
||||
public static IEnumerable<(TFirst First, TSecond Second, TThird Third)> Zip<TFirst, TSecond, TThird>(
|
||||
this IEnumerable<TFirst> first, IEnumerable<TSecond> second, IEnumerable<TThird> third)
|
||||
{
|
||||
return first.Zip(second, (a, b) => (a, b))
|
||||
.Zip(third, (a, b) => (a.a, a.b, b));
|
||||
}
|
||||
}
|
||||
}
|
@ -1,3 +1,11 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Globalization;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using Spectre.Console.Internal;
|
||||
using Spectre.Console.Rendering;
|
||||
|
||||
namespace Spectre.Console
|
||||
{
|
||||
/// <summary>
|
||||
@ -5,6 +13,11 @@ namespace Spectre.Console
|
||||
/// </summary>
|
||||
public static class StringExtensions
|
||||
{
|
||||
// Cache whether or not internally normalized line endings
|
||||
// already are normalized. No reason to do yet another replace if it is.
|
||||
private static readonly bool _alreadyNormalized
|
||||
= Environment.NewLine.Equals("\n", StringComparison.OrdinalIgnoreCase);
|
||||
|
||||
/// <summary>
|
||||
/// Escapes text so that it won’t be interpreted as markup.
|
||||
/// </summary>
|
||||
@ -18,8 +31,137 @@ namespace Spectre.Console
|
||||
}
|
||||
|
||||
return text
|
||||
.Replace("[", "[[")
|
||||
.Replace("]", "]]");
|
||||
.ReplaceExact("[", "[[")
|
||||
.ReplaceExact("]", "]]");
|
||||
}
|
||||
|
||||
internal static int CellLength(this string text, RenderContext context)
|
||||
{
|
||||
if (context is null)
|
||||
{
|
||||
throw new ArgumentNullException(nameof(context));
|
||||
}
|
||||
|
||||
return Cell.GetCellLength(context, text);
|
||||
}
|
||||
|
||||
internal static string Capitalize(this string text, CultureInfo? culture = null)
|
||||
{
|
||||
if (text == null)
|
||||
{
|
||||
return string.Empty;
|
||||
}
|
||||
|
||||
culture ??= CultureInfo.InvariantCulture;
|
||||
|
||||
if (text.Length > 0 && char.IsLower(text[0]))
|
||||
{
|
||||
text = string.Format(culture, "{0}{1}", char.ToUpper(text[0], culture), text.Substring(1));
|
||||
}
|
||||
|
||||
return text;
|
||||
}
|
||||
|
||||
internal static string NormalizeLineEndings(this string? text, bool native = false)
|
||||
{
|
||||
text = text?.ReplaceExact("\r\n", "\n");
|
||||
text = text?.ReplaceExact("\r", string.Empty);
|
||||
text ??= string.Empty;
|
||||
|
||||
if (native && !_alreadyNormalized)
|
||||
{
|
||||
text = text.ReplaceExact("\n", Environment.NewLine);
|
||||
}
|
||||
|
||||
return text;
|
||||
}
|
||||
|
||||
internal static string[] SplitLines(this string text)
|
||||
{
|
||||
var result = text?.NormalizeLineEndings()?.Split(new[] { '\n' }, StringSplitOptions.None);
|
||||
return result ?? Array.Empty<string>();
|
||||
}
|
||||
|
||||
internal static string[] SplitWords(this string word, StringSplitOptions options = StringSplitOptions.None)
|
||||
{
|
||||
var result = new List<string>();
|
||||
|
||||
static string Read(StringBuffer reader, Func<char, bool> criteria)
|
||||
{
|
||||
var buffer = new StringBuilder();
|
||||
while (!reader.Eof)
|
||||
{
|
||||
var current = reader.Peek();
|
||||
if (!criteria(current))
|
||||
{
|
||||
break;
|
||||
}
|
||||
|
||||
buffer.Append(reader.Read());
|
||||
}
|
||||
|
||||
return buffer.ToString();
|
||||
}
|
||||
|
||||
using (var reader = new StringBuffer(word))
|
||||
{
|
||||
while (!reader.Eof)
|
||||
{
|
||||
var current = reader.Peek();
|
||||
if (char.IsWhiteSpace(current))
|
||||
{
|
||||
var x = Read(reader, c => char.IsWhiteSpace(c));
|
||||
if (options != StringSplitOptions.RemoveEmptyEntries)
|
||||
{
|
||||
result.Add(x);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
result.Add(Read(reader, c => !char.IsWhiteSpace(c)));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return result.ToArray();
|
||||
}
|
||||
|
||||
internal static string Repeat(this string text, int count)
|
||||
{
|
||||
if (text is null)
|
||||
{
|
||||
throw new ArgumentNullException(nameof(text));
|
||||
}
|
||||
|
||||
if (count <= 0)
|
||||
{
|
||||
return string.Empty;
|
||||
}
|
||||
|
||||
if (count == 1)
|
||||
{
|
||||
return text;
|
||||
}
|
||||
|
||||
return string.Concat(Enumerable.Repeat(text, count));
|
||||
}
|
||||
|
||||
internal static string ReplaceExact(this string text, string oldValue, string? newValue)
|
||||
{
|
||||
#if NET5_0
|
||||
return text.Replace(oldValue, newValue, StringComparison.Ordinal);
|
||||
#else
|
||||
return text.Replace(oldValue, newValue);
|
||||
#endif
|
||||
}
|
||||
|
||||
internal static bool ContainsExact(this string text, string value)
|
||||
{
|
||||
#if NET5_0
|
||||
return text.Contains(value, StringComparison.Ordinal);
|
||||
#else
|
||||
return text.Contains(value);
|
||||
#endif
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -1,4 +1,5 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
|
||||
namespace Spectre.Console
|
||||
{
|
||||
@ -87,5 +88,26 @@ namespace Spectre.Console
|
||||
decoration: style.Decoration,
|
||||
link: link);
|
||||
}
|
||||
|
||||
internal static Style Combine(this Style style, IEnumerable<Style> source)
|
||||
{
|
||||
if (style is null)
|
||||
{
|
||||
throw new ArgumentNullException(nameof(style));
|
||||
}
|
||||
|
||||
if (source is null)
|
||||
{
|
||||
return style;
|
||||
}
|
||||
|
||||
var current = style;
|
||||
foreach (var item in source)
|
||||
{
|
||||
current = current.Combine(item);
|
||||
}
|
||||
|
||||
return current;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
21
src/Spectre.Console/Extensions/TextWriterExtensions.cs
Normal file
21
src/Spectre.Console/Extensions/TextWriterExtensions.cs
Normal file
@ -0,0 +1,21 @@
|
||||
using System.Diagnostics.CodeAnalysis;
|
||||
using System.IO;
|
||||
|
||||
namespace Spectre.Console.Internal
|
||||
{
|
||||
internal static class TextWriterExtensions
|
||||
{
|
||||
[SuppressMessage("Design", "CA1031:Do not catch general exception types")]
|
||||
public static bool IsStandardOut(this TextWriter writer)
|
||||
{
|
||||
try
|
||||
{
|
||||
return writer == System.Console.Out;
|
||||
}
|
||||
catch
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user