Skip to content

Commit

Permalink
Fixed Enum display names were not being used (#239)
Browse files Browse the repository at this point in the history
shibayan authored Nov 11, 2022

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature. The key has expired.
1 parent 46d2ffc commit 169631f
Showing 4 changed files with 10 additions and 5 deletions.
6 changes: 3 additions & 3 deletions Sharprompt/Internal/EnumHelper.cs
Original file line number Diff line number Diff line change
@@ -31,11 +31,11 @@ private static EnumMetadata GetEnumMetadata(TEnum value)
};
}

public static string? GetDisplayName(TEnum value) => s_metadataCache[value].DisplayName ?? value.ToString();
public static string GetDisplayName(TEnum value) => s_metadataCache[value].DisplayName ?? value.ToString()!;

public static IEnumerable<TEnum> GetValues() => s_metadataCache.OrderBy(x => x.Value.Order)
.Select(x => x.Key)
.ToArray();
.Select(x => x.Key)
.ToArray();

private class EnumMetadata
{
1 change: 1 addition & 0 deletions Sharprompt/MultiSelectOptions.cs
Original file line number Diff line number Diff line change
@@ -14,6 +14,7 @@ public MultiSelectOptions()
if (typeof(T).IsAssignableTo(typeof(Enum)))
{
Items = EnumHelper<T>.GetValues();
TextSelector = EnumHelper<T>.GetDisplayName;
}
}

7 changes: 5 additions & 2 deletions Sharprompt/Prompt.Basic.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;

using Sharprompt.Forms;
using Sharprompt.Internal;
@@ -155,7 +154,11 @@ public static IEnumerable<T> MultiSelect<T>(string message, IEnumerable<T>? item
options.Items = items;
}

options.DefaultValues = defaultValues ?? Enumerable.Empty<T>();
if (defaultValues is not null)
{
options.DefaultValues = defaultValues;
}

options.PageSize = pageSize;
options.Minimum = minimum;
options.Maximum = maximum;
1 change: 1 addition & 0 deletions Sharprompt/SelectOptions.cs
Original file line number Diff line number Diff line change
@@ -13,6 +13,7 @@ public SelectOptions()
if (typeof(T).IsAssignableTo(typeof(Enum)))
{
Items = EnumHelper<T>.GetValues();
TextSelector = EnumHelper<T>.GetDisplayName;
}
}

0 comments on commit 169631f

Please sign in to comment.