@@ -17,51 +17,29 @@ internal static class ConsoleHelper
1717 private const string ST = ESC + @"\" ; // String Terminator
1818
1919 /// <summary>
20- /// Write clickable link to console.
21- /// If console doesn't support OSC 8 hyperlinks. It writes plain link .
20+ /// Try to gets clickable link text for console.
21+ /// If console doesn't support clickable link, it returns false .
2222 /// </summary>
23- public static void WriteLineAsClickableLink ( ILogger consoleLogger , string link , string ? linkCaption = null , LogKind logKind = LogKind . Info , string prefixText = "" , string suffixText = "" )
23+ public static bool TryGetClickableLink ( string link , string ? linkCaption , out string result )
2424 {
25- if ( prefixText != "" )
26- consoleLogger . Write ( logKind , prefixText ) ;
27-
28- WriteAsClickableLink ( consoleLogger , link , linkCaption , logKind ) ;
29-
30- // On Windows Terminal environment.
31- // It need to write extra space to avoid link style corrupted issue that occurred when window resized.
32- if ( IsWindowsTerminal . Value && IsClickableLinkSupported . Value && suffixText == "" )
33- suffixText = " " ;
34-
35- if ( suffixText != "" )
36- consoleLogger . Write ( logKind , suffixText ) ;
37-
38- consoleLogger . WriteLine ( ) ;
39- }
40-
41- /// <summary>
42- /// Write clickable link to console.
43- /// If console doesn't support OSC 8 hyperlinks. It writes plain link.
44- /// </summary>
45- public static void WriteAsClickableLink ( ILogger consoleLogger , string link , string ? linkCaption = null , LogKind logKind = LogKind . Info )
46- {
47- if ( consoleLogger . Id != nameof ( ConsoleLogger ) )
48- throw new NotSupportedException ( "This method is expected logger that has ConsoleLogger id." ) ;
49-
50- // If clickable link supported. Write clickable link with OSC8.
51- if ( IsClickableLinkSupported . Value )
25+ if ( ! IsClickableLinkSupported )
5226 {
53- consoleLogger . Write ( logKind , @$ " { OSC8 } { link } { ST } { linkCaption ?? link } { OSC8 } { ST } " ) ;
54- return ;
27+ result = "" ;
28+ return false ;
5529 }
5630
57- // Write link as plain text. ( linkCaption is ignored)
58- consoleLogger . Write ( logKind , link ) ;
31+ result = @$ " { OSC8 } { link } { ST } { linkCaption ?? link } { OSC8 } { ST } " ;
32+ return true ;
5933 }
6034
61- private static readonly Lazy < bool > IsWindowsTerminal = new ( ( )
35+ public static bool IsWindowsTerminal => _isWindowsTerminal . Value ;
36+
37+ public static bool IsClickableLinkSupported => _isClickableLinkSupported . Value ;
38+
39+ private static readonly Lazy < bool > _isWindowsTerminal = new ( ( )
6240 => Environment . GetEnvironmentVariable ( "WT_SESSION" ) != null ) ;
6341
64- private static readonly Lazy < bool > IsClickableLinkSupported = new ( ( ) =>
42+ private static readonly Lazy < bool > _isClickableLinkSupported = new ( ( ) =>
6543 {
6644 if ( Console . IsOutputRedirected )
6745 return false ;
@@ -114,7 +92,6 @@ public static void WriteAsClickableLink(ILogger consoleLogger, string link, stri
11492 [ SupportedOSPlatform ( "windows" ) ]
11593 private static bool IsVirtualTerminalProcessingEnabled ( )
11694 {
117- // Try to get Virtual Terminal Processing enebled or not.
11895 const uint STD_OUTPUT_HANDLE = unchecked ( ( uint ) - 11 ) ;
11996 IntPtr handle = NativeMethods . GetStdHandle ( STD_OUTPUT_HANDLE ) ;
12097 if ( handle == IntPtr . Zero )
0 commit comments