Skip to content

Commit

Permalink
Upgrade to .NET 8 and latest C# (#293)
Browse files Browse the repository at this point in the history
* Upgrade to .NET 8 and latest C#

* Revert Optional<T> changes
  • Loading branch information
shibayan authored Nov 16, 2024
1 parent 8256df6 commit 8fec3a6
Show file tree
Hide file tree
Showing 18 changed files with 30 additions and 55 deletions.
10 changes: 5 additions & 5 deletions Sharprompt.Example/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ static void Main(string[] args)

private static void RunInputSample()
{
var name = Prompt.Input<string>("What's your name?", defaultValue: "John Smith", placeholder: "At least 3 characters", validators: new[] { Validators.Required(), Validators.MinLength(3) });
var name = Prompt.Input<string>("What's your name?", defaultValue: "John Smith", placeholder: "At least 3 characters", validators: [Validators.Required(), Validators.MinLength(3)]);
Console.WriteLine($"Hello, {name}!");
}

Expand All @@ -66,19 +66,19 @@ private static void RunConfirmSample()

private static void RunPasswordSample()
{
var secret = Prompt.Password("Type new password", placeholder: "At least 8 characters", validators: new[] { Validators.Required(), Validators.MinLength(8) });
var secret = Prompt.Password("Type new password", placeholder: "At least 8 characters", validators: [Validators.Required(), Validators.MinLength(8)]);
Console.WriteLine($"Password OK, {secret}");
}

private static void RunSelectSample()
{
var city = Prompt.Select("Select your city", new[] { "Seattle", "London", "Tokyo", "New York", "Singapore", "Shanghai" }, pageSize: 3);
var city = Prompt.Select("Select your city", ["Seattle", "London", "Tokyo", "New York", "Singapore", "Shanghai"], pageSize: 3);
Console.WriteLine($"Hello, {city}!");
}

private static void RunMultiSelectSample()
{
var options = Prompt.MultiSelect("Which cities would you like to visit?", new[] { "Seattle", "London", "Tokyo", "New York", "Singapore", "Shanghai" }, pageSize: 3, defaultValues: new[] { "Tokyo" });
var options = Prompt.MultiSelect("Which cities would you like to visit?", ["Seattle", "London", "Tokyo", "New York", "Singapore", "Shanghai"], pageSize: 3, defaultValues: ["Tokyo"]);
Console.WriteLine($"You picked {string.Join(", ", options)}");
}

Expand All @@ -90,7 +90,7 @@ private static void RunSelectEnumSample()

private static void RunMultiSelectEnumSample()
{
var value = Prompt.MultiSelect<MyEnum>("Select enum value", defaultValues: new[] { MyEnum.Bar });
var value = Prompt.MultiSelect<MyEnum>("Select enum value", defaultValues: [MyEnum.Bar]);
Console.WriteLine($"You picked {string.Join(", ", value)}");
}

Expand Down
4 changes: 2 additions & 2 deletions Sharprompt.Tests/PropertyMetadataTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -297,9 +297,9 @@ public class MemberItemsModel

public static IEnumerable<int> GetSelectItems()
{
return new[] { 1, 2, 3, 4, 5 };
return [1, 2, 3, 4, 5];
}

public static IEnumerable<int> SelectItems => new[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
public static IEnumerable<int> SelectItems => [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
}
}
4 changes: 1 addition & 3 deletions Sharprompt/BindIgnoreAttribute.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,4 @@
namespace Sharprompt;

[AttributeUsage(AttributeTargets.Property)]
public sealed class BindIgnoreAttribute : Attribute
{
}
public sealed class BindIgnoreAttribute : Attribute;
9 changes: 2 additions & 7 deletions Sharprompt/Forms/FormRenderer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,9 @@

namespace Sharprompt.Forms;

