Skip to content

Commit 8fec3a6

Browse files
authored
Upgrade to .NET 8 and latest C# (#293)
* Upgrade to .NET 8 and latest C# * Revert Optional<T> changes
1 parent 8256df6 commit 8fec3a6

18 files changed

+30
-55
lines changed

Sharprompt.Example/Program.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ static void Main(string[] args)
5454

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

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

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

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

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

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

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

Sharprompt.Tests/PropertyMetadataTests.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -297,9 +297,9 @@ public class MemberItemsModel
297297

298298
public static IEnumerable<int> GetSelectItems()
299299
{
300-
return new[] { 1, 2, 3, 4, 5 };
300+
return [1, 2, 3, 4, 5];
301301
}
302302

303-
public static IEnumerable<int> SelectItems => new[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
303+
public static IEnumerable<int> SelectItems => [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
304304
}
305305
}

Sharprompt/BindIgnoreAttribute.cs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,4 @@
33
namespace Sharprompt;
44

55
[AttributeUsage(AttributeTargets.Property)]
6-
public sealed class BindIgnoreAttribute : Attribute
7-
{
8-
}
6+
public sealed class BindIgnoreAttribute : Attribute;

Sharprompt/Forms/FormRenderer.cs

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,9 @@
55

66
namespace Sharprompt.Forms;
77

8-
internal class FormRenderer : IDisposable
8+
internal class FormRenderer(IConsoleDriver consoleDriver) : IDisposable
99
{
10-
public FormRenderer(IConsoleDriver consoleDriver)
11-
{
12-
_offscreenBuffer = new OffscreenBuffer(consoleDriver);
13-
}
14-
15-
private readonly OffscreenBuffer _offscreenBuffer;
10+
private readonly OffscreenBuffer _offscreenBuffer = new(consoleDriver);
1611

1712
public string? ErrorMessage { get; set; }
1813

Sharprompt/Forms/ListForm.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ public ListForm(ListOptions<T> options)
2121

2222
private readonly ListOptions<T> _options;
2323

24-
private readonly List<T> _inputItems = new();
24+
private readonly List<T> _inputItems = [];
2525

2626
protected override void InputTemplate(OffscreenBuffer offscreenBuffer)
2727
{

Sharprompt/Forms/MultiSelectForm.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ public MultiSelectForm(MultiSelectOptions<T> options)
4141
private readonly MultiSelectOptions<T> _options;
4242
private readonly Paginator<T> _paginator;
4343

44-
private readonly HashSet<T> _selectedItems = new();
44+
private readonly HashSet<T> _selectedItems = [];
4545

4646
protected override void InputTemplate(OffscreenBuffer offscreenBuffer)
4747
{

Sharprompt/Internal/EastAsianWidth.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ private static bool IsFullWidth(uint codePoint)
6161
}
6262

6363
private static readonly EastAsianWidthRange[] s_eastAsianWidthRanges =
64-
{
64+
[
6565
new(161, 0, true),
6666
new(164, 0, true),
6767
new(167, 1, true),
@@ -362,7 +362,7 @@ private static bool IsFullWidth(uint codePoint)
362362
new(917760, 239, true),
363363
new(983040, 65533, true),
364364
new(1048576, 65533, true)
365-
};
365+
];
366366

367367
public readonly record struct EastAsianWidthRange(uint Start, ushort Count, bool Ambiguous);
368368
}

Sharprompt/Internal/EnumHelper.cs

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,7 @@ internal static class EnumHelper<TEnum> where TEnum : notnull
1010
{
1111
static EnumHelper()
1212
{
13-
#if NET7_0_OR_GREATER
1413
var values = (TEnum[])Enum.GetValuesAsUnderlyingType(typeof(TEnum));
15-
#else
16-
var values = (TEnum[])Enum.GetValues(typeof(TEnum));
17-
#endif
1814

1915
foreach (var value in values)
2016
{
Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
11
using System.Collections.Generic;
2-
using System.Linq;
32
using System.Reflection;
43

54
namespace Sharprompt.Internal;
65

76
internal class NullItemsProvider : IItemsProvider
87
{
9-
public IEnumerable<T> GetItems<T>(PropertyInfo targetPropertyInfo) where T : notnull => Enumerable.Empty<T>();
8+
public IEnumerable<T> GetItems<T>(PropertyInfo targetPropertyInfo) where T : notnull => [];
109

1110
public static IItemsProvider Instance { get; } = new NullItemsProvider();
1211
}

Sharprompt/Internal/OffscreenBuffer.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ public OffscreenBuffer(IConsoleDriver consoleDriver)
1616
}
1717

1818
private readonly IConsoleDriver _consoleDriver;
19-
private readonly List<List<TextInfo>> _outputBuffer = new() { new List<TextInfo>() };
19+
private readonly List<List<TextInfo>> _outputBuffer = [new()];
2020

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

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

4242
public void PushCursor()
4343
{
@@ -105,7 +105,7 @@ public void ClearConsole(int cursorBottom, int writtenLineCount)
105105
public void ClearBuffer()
106106
{
107107
_outputBuffer.Clear();
108-
_outputBuffer.Add(new List<TextInfo>());
108+
_outputBuffer.Add([]);
109109

110110
_pushedCursor = null;
111111
}

0 commit comments

Comments
 (0)