Skip to content

Commit

Permalink
.NET 9 Support & small fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
chubrik committed Dec 12, 2024
1 parent 026544d commit f3e884a
Show file tree
Hide file tree
Showing 7 changed files with 86 additions and 31 deletions.
2 changes: 1 addition & 1 deletion XConsole.Demo/XConsole.Demo.csproj
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net8.0-windows</TargetFramework>
<TargetFramework>net9.0-windows</TargetFramework>
<OutputType>Exe</OutputType>
<IsPackable>false</IsPackable>
</PropertyGroup>
Expand Down
2 changes: 1 addition & 1 deletion XConsole/ConsoleExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ public static class ConsoleExtensions
/// <summary>
/// Displays the <paramref name="message"/> and waits until the user presses Y or N and then Enter.
/// </summary>
/// <returns>True or False according to the user’s decision.</returns>
/// <returns><see langword="True"/> or <see langword="false"/> according to the user’s decision.</returns>
public static bool Confirm(
this ConsoleExtras extras, string message = "Continue? [y/n]: ", string yes = "Yes", string no = "No")
{
Expand Down
8 changes: 3 additions & 5 deletions XConsole/ConsoleItem.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,4 @@
#if !NET
#pragma warning disable CS8602 // Dereference of a possibly null reference.
#endif

namespace Chubrik.XConsole;
namespace Chubrik.XConsole;

using System;

Expand Down Expand Up @@ -82,6 +78,8 @@ public int GetSingleLineLengthOrZero()
#if NET
if (@char == '\x1b' && VirtualTerminal.IsEnabled)
{
i++;

for (; i < valueLength; i++)
{
@char = value[i];
Expand Down
7 changes: 4 additions & 3 deletions XConsole/ConsolePosition.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
#pragma warning disable CS1734 // XML comment has a paramref tag, but there is no parameter by that name

namespace Chubrik.XConsole;
namespace Chubrik.XConsole;

using System;
#if NET
Expand Down Expand Up @@ -75,6 +73,8 @@ internal ConsolePosition(int left, int top, long shiftTop)
ShiftTop = shiftTop;
}


#pragma warning disable CS1734 // XML comment has a paramref tag, but there is no parameter by that name
/// <summary>
/// <paramref name="Left"/> and <paramref name="top"/> arguments should be specified.
/// </summary>
Expand All @@ -83,6 +83,7 @@ internal ConsolePosition(int left, int top, long shiftTop)
/// <br/>• <see cref="ConsolePosition(int, int)"/>
/// </remarks>
/// <exception cref="InvalidOperationException"/>
#pragma warning restore CS1734 // XML comment has a paramref tag, but there is no parameter by that name
[Obsolete("Arguments should be specified.", error: true)]
public ConsolePosition() => throw new InvalidOperationException();
}
4 changes: 1 addition & 3 deletions XConsole/ConsolePositionExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
#pragma warning disable CS1573 // Parameter has no matching param tag in the XML comment (but other parameters do)
#pragma warning disable CS1584 // XML comment has syntactically incorrect cref attribute
#pragma warning disable CS1658 // Warning is overriding an error
#if !NET
#pragma warning disable CS8604 // Possible null reference argument.
#endif
Expand Down Expand Up @@ -232,7 +230,7 @@ public static ConsolePosition Write(this ConsolePosition position, ulong value)
/// </param>
/// <returns>
/// The new end <see cref="ConsolePosition"/> structure,
/// or <see cref="null"/> if the current position actually points outside the console buffer area.
/// or <see langword="null"/> if the current position actually points outside the console buffer area.
/// </returns>
/// <exception cref="System.Security.SecurityException"/>
/// <exception cref="System.IO.IOException"/>
Expand Down
90 changes: 74 additions & 16 deletions XConsole/XConsole.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ namespace Chubrik.XConsole;
#if NET
using System.Runtime.Versioning;
#endif
#if NET9_0_OR_GREATER
using System.Threading;
#endif
#if NET7_0_OR_GREATER
using System.Diagnostics.CodeAnalysis;
#endif
Expand Down Expand Up @@ -52,7 +55,11 @@ public static class XConsole

private static readonly string _newLine = Environment.NewLine;
private static readonly bool _positioningEnabled;
#if NET9_0_OR_GREATER
private static readonly Lock _syncLock = new();
#else
private static readonly object _syncLock = new();
#endif

private static bool _coloringEnabled = Environment.GetEnvironmentVariable("NO_COLOR") == null;
private static bool _cursorVisible;
Expand Down Expand Up @@ -565,7 +572,10 @@ internal static ConsolePosition WriteToPosition(
{
#if NET
if (!OperatingSystem.IsWindows() && keyInfo.KeyChar == '\x1a')
{
Console.WriteLine();
return null;
}
#endif
if (isMaskedMode)
Console.Write(maskChar);
Expand Down Expand Up @@ -1190,6 +1200,30 @@ public static (ConsolePosition Begin, ConsolePosition End) Write(
return WriteBase([ConsoleItem.Parse(string.Format(format, arg ?? []))], isWriteLine: false);
}

#if NET9_0_OR_GREATER
/// <summary>
/// <inheritdoc cref="Console.Write(string, ReadOnlySpan{object?})"/>
/// Text can be colored using a simple <see href="https://github.com/chubrik/XConsole#coloring">microsyntax</see>.
/// </summary>
/// <param name="format">
/// A composite format string.
/// Text can be colored using a simple <see href="https://github.com/chubrik/XConsole#coloring">microsyntax</see>.
/// </param>
/// <returns><inheritdoc cref="Write(string?)"/></returns>
/// <inheritdoc cref="Console.Write(string, ReadOnlySpan{object?})"/>
public static (ConsolePosition Begin, ConsolePosition End) Write(
[StringSyntax(StringSyntaxAttribute.CompositeFormat)] string format, params ReadOnlySpan<object?> arg)
{
if (format.Length == 0)
{
var position = CursorPosition;
return (position, position);
}

return WriteBase([ConsoleItem.Parse(string.Format(format, arg))], isWriteLine: false);
}
#endif

/// <inheritdoc cref="Console.Write(uint)"/>
/// <returns><inheritdoc cref="Write(string?)"/></returns>
public static (ConsolePosition Begin, ConsolePosition End) Write(uint value)
Expand Down Expand Up @@ -1377,6 +1411,27 @@ public static (ConsolePosition Begin, ConsolePosition End) WriteLine(
return WriteBase([ConsoleItem.Parse(string.Format(format, arg ?? []))], isWriteLine: true);
}

#if NET9_0_OR_GREATER
/// <summary>
/// <inheritdoc cref="Console.WriteLine(string, ReadOnlySpan{object?})"/>
/// Text can be colored using a simple <see href="https://github.com/chubrik/XConsole#coloring">microsyntax</see>.
/// </summary>
/// <param name="format">
/// A composite format string.
/// Text can be colored using a simple <see href="https://github.com/chubrik/XConsole#coloring">microsyntax</see>.
/// </param>
/// <returns><inheritdoc cref="WriteLine(string?)"/></returns>
/// <inheritdoc cref="Console.WriteLine(string, ReadOnlySpan{object?})"/>
public static (ConsolePosition Begin, ConsolePosition End) WriteLine(
[StringSyntax(StringSyntaxAttribute.CompositeFormat)] string format, params ReadOnlySpan<object?> arg)
{
if (format.Length == 0)
return WriteLine();

return WriteBase([ConsoleItem.Parse(string.Format(format, arg))], isWriteLine: true);
}
#endif

/// <inheritdoc cref="Console.WriteLine(uint)"/>
/// <returns><inheritdoc cref="WriteLine(string?)"/></returns>
public static (ConsolePosition Begin, ConsolePosition End) WriteLine(uint value)
Expand Down Expand Up @@ -1790,15 +1845,18 @@ public static int WindowTop
}

/// <inheritdoc cref="Console.WindowWidth"/>
#if NET
[UnsupportedOSPlatform("android")]
[UnsupportedOSPlatform("browser")]
[UnsupportedOSPlatform("ios")]
[UnsupportedOSPlatform("tvos")]
#endif
public static int WindowWidth
{
#if NET
[UnsupportedOSPlatform("android")]
[UnsupportedOSPlatform("browser")]
[UnsupportedOSPlatform("ios")]
[UnsupportedOSPlatform("tvos")]
#endif
get => Console.WindowWidth;
#if NET
[SupportedOSPlatform("windows")]
#endif
set
{
lock (_syncLock)
Expand All @@ -1807,15 +1865,18 @@ public static int WindowWidth
}

/// <inheritdoc cref="Console.WindowHeight"/>
#if NET
[UnsupportedOSPlatform("android")]
[UnsupportedOSPlatform("browser")]
[UnsupportedOSPlatform("ios")]
[UnsupportedOSPlatform("tvos")]
#endif
public static int WindowHeight
{
#if NET
[UnsupportedOSPlatform("android")]
[UnsupportedOSPlatform("browser")]
[UnsupportedOSPlatform("ios")]
[UnsupportedOSPlatform("tvos")]
#endif
get => Console.WindowHeight;
#if NET
[SupportedOSPlatform("windows")]
#endif
set
{
lock (_syncLock)
Expand All @@ -1834,10 +1895,7 @@ public static void SetWindowPosition(int left, int top)

/// <inheritdoc cref="Console.SetWindowSize(int, int)"/>
#if NET
[UnsupportedOSPlatform("android")]
[UnsupportedOSPlatform("browser")]
[UnsupportedOSPlatform("ios")]
[UnsupportedOSPlatform("tvos")]
[SupportedOSPlatform("windows")]
#endif
public static void SetWindowSize(int width, int height)
{
Expand Down
4 changes: 2 additions & 2 deletions XConsole/XConsole.csproj
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFrameworks>net8.0;net7.0;net6.0;net5.0;netstandard2.0;netstandard1.3;netframework462</TargetFrameworks>
<AllowUnsafeBlocks Condition="'$(TargetFramework)'=='net7.0'">true</AllowUnsafeBlocks>
<TargetFrameworks>net9.0;net8.0;net7.0;net6.0;net5.0;netstandard2.0;netstandard1.3;netframework462</TargetFrameworks>
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
<NoWarn>IDE0130;NETSDK1215</NoWarn>
</PropertyGroup>

<PropertyGroup>
Expand Down

0 comments on commit f3e884a

Please sign in to comment.