Skip to content

Commit

Permalink
v2: упрощение перечислений
Browse files Browse the repository at this point in the history
  • Loading branch information
Mr-Rm committed Jun 24, 2024
1 parent 1f3894e commit e79ef85
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 70 deletions.
79 changes: 49 additions & 30 deletions src/OneScript.StandardLibrary/Text/ConsoleColorEnum.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,46 +7,65 @@ This Source Code Form is subject to the terms of the

using System;
using OneScript.Contexts.Enums;
using OneScript.Types;
using OneScript.Exceptions;
using ScriptEngine.Machine;
using ScriptEngine.Machine.Contexts;

namespace OneScript.StandardLibrary.Text
{
[SystemEnum("ЦветКонсоли", "ConsoleColor")]
public class ConsoleColorEnum : ClrEnumWrapper<ConsoleColor>
[EnumerationType("ЦветКонсоли", "ConsoleColor")]
public enum ConsoleColorEnum
{
private ConsoleColorEnum(TypeDescriptor typeRepresentation, TypeDescriptor valuesType)
: base(typeRepresentation, valuesType)
[EnumValue("Черный")]
Black = System.ConsoleColor.Black,
[EnumValue("ТемноСиний")]
DarkBlue = ConsoleColor.DarkBlue,
[EnumValue("ТемноЗеленый")]
DarkGreen = ConsoleColor.DarkGreen,
[EnumValue("ТемноБирюзовый")]
DarkCyan = ConsoleColor.DarkCyan,
[EnumValue("ТемноКрасный")]
DarkRed = ConsoleColor.DarkRed,
[EnumValue("ТемноМалиновый")]
DarkMagenta = ConsoleColor.DarkMagenta,
[EnumValue("ТемноЖелтый")]
DarkYellow = ConsoleColor.DarkYellow,
[EnumValue("Серый")]
Gray = ConsoleColor.Gray,

[EnumValue("ТемноСерый")]
DarkGray = System.ConsoleColor.DarkGray,
[EnumValue("Синий")]
Blue = ConsoleColor.Blue,
[EnumValue("Зеленый")]
Green = ConsoleColor.Green,
[EnumValue("Бирюза")]
Cyan = ConsoleColor.Cyan,
[EnumValue("Красный")]
Red = ConsoleColor.Red,
[EnumValue("Малиновый")]
Magenta = ConsoleColor.Magenta,
[EnumValue("Желтый")]
Yellow = ConsoleColor.Yellow,
[EnumValue("Белый")]
White = ConsoleColor.White
}

public static class ConsoleColorEnumExt
{
public static IValue Wrap(this ConsoleColor color)
{
return ClrEnumWrapper<ConsoleColorEnum>.Instance?.FromNativeValue((ConsoleColorEnum)color);
}

public static ConsoleColorEnum CreateInstance(ITypeManager typeManager)
public static ConsoleColor Unwrap(IValue color)
{
var instance = EnumContextHelper.CreateClrEnumInstance<ConsoleColorEnum, ConsoleColor>(
typeManager,
(t,v) => new ConsoleColorEnum(t,v));

instance.WrapClrValue("Черный", "Black", ConsoleColor.Black);
instance.WrapClrValue("ТемноСиний", "DarkBlue", ConsoleColor.DarkBlue);
instance.WrapClrValue("ТемноЗеленый", "DarkGreen", ConsoleColor.DarkGreen);
instance.WrapClrValue("ТемноБирюзовый", "DarkCyan", ConsoleColor.DarkCyan);
instance.WrapClrValue("ТемноКрасный", "DarkRed", ConsoleColor.DarkRed);
instance.WrapClrValue("ТемноМалиновый", "DarkMagenta", ConsoleColor.DarkMagenta);
instance.WrapClrValue("ТемноЖелтый", "DarkYellow", ConsoleColor.DarkYellow);
instance.WrapClrValue("Серый", "Gray", ConsoleColor.Gray);

instance.WrapClrValue("ТемноСерый", "DarkGray", ConsoleColor.DarkGray);
instance.WrapClrValue("Синий", "Blue", ConsoleColor.Blue);
instance.WrapClrValue("Зеленый", "Green", ConsoleColor.Green);
instance.WrapClrValue("Бирюза", "Cyan", ConsoleColor.Cyan);
instance.WrapClrValue("Красный", "Red", ConsoleColor.Red);
instance.WrapClrValue("Малиновый", "Magenta", ConsoleColor.Magenta);
instance.WrapClrValue("Желтый", "Yellow", ConsoleColor.Yellow);
instance.WrapClrValue("Белый", "White", ConsoleColor.White);
if (color.GetRawValue() is ClrEnumValueWrapper<ConsoleColorEnum> typed)
{
return Console.ForegroundColor = (ConsoleColor)typed.UnderlyingValue;
}

OnInstanceCreation(instance);

return instance;
throw new TypeConversionException();
}
}
}
44 changes: 4 additions & 40 deletions src/OneScript.StandardLibrary/Text/ConsoleContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -87,51 +87,15 @@ public bool CursorVisible(bool visible)
[ContextProperty("ЦветТекста", "TextColor")]
public IValue TextColor
{
get
{
try
{
return ConsoleColorEnum.Instance.FromNativeValue(Console.ForegroundColor);
}
catch (InvalidOperationException)
{
return null;
}
}
set
{
if (value.GetRawValue() is ClrEnumValueWrapper<ConsoleColor> typed)
{
Console.ForegroundColor = typed.UnderlyingValue;
}
else
throw new TypeConversionException();
}
get => Console.ForegroundColor.Wrap();
set => Console.ForegroundColor = ConsoleColorEnumExt.Unwrap(value);
}

[ContextProperty("ЦветФона", "BackgroundColor")]
public IValue BackgroundColor
{
get
{
try
{
return GlobalsHelper.GetEnum<ConsoleColorEnum>().FromNativeValue(Console.BackgroundColor);
}
catch (InvalidOperationException)
{
return null;
}
}
set
{
if (value.GetRawValue() is ClrEnumValueWrapper<ConsoleColor> typed)
{
Console.BackgroundColor = typed.UnderlyingValue;
}
else
throw new TypeConversionException();
}
get => Console.BackgroundColor.Wrap();
set => Console.BackgroundColor = ConsoleColorEnumExt.Unwrap(value);
}

/// <summary>
Expand Down

0 comments on commit e79ef85

Please sign in to comment.