Skip to content

Commit f3e884a

Browse files
committed
.NET 9 Support & small fixes
1 parent 026544d commit f3e884a

7 files changed

+86
-31
lines changed

XConsole.Demo/XConsole.Demo.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<Project Sdk="Microsoft.NET.Sdk">
22

33
<PropertyGroup>
4-
<TargetFramework>net8.0-windows</TargetFramework>
4+
<TargetFramework>net9.0-windows</TargetFramework>
55
<OutputType>Exe</OutputType>
66
<IsPackable>false</IsPackable>
77
</PropertyGroup>

XConsole/ConsoleExtensions.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ public static class ConsoleExtensions
1212
/// <summary>
1313
/// Displays the <paramref name="message"/> and waits until the user presses Y or N and then Enter.
1414
/// </summary>
15-
/// <returns>True or False according to the user’s decision.</returns>
15+
/// <returns><see langword="True"/> or <see langword="false"/> according to the user’s decision.</returns>
1616
public static bool Confirm(
1717
this ConsoleExtras extras, string message = "Continue? [y/n]: ", string yes = "Yes", string no = "No")
1818
{

XConsole/ConsoleItem.cs

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,4 @@
1-
#if !NET
2-
#pragma warning disable CS8602 // Dereference of a possibly null reference.
3-
#endif
4-
5-
namespace Chubrik.XConsole;
1+
namespace Chubrik.XConsole;
62

73
using System;
84

@@ -82,6 +78,8 @@ public int GetSingleLineLengthOrZero()
8278
#if NET
8379
if (@char == '\x1b' && VirtualTerminal.IsEnabled)
8480
{
81+
i++;
82+
8583
for (; i < valueLength; i++)
8684
{
8785
@char = value[i];

XConsole/ConsolePosition.cs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
1-
#pragma warning disable CS1734 // XML comment has a paramref tag, but there is no parameter by that name
2-
3-
namespace Chubrik.XConsole;
1+
namespace Chubrik.XConsole;
42

53
using System;
64
#if NET
@@ -75,6 +73,8 @@ internal ConsolePosition(int left, int top, long shiftTop)
7573
ShiftTop = shiftTop;
7674
}
7775

76+
77+
#pragma warning disable CS1734 // XML comment has a paramref tag, but there is no parameter by that name
7878
/// <summary>
7979
/// <paramref name="Left"/> and <paramref name="top"/> arguments should be specified.
8080
/// </summary>
@@ -83,6 +83,7 @@ internal ConsolePosition(int left, int top, long shiftTop)
8383
/// <br/>• <see cref="ConsolePosition(int, int)"/>
8484
/// </remarks>
8585
/// <exception cref="InvalidOperationException"/>
86+
#pragma warning restore CS1734 // XML comment has a paramref tag, but there is no parameter by that name
8687
[Obsolete("Arguments should be specified.", error: true)]
8788
public ConsolePosition() => throw new InvalidOperationException();
8889
}

XConsole/ConsolePositionExtensions.cs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
11
#pragma warning disable CS1573 // Parameter has no matching param tag in the XML comment (but other parameters do)
2-
#pragma warning disable CS1584 // XML comment has syntactically incorrect cref attribute
3-
#pragma warning disable CS1658 // Warning is overriding an error
42
#if !NET
53
#pragma warning disable CS8604 // Possible null reference argument.
64
#endif
@@ -232,7 +230,7 @@ public static ConsolePosition Write(this ConsolePosition position, ulong value)
232230
/// </param>
233231
/// <returns>
234232
/// The new end <see cref="ConsolePosition"/> structure,
235-
/// or <see cref="null"/> if the current position actually points outside the console buffer area.
233+
/// or <see langword="null"/> if the current position actually points outside the console buffer area.
236234
/// </returns>
237235
/// <exception cref="System.Security.SecurityException"/>
238236
/// <exception cref="System.IO.IOException"/>

XConsole/XConsole.cs

Lines changed: 74 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,9 @@ namespace Chubrik.XConsole;
1414
#if NET
1515
using System.Runtime.Versioning;
1616
#endif
17+
#if NET9_0_OR_GREATER
18+
using System.Threading;
19+
#endif
1720
#if NET7_0_OR_GREATER
1821
using System.Diagnostics.CodeAnalysis;
1922
#endif
@@ -52,7 +55,11 @@ public static class XConsole
5255

5356
private static readonly string _newLine = Environment.NewLine;
5457
private static readonly bool _positioningEnabled;
58+
#if NET9_0_OR_GREATER
59+
private static readonly Lock _syncLock = new();
60+
#else
5561
private static readonly object _syncLock = new();
62+
#endif
5663

5764
private static bool _coloringEnabled = Environment.GetEnvironmentVariable("NO_COLOR") == null;
5865
private static bool _cursorVisible;
@@ -565,7 +572,10 @@ internal static ConsolePosition WriteToPosition(
565572
{
566573
#if NET
567574
if (!OperatingSystem.IsWindows() && keyInfo.KeyChar == '\x1a')
575+
{
576+
Console.WriteLine();
568577
return null;
578+
}
569579
#endif
570580
if (isMaskedMode)
571581
Console.Write(maskChar);
@@ -1190,6 +1200,30 @@ public static (ConsolePosition Begin, ConsolePosition End) Write(
11901200
return WriteBase([ConsoleItem.Parse(string.Format(format, arg ?? []))], isWriteLine: false);
11911201
}
11921202

1203+
#if NET9_0_OR_GREATER
1204+
/// <summary>
1205+
/// <inheritdoc cref="Console.Write(string, ReadOnlySpan{object?})"/>
1206+
/// Text can be colored using a simple <see href="https://github.com/chubrik/XConsole#coloring">microsyntax</see>.
1207+
/// </summary>
1208+
/// <param name="format">
1209+
/// A composite format string.
1210+
/// Text can be colored using a simple <see href="https://github.com/chubrik/XConsole#coloring">microsyntax</see>.
1211+
/// </param>
1212+
/// <returns><inheritdoc cref="Write(string?)"/></returns>
1213+
/// <inheritdoc cref="Console.Write(string, ReadOnlySpan{object?})"/>
1214+
public static (ConsolePosition Begin, ConsolePosition End) Write(
1215+
[StringSyntax(StringSyntaxAttribute.CompositeFormat)] string format, params ReadOnlySpan<object?> arg)
1216+
{
1217+
if (format.Length == 0)
1218+
{
1219+
var position = CursorPosition;
1220+
return (position, position);
1221+
}
1222+
1223+
return WriteBase([ConsoleItem.Parse(string.Format(format, arg))], isWriteLine: false);
1224+
}
1225+
#endif
1226+
11931227
/// <inheritdoc cref="Console.Write(uint)"/>
11941228
/// <returns><inheritdoc cref="Write(string?)"/></returns>
11951229
public static (ConsolePosition Begin, ConsolePosition End) Write(uint value)
@@ -1377,6 +1411,27 @@ public static (ConsolePosition Begin, ConsolePosition End) WriteLine(
13771411
return WriteBase([ConsoleItem.Parse(string.Format(format, arg ?? []))], isWriteLine: true);
13781412
}
13791413

1414+
#if NET9_0_OR_GREATER
1415+
/// <summary>
1416+
/// <inheritdoc cref="Console.WriteLine(string, ReadOnlySpan{object?})"/>
1417+
/// Text can be colored using a simple <see href="https://github.com/chubrik/XConsole#coloring">microsyntax</see>.
1418+
/// </summary>
1419+
/// <param name="format">
1420+
/// A composite format string.
1421+
/// Text can be colored using a simple <see href="https://github.com/chubrik/XConsole#coloring">microsyntax</see>.
1422+
/// </param>
1423+
/// <returns><inheritdoc cref="WriteLine(string?)"/></returns>
1424+
/// <inheritdoc cref="Console.WriteLine(string, ReadOnlySpan{object?})"/>
1425+
public static (ConsolePosition Begin, ConsolePosition End) WriteLine(
1426+
[StringSyntax(StringSyntaxAttribute.CompositeFormat)] string format, params ReadOnlySpan<object?> arg)
1427+
{
1428+
if (format.Length == 0)
1429+
return WriteLine();
1430+
1431+
return WriteBase([ConsoleItem.Parse(string.Format(format, arg))], isWriteLine: true);
1432+
}
1433+
#endif
1434+
13801435
/// <inheritdoc cref="Console.WriteLine(uint)"/>
13811436
/// <returns><inheritdoc cref="WriteLine(string?)"/></returns>
13821437
public static (ConsolePosition Begin, ConsolePosition End) WriteLine(uint value)
@@ -1790,15 +1845,18 @@ public static int WindowTop
17901845
}
17911846

17921847
/// <inheritdoc cref="Console.WindowWidth"/>
1793-
#if NET
1794-
[UnsupportedOSPlatform("android")]
1795-
[UnsupportedOSPlatform("browser")]
1796-
[UnsupportedOSPlatform("ios")]
1797-
[UnsupportedOSPlatform("tvos")]
1798-
#endif
17991848
public static int WindowWidth
18001849
{
1850+
#if NET
1851+
[UnsupportedOSPlatform("android")]
1852+
[UnsupportedOSPlatform("browser")]
1853+
[UnsupportedOSPlatform("ios")]
1854+
[UnsupportedOSPlatform("tvos")]
1855+
#endif
18011856
get => Console.WindowWidth;
1857+
#if NET
1858+
[SupportedOSPlatform("windows")]
1859+
#endif
18021860
set
18031861
{
18041862
lock (_syncLock)
@@ -1807,15 +1865,18 @@ public static int WindowWidth
18071865
}
18081866

18091867
/// <inheritdoc cref="Console.WindowHeight"/>
1810-
#if NET
1811-
[UnsupportedOSPlatform("android")]
1812-
[UnsupportedOSPlatform("browser")]
1813-
[UnsupportedOSPlatform("ios")]
1814-
[UnsupportedOSPlatform("tvos")]
1815-
#endif
18161868
public static int WindowHeight
18171869
{
1870+
#if NET
1871+
[UnsupportedOSPlatform("android")]
1872+
[UnsupportedOSPlatform("browser")]
1873+
[UnsupportedOSPlatform("ios")]
1874+
[UnsupportedOSPlatform("tvos")]
1875+
#endif
18181876
get => Console.WindowHeight;
1877+
#if NET
1878+
[SupportedOSPlatform("windows")]
1879+
#endif
18191880
set
18201881
{
18211882
lock (_syncLock)
@@ -1834,10 +1895,7 @@ public static void SetWindowPosition(int left, int top)
18341895

18351896
/// <inheritdoc cref="Console.SetWindowSize(int, int)"/>
18361897
#if NET
1837-
[UnsupportedOSPlatform("android")]
1838-
[UnsupportedOSPlatform("browser")]
1839-
[UnsupportedOSPlatform("ios")]
1840-
[UnsupportedOSPlatform("tvos")]
1898+
[SupportedOSPlatform("windows")]
18411899
#endif
18421900
public static void SetWindowSize(int width, int height)
18431901
{

XConsole/XConsole.csproj

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
<Project Sdk="Microsoft.NET.Sdk">
22

33
<PropertyGroup>
4-
<TargetFrameworks>net8.0;net7.0;net6.0;net5.0;netstandard2.0;netstandard1.3;netframework462</TargetFrameworks>
5-
<AllowUnsafeBlocks Condition="'$(TargetFramework)'=='net7.0'">true</AllowUnsafeBlocks>
4+
<TargetFrameworks>net9.0;net8.0;net7.0;net6.0;net5.0;netstandard2.0;netstandard1.3;netframework462</TargetFrameworks>
65
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
6+
<NoWarn>IDE0130;NETSDK1215</NoWarn>
77
</PropertyGroup>
88

99
<PropertyGroup>

0 commit comments

Comments
 (0)