Skip to content

Conversation

@albahari
Copy link
Contributor

Fixes #2237

@AndreyAkinshin AndreyAkinshin added this to the v0.13.4 milestone Dec 29, 2022
@AndreyAkinshin
Copy link
Member

@albahari thanks for the PR!

@timcassell do you have any comments on this?

@AndreyAkinshin
Copy link
Member

@timcassell does everything look fine from your side?

@timcassell
Copy link
Collaborator

timcassell commented Dec 31, 2022

I left comments that should be addressed before you merge.

@albahari
Copy link
Contributor Author

albahari commented Jan 1, 2023

I left comments that should be addressed before you merge.

Which comments are you referring to? I added a constructor argument to the class as suggested, and updated the logic to handle platforms with write-only Console.Title.

@timcassell
Copy link
Collaborator

timcassell commented Jan 1, 2023

[Edit] Sorry, I didn't submit the change request so the comments weren't posted.

Comment on lines 649 to 652
private static void UpdateTitle(int totalBenchmarkCount, int benchmarksToRunCount, ConsoleTitler consoleTitler)
{
if (!Console.IsOutputRedirected && (RuntimeInformation.IsWindows() || RuntimeInformation.IsLinux() || RuntimeInformation.IsMacOS()))
{
Console.Title = $"{benchmarksToRunCount}/{totalBenchmarkCount} Remaining";
}
consoleTitler.UpdateTitle(() => $"{benchmarksToRunCount}/{totalBenchmarkCount} Remaining");
}
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

UpdateTitle function is no longer needed, just call consoleTitler.UpdateTitle directly.

var reports = new List<BenchmarkReport>();
string title = GetTitle(new[] { benchmarkRunInfo });
var consoleTitle = RuntimeInformation.IsWindows() ? Console.Title : string.Empty;
using var consoleTitler = new ConsoleTitler("");
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

- using var consoleTitler = new ConsoleTitler("");
+ using var consoleTitler = new ConsoleTitler($"{benchmarksToRunCount}/{totalBenchmarkCount} Remaining");

logger.WriteLine();

UpdateTitle(totalBenchmarkCount, benchmarksToRunCount);
UpdateTitle(totalBenchmarkCount, benchmarksToRunCount, consoleTitler);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remove this update title line since it's passed in the constructor.

/// Constructs a ConsoleTitler
/// </summary>
/// <param name="fallbackTitle">What to restore Console.Title to upon disposal (for platforms with write-only Console.Title)</param>
public ConsoleTitler(string fallbackTitle)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was thinking the passed in string would be the initial title to write, not the fallback title on dispose. I don't think there is any value in having a custom fallback title on dispose.

// 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.
// Because we won't be able to write Console.Title either, return without enabling the titler.
}
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Write the passed in title here. This is where you need the flag to check in UpdateTitle if it is writable. The flag you have now only checks if it's readable.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think you need a separate try/catch for the write instead of a platform check. We'd want to catch IOException and PlatformNotSupportedException. Let other exceptions propagate.

try
{
   Console.Title = title;
   IsEnabled = true;
}
catch (IOException) { }
catch (PlatformNotSupportedException) { }

Comment on lines 67 to 76
/// <summary>
/// Updates Console.Title if enabled, using a Func to avoid potential string-building cost.
/// </summary>
public void UpdateTitle(Func<string> title)
{
if (IsEnabled)
{
Console.Title = title();
}
}
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think this is necessary. The cost of string building in this case is minimal, and this is just adding the cost of a delegate and closures.

Comment on lines 33 to 38
try
{
oldConsoleTitle = PlatformSupportsTitleRead() ? Console.Title : fallbackTitle;
IsEnabled = true;
}
catch (IOException)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this can be simplified to

try
{
    oldConsoleTitle = Console.Title
}
catch
{
    oldConsoleTitle = string.Empty;
}

That should make it more future-proof.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This will require a #pragma to suppress CA1416 - you OK with that?

Console.Title's get accessor is defined with [SupportedOSPlatform("windows")]

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Well, I suppose it makes sense to have the platform check for the get since that's currently the only supported platform. It can be updated in the future if that ever changes. But no platform check for the set. I still think the simpler try/catch makes sense, though, even with the platform check.

Copy link
Collaborator

@timcassell timcassell left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, thanks for implementing the feedback.

Comment on lines +23 to +27
// Return without enabling if Console output is redirected.
if (Console.IsOutputRedirected)
{
return;
}
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not 100% sure if we want to disable this if output is redirected. I can imagine a scenario where the output is piped to a file, but it would still be useful to see the progress in the title. But not a big deal to me either way.

@AndreyAkinshin AndreyAkinshin merged commit e04e2d7 into dotnet:master Jan 4, 2023
@AndreyAkinshin
Copy link
Member

@albahari @timcassell thanks for your help!

@AndreyAkinshin
Copy link
Member

@albahari could you please verify that the latest nightly version (0.13.3.2068) works in LINQPad? I don't have a Windows machine right now, so I can't check myself.

You can get the nupkg from the appveyor build artifacts or via the nightly feed:

<packageSources>
  <add key="bdn-nightly" value="https://ci.appveyor.com/nuget/benchmarkdotnet" />
</packageSources>

@albahari
Copy link
Contributor Author

albahari commented Jan 5, 2023

Works a treat:

image

@AndreyAkinshin
Copy link
Member

@albahari v0.13.4 has been released. Thanks again for your assistance!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Version 0.13.3 breaks LINQPad (and any non-Console application)

3 participants