-
Notifications
You must be signed in to change notification settings - Fork 383
[ToolsCommon] Refactor Utils.cs #5622
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
mdh1418
merged 22 commits into
dotnet:main
from
mdh1418:enable_command_utils_console_redirection
Nov 6, 2025
Merged
Changes from 20 commits
Commits
Show all changes
22 commits
Select commit
Hold shift + click to select a range
155b9af
[dotnet-counters] Revert CounterMonitor to using IConsole
mdh1418 079b98a
[dotnet-trace][collect] Set console through constructor
mdh1418 3422ff6
[CommandUtils] Enable output redirection
mdh1418 30d433d
Revert "[CommandUtils] Enable output redirection"
mdh1418 b116e70
[CommandUtils] Convert FindProcessIdWithName to throw CommandLineErro…
mdh1418 44f7364
[CommandUtils] Convert ValidateArgumentsForChildProcess to throw
mdh1418 7a8450a
[CommandUtils] Convert ResolveProcessForAttach to throw
mdh1418 2e9cab0
Add corresponding compile items
mdh1418 f7f7704
Fix exception typo
mdh1418 90f7841
Default DefaultConsole to not use ansi
mdh1418 7b0aa79
[dotnet-trace] Fix ReturnCode from CommandLineErrorException
mdh1418 6be25ae
[dotnet-trace] Handle DSRouter Launch Failure ReturnCode
mdh1418 0e29459
Cleanup ProcessLauncher Start
mdh1418 e2d336a
Rename CommandLineErrorException
mdh1418 61d6413
Merge remote-tracking branch 'upstream/main' into enable_command_util…
mdh1418 2814b51
[CommandUtils] Convert ResolveProcess to throw
mdh1418 bdeb602
Add ReturnCode to DiagnosticToolException
mdh1418 cced459
[ToolsCommon] Break out ReturnCode
mdh1418 56d7451
[ToolsCommon] Break out LineRewriter
mdh1418 944c6ce
[CommonTools] Rename and move CommandUtils
mdh1418 f35c402
Cleanup remnant cast
mdh1418 0184d7c
Merge remote-tracking branch 'upstream/main' into enable_command_util…
mdh1418 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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,74 @@ | ||
| // Licensed to the .NET Foundation under one or more agreements. | ||
| // The .NET Foundation licenses this file to you under the MIT license. | ||
|
|
||
| using Microsoft.Diagnostics.Tools.Common; | ||
|
|
||
| namespace Microsoft.Internal.Common.Utils | ||
| { | ||
| internal sealed class LineRewriter | ||
| { | ||
| public int LineToClear { get; set; } | ||
|
|
||
| private IConsole Console { get; } | ||
|
|
||
| public LineRewriter(IConsole console) | ||
| { | ||
| Console = console; | ||
| } | ||
|
|
||
| // ANSI escape codes: | ||
| // [2K => clear current line | ||
| // [{LineToClear};0H => move cursor to column 0 of row `LineToClear` | ||
| public void RewriteConsoleLine() | ||
| { | ||
| bool useConsoleFallback = true; | ||
| if (!Console.IsInputRedirected) | ||
| { | ||
| // in case of console input redirection, the control ANSI codes would appear | ||
|
|
||
| // first attempt ANSI Codes | ||
| int before = Console.CursorTop; | ||
| Console.Out.Write($"\u001b[2K\u001b[{LineToClear};0H"); | ||
| int after = Console.CursorTop; | ||
|
|
||
| // Some consoles claim to be VT100 compliant, but don't respect | ||
| // all of the ANSI codes, so fallback to the System.Console impl in that case | ||
| useConsoleFallback = (before == after); | ||
| } | ||
|
|
||
| if (useConsoleFallback) | ||
| { | ||
| SystemConsoleLineRewriter(); | ||
| } | ||
| } | ||
|
|
||
| private void SystemConsoleLineRewriter() => Console.SetCursorPosition(0, LineToClear); | ||
|
|
||
| private static bool? _isSetCursorPositionSupported; | ||
| public bool IsRewriteConsoleLineSupported | ||
| { | ||
| get | ||
| { | ||
| bool isSupported = _isSetCursorPositionSupported ?? EnsureInitialized(); | ||
| return isSupported; | ||
|
|
||
| bool EnsureInitialized() | ||
| { | ||
| try | ||
| { | ||
| int left = Console.CursorLeft; | ||
| int top = Console.CursorTop; | ||
| Console.SetCursorPosition(0, LineToClear); | ||
| Console.SetCursorPosition(left, top); | ||
| _isSetCursorPositionSupported = true; | ||
| } | ||
| catch | ||
| { | ||
| _isSetCursorPositionSupported = false; | ||
| } | ||
| return (bool)_isSetCursorPositionSupported; | ||
| } | ||
| } | ||
| } | ||
| } | ||
| } |
This file contains hidden or 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,15 @@ | ||
| // Licensed to the .NET Foundation under one or more agreements. | ||
| // The .NET Foundation licenses this file to you under the MIT license. | ||
|
|
||
| namespace Microsoft.Internal.Common.Utils | ||
| { | ||
| internal enum ReturnCode | ||
| { | ||
| Ok, | ||
| SessionCreationError, | ||
| TracingError, | ||
| ArgumentError, | ||
| PlatformNotSupportedError, | ||
| UnknownError | ||
| } | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.