Nullify IConsole and Console

This commit is contained in:
Jordan Dominion
2023-12-18 14:13:51 -05:00
parent 9ab553fe17
commit 49809d690c
4 changed files with 25 additions and 19 deletions
@@ -489,7 +489,7 @@ namespace Tgstation.Server.Host.Components
finally
{
if (originalConsoleTitle != null)
console.Title = originalConsoleTitle;
console.SetTitle(originalConsoleTitle);
}
}
catch (Exception ex)
@@ -569,7 +569,7 @@ namespace Tgstation.Server.Host.Components
try
{
logger.LogInformation("{versionString}", assemblyInformationProvider.VersionString);
console.Title = assemblyInformationProvider.VersionString;
console.SetTitle(assemblyInformationProvider.VersionString);
CheckSystemCompatibility();
+13 -11
View File
@@ -5,8 +5,6 @@ using System.Threading.Tasks;
using Tgstation.Server.Host.System;
#nullable disable
namespace Tgstation.Server.Host.IO
{
/// <summary>
@@ -15,13 +13,9 @@ namespace Tgstation.Server.Host.IO
sealed class Console : IConsole, IDisposable
{
/// <inheritdoc />
public string Title
{
get => platformIdentifier.IsWindows
? global::System.Console.Title
: null;
set => global::System.Console.Title = value;
}
public string? Title => platformIdentifier.IsWindows
? global::System.Console.Title
: null;
/// <inheritdoc />
public bool Available => Environment.UserInteractive;
@@ -91,7 +85,8 @@ namespace Tgstation.Server.Host.IO
// TODO: Make this better: https://stackoverflow.com/questions/9479573/how-to-interrupt-console-readline
CheckAvailable();
if (!usePasswordChar)
return global::System.Console.ReadLine();
return global::System.Console.ReadLine()
?? throw new InvalidOperationException("Console input has been closed!");
var passwordBuilder = new StringBuilder();
do
@@ -126,7 +121,7 @@ namespace Tgstation.Server.Host.IO
.WaitAsync(cancellationToken);
/// <inheritdoc />
public Task WriteAsync(string text, bool newLine, CancellationToken cancellationToken) => Task.Factory.StartNew(
public Task WriteAsync(string? text, bool newLine, CancellationToken cancellationToken) => Task.Factory.StartNew(
() =>
{
CheckAvailable();
@@ -145,6 +140,13 @@ namespace Tgstation.Server.Host.IO
DefaultIOManager.BlockingTaskCreationOptions,
TaskScheduler.Current);
/// <inheritdoc />
public void SetTitle(string newTitle)
{
ArgumentNullException.ThrowIfNull(newTitle);
global::System.Console.Title = newTitle;
}
/// <summary>
/// Assert that the <see cref="Console"/> is available, throwing an <see cref="InvalidOperationException"/> otherwise.
/// </summary>
+8 -4
View File
@@ -1,8 +1,6 @@
using System.Threading;
using System.Threading.Tasks;
#nullable disable
namespace Tgstation.Server.Host.IO
{
/// <summary>
@@ -13,7 +11,7 @@ namespace Tgstation.Server.Host.IO
/// <summary>
/// Gets or sets the <see cref="IConsole"/> window's title. Can return <see langword="null"/> if getting the console title is not supported.
/// </summary>
string Title { get; set; }
string? Title { get; }
/// <summary>
/// If the <see cref="IConsole"/> is visible to the user.
@@ -32,7 +30,7 @@ namespace Tgstation.Server.Host.IO
/// <param name="newLine">If there should be a new line after the <paramref name="text"/>.</param>
/// <param name="cancellationToken">The <see cref="CancellationToken"/> for the operation.</param>
/// <returns>A <see cref="Task"/> representing the running operation.</returns>
Task WriteAsync(string text, bool newLine, CancellationToken cancellationToken);
Task WriteAsync(string? text, bool newLine, CancellationToken cancellationToken);
/// <summary>
/// Wait for a key press on the <see cref="IConsole"/>.
@@ -48,5 +46,11 @@ namespace Tgstation.Server.Host.IO
/// <param name="cancellationToken">The <see cref="CancellationToken"/> for the operation.</param>
/// <returns>A <see cref="Task{TResult}"/> resulting in the <see cref="string"/> read by the <see cref="IConsole"/>.</returns>
Task<string> ReadLineAsync(bool usePasswordChar, CancellationToken cancellationToken);
/// <summary>
/// Sets a <paramref name="newTitle"/> console window.
/// </summary>
/// <param name="newTitle">The new <see cref="Title"/>.</param>
void SetTitle(string newTitle);
}
}
@@ -1107,7 +1107,7 @@ namespace Tgstation.Server.Host.Setup
return;
originalConsoleTitle = console.Title;
console.Title = $"{assemblyInformationProvider.VersionString} Setup Wizard";
console.SetTitle($"{assemblyInformationProvider.VersionString} Setup Wizard");
}
// Link passed cancellationToken with cancel key press
@@ -1197,7 +1197,7 @@ namespace Tgstation.Server.Host.Setup
{
await finalTask;
if (originalConsoleTitle != null)
console.Title = originalConsoleTitle;
console.SetTitle(originalConsoleTitle);
}
}
}