mirror of
https://github.com/nsnail/spectre.console.git
synced 2025-07-01 18:38:16 +08:00
Move Spectre.Console.Cli to it's own package
This commit is contained in:

committed by
Patrik Svensson

parent
b600832e00
commit
36ca22ffac
@ -0,0 +1,37 @@
|
||||
namespace Spectre.Console.Cli;
|
||||
|
||||
internal static class AnsiConsoleExtensions
|
||||
{
|
||||
private static readonly Lazy<IAnsiConsole> _console;
|
||||
|
||||
static AnsiConsoleExtensions()
|
||||
{
|
||||
_console = new Lazy<IAnsiConsole>(() => AnsiConsole.Console);
|
||||
}
|
||||
|
||||
public static IAnsiConsole GetConsole(this IAnsiConsole? console)
|
||||
{
|
||||
return console ?? _console.Value;
|
||||
}
|
||||
|
||||
public static void SafeRender(this IAnsiConsole? console, IRenderable? renderable)
|
||||
{
|
||||
if (renderable != null)
|
||||
{
|
||||
console ??= _console.Value;
|
||||
console.Write(renderable);
|
||||
}
|
||||
}
|
||||
|
||||
public static void SafeRender(this IAnsiConsole? console, IEnumerable<IRenderable?> renderables)
|
||||
{
|
||||
console ??= _console.Value;
|
||||
foreach (var renderable in renderables)
|
||||
{
|
||||
if (renderable != null)
|
||||
{
|
||||
console.Write(renderable);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,32 @@
|
||||
namespace Spectre.Console.Cli;
|
||||
|
||||
internal static class CaseSensitivityExtensions
|
||||
{
|
||||
public static StringComparison GetStringComparison(this CaseSensitivity caseSensitivity, CommandPart part)
|
||||
{
|
||||
if (part == CommandPart.CommandName && (caseSensitivity & CaseSensitivity.Commands) == 0)
|
||||
{
|
||||
return StringComparison.OrdinalIgnoreCase;
|
||||
}
|
||||
else if (part == CommandPart.LongOption && (caseSensitivity & CaseSensitivity.LongOptions) == 0)
|
||||
{
|
||||
return StringComparison.OrdinalIgnoreCase;
|
||||
}
|
||||
|
||||
return StringComparison.Ordinal;
|
||||
}
|
||||
|
||||
public static StringComparer GetStringComparer(this CaseSensitivity caseSensitivity, CommandPart part)
|
||||
{
|
||||
if (part == CommandPart.CommandName && (caseSensitivity & CaseSensitivity.Commands) == 0)
|
||||
{
|
||||
return StringComparer.OrdinalIgnoreCase;
|
||||
}
|
||||
else if (part == CommandPart.LongOption && (caseSensitivity & CaseSensitivity.LongOptions) == 0)
|
||||
{
|
||||
return StringComparer.OrdinalIgnoreCase;
|
||||
}
|
||||
|
||||
return StringComparer.Ordinal;
|
||||
}
|
||||
}
|
@ -0,0 +1,51 @@
|
||||
namespace Spectre.Console.Cli;
|
||||
|
||||
internal static class ListExtensions
|
||||
{
|
||||
public static void ForEach<T>(this IEnumerable<T> source, Action<T> action)
|
||||
{
|
||||
if (source != null && action != null)
|
||||
{
|
||||
foreach (var item in source)
|
||||
{
|
||||
action(item);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public static T AddAndReturn<T>(this IList<T> source, T item)
|
||||
where T : class
|
||||
{
|
||||
source.Add(item);
|
||||
return item;
|
||||
}
|
||||
|
||||
public static void AddIfNotNull<T>(this IList<T> source, T? item)
|
||||
where T : class
|
||||
{
|
||||
if (item != null)
|
||||
{
|
||||
source.Add(item);
|
||||
}
|
||||
}
|
||||
|
||||
public static void AddRangeIfNotNull<T>(this IList<T> source, IEnumerable<T?> items)
|
||||
where T : class
|
||||
{
|
||||
foreach (var item in items)
|
||||
{
|
||||
if (item != null)
|
||||
{
|
||||
source.Add(item);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public static void AddRange<T>(this IList<T> source, IEnumerable<T> items)
|
||||
{
|
||||
foreach (var item in items)
|
||||
{
|
||||
source.Add(item);
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,13 @@
|
||||
namespace Spectre.Console.Cli;
|
||||
|
||||
internal static class StringExtensions
|
||||
{
|
||||
internal static int OrdinalIndexOf(this string text, char token)
|
||||
{
|
||||
#if NETSTANDARD2_0
|
||||
return text.IndexOf(token);
|
||||
#else
|
||||
return text.IndexOf(token, System.StringComparison.Ordinal);
|
||||
#endif
|
||||
}
|
||||
}
|
@ -0,0 +1,19 @@
|
||||
namespace Spectre.Console.Cli;
|
||||
|
||||
internal static class TypeExtensions
|
||||
{
|
||||
public static bool IsPairDeconstructable(this Type type)
|
||||
{
|
||||
if (type.IsGenericType)
|
||||
{
|
||||
if (type.GetGenericTypeDefinition() == typeof(ILookup<,>) ||
|
||||
type.GetGenericTypeDefinition() == typeof(IDictionary<,>) ||
|
||||
type.GetGenericTypeDefinition() == typeof(IReadOnlyDictionary<,>))
|
||||
{
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
}
|
@ -0,0 +1,52 @@
|
||||
namespace Spectre.Console.Cli;
|
||||
|
||||
internal static class TypeRegistrarExtensions
|
||||
{
|
||||
public static void RegisterDependencies(this ITypeRegistrar registrar, CommandModel model)
|
||||
{
|
||||
var stack = new Stack<CommandInfo>();
|
||||
model.Commands.ForEach(c => stack.Push(c));
|
||||
if (model.DefaultCommand != null)
|
||||
{
|
||||
stack.Push(model.DefaultCommand);
|
||||
}
|
||||
|
||||
while (stack.Count > 0)
|
||||
{
|
||||
var command = stack.Pop();
|
||||
|
||||
if (command.SettingsType == null)
|
||||
{
|
||||
// TODO: Error message
|
||||
throw new InvalidOperationException("Command setting type cannot be null.");
|
||||
}
|
||||
|
||||
if (command.CommandType != null)
|
||||
{
|
||||
registrar?.Register(command.CommandType, command.CommandType);
|
||||
}
|
||||
|
||||
foreach (var parameter in command.Parameters)
|
||||
{
|
||||
var pairDeconstructor = parameter?.PairDeconstructor?.Type;
|
||||
if (pairDeconstructor != null)
|
||||
{
|
||||
registrar?.Register(pairDeconstructor, pairDeconstructor);
|
||||
}
|
||||
|
||||
var typeConverterTypeName = parameter?.Converter?.ConverterTypeName;
|
||||
if (!string.IsNullOrWhiteSpace(typeConverterTypeName))
|
||||
{
|
||||
var typeConverterType = Type.GetType(typeConverterTypeName);
|
||||
Debug.Assert(typeConverterType != null, "Could not create type");
|
||||
registrar?.Register(typeConverterType, typeConverterType);
|
||||
}
|
||||
}
|
||||
|
||||
foreach (var child in command.Children)
|
||||
{
|
||||
stack.Push(child);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,39 @@
|
||||
namespace Spectre.Console.Cli;
|
||||
|
||||
internal static class XmlElementExtensions
|
||||
{
|
||||
public static void SetNullableAttribute(this XmlElement element, string name, string? value)
|
||||
{
|
||||
element.SetAttribute(name, value ?? "NULL");
|
||||
}
|
||||
|
||||
public static void SetNullableAttribute(this XmlElement element, string name, IEnumerable<string>? values)
|
||||
{
|
||||
if (values?.Any() != true)
|
||||
{
|
||||
element.SetAttribute(name, "NULL");
|
||||
}
|
||||
|
||||
element.SetAttribute(name, string.Join(",", values ?? Enumerable.Empty<string>()));
|
||||
}
|
||||
|
||||
public static void SetBooleanAttribute(this XmlElement element, string name, bool value)
|
||||
{
|
||||
element.SetAttribute(name, value ? "true" : "false");
|
||||
}
|
||||
|
||||
public static void SetEnumAttribute(this XmlElement element, string name, Enum value)
|
||||
{
|
||||
var field = value.GetType().GetField(value.ToString());
|
||||
if (field != null)
|
||||
{
|
||||
var attribute = field.GetCustomAttribute<DescriptionAttribute>(false);
|
||||
if (attribute == null)
|
||||
{
|
||||
throw new InvalidOperationException("Enum is missing description.");
|
||||
}
|
||||
|
||||
element.SetAttribute(name, attribute.Description);
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user