-
-
Notifications
You must be signed in to change notification settings - Fork 981
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
- Loading branch information
Showing
2 changed files
with
86 additions
and
24 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
using System; | ||
using System.IO; | ||
using BenchmarkDotNet.Portability; | ||
|
||
namespace BenchmarkDotNet.Running | ||
{ | ||
/// <summary> | ||
/// Updates Console.Title, subject to platform capabilities and Console availability. | ||
/// Restores the original (or fallback) title upon disposal. | ||
/// </summary> | ||
internal class ConsoleTitler : IDisposable | ||
{ | ||
/// <summary> | ||
/// Whether this instance has any effect. This will be false if the platform doesn't support Console retitling, | ||
/// or if Console output is redirected. | ||
/// </summary> | ||
public bool IsEnabled { get; private set; } | ||
|
||
private string oldConsoleTitle; | ||
|
||
public ConsoleTitler(string initialTitle) | ||
{ | ||
// Return without enabling if Console output is redirected. | ||
if (Console.IsOutputRedirected) | ||
{ | ||
return; | ||
} | ||
|
||
try | ||
{ | ||
oldConsoleTitle = PlatformSupportsTitleRead() ? Console.Title : ""; | ||
} | ||
catch (IOException) | ||
{ | ||
// We're unable to read Console.Title on a platform that supports it. This can happen when no console | ||
// window is available due to the application being Windows Forms, WPF, Windows Service or a daemon. | ||
oldConsoleTitle = ""; | ||
} | ||
|
||
try | ||
{ | ||
// Enable ConsoleTitler if and only if we can successfully set the Console.Title property. | ||
Console.Title = initialTitle; | ||
IsEnabled = true; | ||
} | ||
catch (IOException) | ||
{ | ||
} | ||
catch (PlatformNotSupportedException) | ||
{ | ||
// As of .NET 7, platforms other than Windows, Linux and MacOS do not support Console retitling. | ||
} | ||
} | ||
|
||
#if NET6_0_OR_GREATER | ||
[System.Runtime.Versioning.SupportedOSPlatformGuard("windows")] | ||
#endif | ||
private static bool PlatformSupportsTitleRead() => RuntimeInformation.IsWindows(); | ||
|
||
/// <summary> | ||
/// Updates Console.Title if enabled. | ||
/// </summary> | ||
public void UpdateTitle(string title) | ||
{ | ||
if (IsEnabled) | ||
{ | ||
Console.Title = title; | ||
} | ||
} | ||
|
||
public void Dispose() | ||
{ | ||
if (IsEnabled) | ||
{ | ||
Console.Title = oldConsoleTitle; | ||
IsEnabled = false; | ||
} | ||
} | ||
} | ||
} |