internal class FormRenderer : IDisposable
internal class FormRenderer(IConsoleDriver consoleDriver) : IDisposable
{
public FormRenderer(IConsoleDriver consoleDriver)
{
_offscreenBuffer = new OffscreenBuffer(consoleDriver);
}

private readonly OffscreenBuffer _offscreenBuffer;
private readonly OffscreenBuffer _offscreenBuffer = new(consoleDriver);

public string? ErrorMessage { get; set; }

Expand Down
2 changes: 1 addition & 1 deletion Sharprompt/Forms/ListForm.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public ListForm(ListOptions<T> options)

private readonly ListOptions<T> _options;

private readonly List<T> _inputItems = new();
private readonly List<T> _inputItems = [];

protected override void InputTemplate(OffscreenBuffer offscreenBuffer)
{
Expand Down
2 changes: 1 addition & 1 deletion Sharprompt/Forms/MultiSelectForm.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public MultiSelectForm(MultiSelectOptions<T> options)
private readonly MultiSelectOptions<T> _options;
private readonly Paginator<T> _paginator;

private readonly HashSet<T> _selectedItems = new();
private readonly HashSet<T> _selectedItems = [];

protected override void InputTemplate(OffscreenBuffer offscreenBuffer)
{
Expand Down
4 changes: 2 additions & 2 deletions Sharprompt/Internal/EastAsianWidth.cs
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ private static bool IsFullWidth(uint codePoint)
}

private static readonly EastAsianWidthRange[] s_eastAsianWidthRanges =
{
[
new(161, 0, true),
new(164, 0, true),
new(167, 1, true),
Expand Down Expand Up @@ -362,7 +362,7 @@ private static bool IsFullWidth(uint codePoint)
new(917760, 239, true),
new(983040, 65533, true),
new(1048576, 65533, true)
};
];

public readonly record struct EastAsianWidthRange(uint Start, ushort Count, bool Ambiguous);
}
4 changes: 0 additions & 4 deletions Sharprompt/Internal/EnumHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,7 @@ internal static class EnumHelper<TEnum> where TEnum : notnull
{
static EnumHelper()
{
#if NET7_0_OR_GREATER
var values = (TEnum[])Enum.GetValuesAsUnderlyingType(typeof(TEnum));
#else
var values = (TEnum[])Enum.GetValues(typeof(TEnum));
#endif

foreach (var value in values)
{
Expand Down
3 changes: 1 addition & 2 deletions Sharprompt/Internal/NullItemsProvider.cs
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
using System.Collections.Generic;
using System.Linq;
using System.Reflection;

namespace Sharprompt.Internal;

internal class NullItemsProvider : IItemsProvider
{
public IEnumerable<T> GetItems<T>(PropertyInfo targetPropertyInfo) where T : notnull => Enumerable.Empty<T>();
public IEnumerable<T> GetItems<T>(PropertyInfo targetPropertyInfo) where T : notnull => [];

public static IItemsProvider Instance { get; } = new NullItemsProvider();
}
6 changes: 3 additions & 3 deletions Sharprompt/Internal/OffscreenBuffer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ public OffscreenBuffer(IConsoleDriver consoleDriver)
}

private readonly IConsoleDriver _consoleDriver;
private readonly List<List<TextInfo>> _outputBuffer = new() { new List<TextInfo>() };
private readonly List<List<TextInfo>> _outputBuffer = [new()];

private int _cursorBottom;
private Cursor? _pushedCursor;
Expand All @@ -37,7 +37,7 @@ public void Write(string text, ConsoleColor color)
_outputBuffer.Last().Add(new TextInfo(text, color));
}

public void WriteLine() => _outputBuffer.Add(new List<TextInfo>());
public void WriteLine() => _outputBuffer.Add([]);

public void PushCursor()
{
Expand Down Expand Up @@ -105,7 +105,7 @@ public void ClearConsole(int cursorBottom, int writtenLineCount)
public void ClearBuffer()
{
_outputBuffer.Clear();
_outputBuffer.Add(new List<TextInfo>());
_outputBuffer.Add([]);

_pushedCursor = null;
}
Expand Down
4 changes: 2 additions & 2 deletions Sharprompt/Internal/Optional.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@ public Optional(T value)
Value = value;
}

public bool HasValue { get; }
public bool HasValue { get; } = false;

public T Value { get; }
public T Value { get; } = default!;

public static readonly Optional<T> Empty = new();

Expand Down
2 changes: 1 addition & 1 deletion Sharprompt/Internal/Paginator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public Paginator(IEnumerable<T> items, int pageSize, Optional<T> defaultValue, F
private readonly Func<T, string> _textSelector;

private int _pageSize;
private T[] _filteredItems = Array.Empty<T>();
private T[] _filteredItems = [];
private int _selectedIndex = -1;

public ReadOnlySpan<T> CurrentItems => new(_filteredItems, _pageSize * CurrentPage, Count);
Expand Down
3 changes: 1 addition & 2 deletions Sharprompt/Internal/ValidatorsExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;

namespace Sharprompt.Internal;

internal static class ValidatorsExtensions
{
public static void Merge(this IList<Func<object?, ValidationResult?>> source, IEnumerable<Func<object?, ValidationResult?>>? validators)
{
foreach (var validator in validators ?? Enumerable.Empty<Func<object?, ValidationResult?>>())
foreach (var validator in validators ?? [])
{
source.Add(validator);
}
Expand Down
3 changes: 1 addition & 2 deletions Sharprompt/ListOptions.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.Strings;

Expand All @@ -11,7 +10,7 @@ public class ListOptions<T> where T : notnull
{
public string Message { get; set; } = null!;

public IEnumerable<T> DefaultValues { get; set; } = Enumerable.Empty<T>();
public IEnumerable<T> DefaultValues { get; set; } = [];

public int Minimum { get; set; } = 1;

Expand Down
3 changes: 1 addition & 2 deletions Sharprompt/MultiSelectOptions.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
using System;
using System.Collections.Generic;
using System.Linq;

using Sharprompt.Internal;
using Sharprompt.Strings;
Expand All @@ -22,7 +21,7 @@ public MultiSelectOptions()

public IEnumerable<T> Items { get; set; } = null!;

public IEnumerable<T> DefaultValues { get; set; } = Enumerable.Empty<T>();
public IEnumerable<T> DefaultValues { get; set; } = [];

public int PageSize { get; set; } = int.MaxValue;

Expand Down
7 changes: 3 additions & 4 deletions Sharprompt/Prompt.Bind.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;

using Sharprompt.Forms;
Expand Down Expand Up @@ -77,7 +76,7 @@ private static IEnumerable<T> MakeListCore<T>(PropertyMetadata propertyMetadata)
return List<T>(options =>
{
options.Message = propertyMetadata.Message;
options.DefaultValues = (IEnumerable<T>?)propertyMetadata.DefaultValue ?? Enumerable.Empty<T>();
options.DefaultValues = (IEnumerable<T>?)propertyMetadata.DefaultValue ?? [];
options.Validators.Merge(propertyMetadata.Validators);
});
Expand All @@ -91,7 +90,7 @@ private static IEnumerable<T> MakeMultiSelectCore<T>(PropertyMetadata propertyMe
{
options.Message = propertyMetadata.Message;
options.Items = propertyMetadata.ItemsProvider.GetItems<T>(propertyMetadata.PropertyInfo);
options.DefaultValues = (IEnumerable<T>?)propertyMetadata.DefaultValue ?? Enumerable.Empty<T>();
options.DefaultValues = (IEnumerable<T>?)propertyMetadata.DefaultValue ?? [];
});
}

Expand Down Expand Up @@ -123,6 +122,6 @@ private static object InvokeMethod(string name, PropertyMetadata propertyMetadat
var method = typeof(Prompt).GetMethod(name, BindingFlags.NonPublic | BindingFlags.Static)!
.MakeGenericMethod(genericType ?? propertyMetadata.Type);

return method.Invoke(null, new object[] { propertyMetadata })!;
return method.Invoke(null, [propertyMetadata])!;
}
}
2 changes: 1 addition & 1 deletion Sharprompt/Sharprompt.csproj
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFrameworks>net6.0;net8.0</TargetFrameworks>
<TargetFramework>net8.0</TargetFramework>
<Nullable>enable</Nullable>
</PropertyGroup>

Expand Down
13 changes: 2 additions & 11 deletions Sharprompt/Symbol.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,9 @@

namespace Sharprompt;

public class Symbol
public class Symbol(string value, string fallbackValue)
{
public Symbol(string value, string fallbackValue)
{
_value = value;
_fallbackValue = fallbackValue;
}

private readonly string _value;
private readonly string _fallbackValue;

public override string ToString() => IsUnicodeSupported ? _value : _fallbackValue;
public override string ToString() => IsUnicodeSupported ? value : fallbackValue;

public static implicit operator string(Symbol symbol) => symbol.ToString();

Expand Down

0 comments on commit 8fec3a6

Please sign in to comment.